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

问题描述:

我用灰色colormap框架的imagesc,尝试gray2ind - ind2rgbimwrite,然后再读imread但我得到的错误,指出尺寸不匹配。如何将Imagesc框架传递到Matlab 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://*.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://*.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
动机:I为保存时[I,alpha]=export_fig(...)用Matlab所以试图描述here但现在也观察相移从1-graygrayexport_fig正在逐渐假象imwrite/imread

你variabel I是RGB数据(M x N x 3),所以当你把它传递给gray2ind你找回指数矩阵。 ind2rgb接受二维索引数组,而不是您传递的3D数组。

我不太清楚你所期望的,但你可以先转换I一个真正的灰度图像先用rgb2gray

I = getframe(hax); 
I = rgb2gray(I.cdata); 
Cind = gray2ind(I, 256); 
Crgb = ind2rgb(Cind, parula(256)); 

或者你可以跳过这一切和刚刚成立的颜色表你的身影parula并直接传递到I.cdataimwrite

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

@Masi我只用parula因为多数民众赞成正是你贴过什么.... – Suever

+2

@Masi你不需要这两个,如果你只是想灰色......直接使用'I'。 – Suever

+0

@Masi我有点困惑你的意思。你是说如果你隐藏轴,当使用'getframe'时它仍然显示出来? – Suever