-1

Basically, I'm trying to do the equivalent of MATLAB's image function on a background. The background is of the same size, if that helps.

In the standard colormap, the colors obtained using image (with a matrix as an argument) range from blue (low values) from yellow (high values).

I want to do such a plot, over an image background. The matrix data changes over time, so I have a loop and want to plot the matrix over the background at each step. I want to ignore the values < 0 from the matrix (i.e. display the original background at those places), and display only the positive ones (which will replace the original background).

How can I do this? Something close to what I want is the imfuse function. However, this also alters the color of the original image as a whole, as the matrix data changes with time. I don't want that. I want to change the background color only at those specific points where the data from the matrix is positive.

odnerpmocon
  • 282
  • 1
  • 4
  • 17

1 Answers1

0

Check the following solution; explanations are in the comments:

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

Result:

enter image description here


I hope this is what you have meant.
I had to make some assumptions, because your question is not so clear...

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • If a question is unclear, you should flag accordingly. I could make different assumptions and answer in a different way. Next time, please ask the OP first to clarify any unclarities. – Adriaan Feb 27 '17 at 22:03
  • @Adriaan Actually I did ask, but had no reply. I decided to deleted my comment and answer anyway. – Rotem Feb 27 '17 at 22:05
  • But what if the OP changes their question to clarify things and it turns out they mean something else entirely? It's not that I think this answer not useful, it's just not how the site is supposed to work and can cause problems. – Adriaan Feb 27 '17 at 22:08
  • @Adriaan I know, but when reading the question carefully, I think my assumptions are very close to what OP meant. – Rotem Feb 27 '17 at 22:12