【问题标题】:How to read out base64 image in MATLAB?如何在 MATLAB 中读取 base64 图像?
【发布时间】:2017-12-25 10:02:54
【问题描述】:

我有以下 base64 字符串,它是一个图像:Link to base64 image 我希望在 Matlab 中取回图像。我使用以下网站检查图像是否有效:https://codebeautify.org/base64-to-image-converter 并且没有任何问题。

从网站解码后下载图像时,我得到以下信息:

图像的大小为 512x512,位深为 32 位。此外,我知道这是一张彩色图像。

我已经尝试过这里提到的方法Decode base64 to RGB image in matlab,它利用了 Apache 库。但是,当使用以下代码时:

result = native2unicode(base64.decode(uint8(img)).');

其中img 是base64 字符串,结果将是一个599636 长字符数组。但是,512*512*3 = 786432

我想知道如何从这里开始获得 512*512 彩色图像?

编辑:

我目前的解决方法是将base64编码的图像写入文本文件,并通过python将其读出,并将其作为图像写入磁盘,然后在matlab中重新加载。哪个有效,但非常非常难看。在 Python 中,它使用简单的脚本:

import base64
f = open('test.txt','r')
message = f.read()
print(message)
with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(bytearray(message,'utf-8')))

【问题讨论】:

    标签: image matlab base64


    【解决方案1】:
    % Create a Java Base64 instance...
    import('org.apache.commons.codec.binary.Base64');
    base64 = Base64();
    
    % Read the content of the file as text...
    text = fileread('test.txt');
    text = strtrim(text);
    
    % Decode the text into a byte array...
    img_bytes = base64.decode(text_bytes);
    
    % Fill a temporary file with the decoded byte array...
    fid = fopen('test.bin','w');
    fwrite(fid,img_bytes,'int8');
    fclose(fid);
    
    % Read the temporary file as image and show the image...
    img = imread('test.bin');
    imshow(img);
    

    【讨论】:

    • 谢谢,我会试试看 - 是否可以在不将其写入磁盘的情况下访问图像?
    • 我没有找到快速现成的解决方案,但一定有。
    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多