在MATLAB中显示图像上的矩阵数据

时间:2017-02-27 03:29:02

标签: image matlab matrix image-manipulation

基本上,我试图在背景上做相当于MATLAB的图像功能。如果有帮助,背景大小相同。

在标准色图中,使用图像(以矩阵作为参数)获得的颜色范围从蓝色(低值)到黄色(高值)。

我想在图像背景上做这样的情节。矩阵数据随着时间的推移而变化,所以我有一个循环,并希望在每一步的背景上绘制矩阵。我想忽略值<来自矩阵的0(即在那些地方显示原始背景),并仅显示正数(将替换原始背景)。

我该怎么做?接近我想要的是 imfuse 功能。然而,这也改变了原始图像的整体颜色,因为矩阵数据随时间变化。我不希望这样。我想仅在矩阵数据为正的特定点改变背景颜色。

1 个答案:

答案 0 :(得分:0)

检查以下解决方案;解释在评论中:

%Build input image, and backgound image
%Assume "plot" image is 2D matrix
%Assume backgound image is RGB color matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Read sample backgound image, and convert to double.
B = im2double(imread('peppers.png'));

%Read sample image, and convert to double.
I = im2double(imread('cameraman.tif'));

%Set some pixels of I to negative values.
I((end-80)/2+1:(end+80)/2, (end-80)/2+1:(end+80)/2) = -1;

%Pad input matrix I with surrounding negative values.
%Horizontal and vertical dimenstions of I are the same as dimenstions of B.
I = padarray(I, [(size(B,1)-size(I,1))/2, (size(B,2)-size(I,2))/2], -1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%Convert I to RGB, with parula colormap "range from blue (low values) to yellow (high values)".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Put I to temporary matrix T:
T = I;

%Set negative values to 0, before applying parula colormap.
T(I < 0) = 0;

%Convert T to RGB with parula colormap.
T = ind2rgb(T*255, parula(256));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Fuse background B and foreground T, only where I is positive (or zero).
%J - destination image (in RGB color space).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Duplicate I to 3D matrix (dimensions of III fit dimensions of T and B).
III = cat(3, I, I, I);

%Initialize J to B.
J = B;

%Set values of J to be equal to T where I >= 0.
J(III >=0) = T(III >= 0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure;imshow(J);

结果:

enter image description here

我希望这就是你的意思。
我不得不做一些假设,因为你的问题不是那么清楚......