如何将Imagesc框架传递给Matlab imwrite和imread?

时间:2016-10-12 18:31:15

标签: matlab matlab-figure

我拍摄了图像的灰色 - 彩色图框,尝试gray2ind - ind2rgb imwrite,然后imread再次读取它,但我收到的错误表明尺寸不匹配。

Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Error in ind2rgb (line 34)
  rout(:,:,1) = r;

Error in test_imagesc_output_imwrite (line 14)
Crgb = ind2rgb(Cind, parula(256)); % https://stackoverflow.com/a/39968435/54964

我不确定如何正确地将1-gray(1024)色彩图详细信息传递给gray2ind的代码;我认为gray2ind(I,256)会丢失一些信息;另外,ind2rgb(Cind, parula(256)不正确,但我不能直接使用1-gray(1024)

clear all; close all; clc; 
x = [5 8];
y = [3 6];
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
f=figure; 
hax=axes(f); 
imagesc(hax, x,y,C) % I could not use here I=imagesc and then I.CData for some reason
colormap(hax, 1-gray(1024)); 
I=getframe(hax); 
I=I.cdata; 
assert(isa(I, 'uint8'), sprintf('I is not uint8 but %s', class(I)));
Cind = gray2ind(I, 256); 
% TODO here something
Crgb = ind2rgb(Cind, parula(256)); % https://stackoverflow.com/a/39968435/54964
imwrite(Crgb, '/home/masi/Images/1.png');

I=imread('/home/masi/Images/1.png'); 
assert(isa(I, 'uint8'), sprintf('I is not uint8 but %s', class(I)));
f2=figure;
hax=axes(f2); 
imagesc(hax2, I); 

图。 1 Imagsc灰色图像,getframe并尝试由imwrite存储并由imread读取

enter image description here

Matlab:2016a
操作系统:Debian 8.5 64位
硬件:华硕Zenbook UX303UA
动机:我正在export_fig按照here所述获得人工制品,但现在也在观察从1-graygray的相移时保存[I,alpha]=export_fig(...)所以尝试使用Matlab {{} 1}} / imwrite

1 个答案:

答案 0 :(得分:2)

您的变量I是RGB数据(M x N x 3),因此当您将其传递给gray2ind时,您将获得M x N x 3索引矩阵。 ind2rgb接受一个二维索引数组,您正在传递它的3D数组。

我不太确定您的期望,但您可以先使用I

rgb2gray转换为真正的灰度图像
I = getframe(hax);
I = rgb2gray(I.cdata);
Cind = gray2ind(I, 256);
Crgb = ind2rgb(Cind, parula(256));

或者您可以跳过所有,只需将图片的色彩图设置为parula并将I.cdata直接传递给imwrite

imagesc(C, 'Parent', hax);

% Use an inverted parula colormap which seems to be what you're trying to do
colormap(flipud(parula(256)));
I = getframe(hax);

imwrite(I.cdata, '1.png')