我在文本文件中有32位像素的矩阵。我想将此数据转换为实际图像。
00000000000000000000000000000000 00000000000000000000000000000000
00000000000000000000000000000000 00000000000000000000000000000000
这是我在.txt文件中的输入数据存储。我的预期输出是2x2分辨率的黑色图像。在MATLAB中是否有任何特定的命令来执行此操作?
答案 0 :(得分:6)
以下示例代码执行以下步骤:
function [im, width, height] = load_image(filename)
fid = fopen(filename,'r');
first_line = fgetl(fid);
width = (length(first_line)+1)/33;
frewind(fid);
data = textscan(fid, '%s');
fclose(fid);
height = size(data{1},1) / width;
im = bin2dec(data{:})/2^32;
im = reshape(im, width, height)';
示例使用:
data.dat文件
00000000000000000000000000000000 00000000000000000000000000000000 10000000000000000000000000000000
00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000
10000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000
im = load_image('data.dat');
imshow(im);