如何在MATLAB中读取base64图像? [英] How to read out base64 image in MATLAB?

查看:311
本文介绍了如何在MATLAB中读取base64图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following base64 string which is an image: Link to base64 image and I wish to get back the image in Matlab. I used the following website to check if the image is valid: https://codebeautify.org/base64-to-image-converter and it worked without any problems.

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

When downloading the image after decoding it from the website, I get the following information:

图片的大小为512x512,位深度为32bit.此外,我知道这是彩色图像.

The image has the size of 512x512 and a bit depth of 32bit. Furthermore, I know that this is a color image.

我尝试了此处提到的方法在matlab中将base64解码为RGB图像,它利用了Apache库.但是,使用以下代码时:

I have tried the approach mentioned here Decode base64 to RGB image in matlab which utilizes the Apache library. However, when using the following code:

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

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

where img is the base64 string, result will be a 599636 long char array. However, 512*512*3 = 786432.

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

I am wondering how to proceed from here to get the 512*512 color image ?

我当前的解决方法是将base64编码的图像写入文本文件,并通过python读取,然后将其作为图像写入磁盘,然后将其重新加载到matlab中.哪个可行,但非常非常难看.在Python中,它可以与简单的脚本一起使用:

My current workaround is to write the base64 coded image to a text file, and read it out via python, and write it to the disk as image, and reload it in matlab. Which works, but is very very ugly. In Python it works with the simple script:

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')))

推荐答案

% 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);

这篇关于如何在MATLAB中读取base64图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆