6

I have an image (png) that I want to put underneath a heatmap(so to speak) made from a and a 2D matrix of values 0-1. So the intensity of the spot would be decided by how large the value in the matrix is.

I can use imshow(matrix) but that completely draws over the image underneath. Is it possible to perhaps, not draw any pixels with matrix values <.05 or some other way to make this work?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
Michael
  • 446
  • 2
  • 7
  • 19

2 Answers2

8

Here is an example of overlaying a binary heatmap on top of a color image:

%# some image
I = im2double( imread('peppers.png') );

%# I create here a random mask (gaussian centered in middle of image)
[r,c,~] = size(I);
[X Y] = meshgrid(1:r,1:c);
Z = mvnpdf([X(:) Y(:)], [r c]./2, diag(15.*[r c]));
Z = (Z-min(Z(:)))./range(Z(:));
Z = reshape(Z',[c r])';

%# show image and mask separately
subplot(121), imshow(I)
subplot(122), imshow(Z)

%# show overlayed images
figure, imshow(I), hold on
hImg = imshow(Z); set(hImg, 'AlphaData', 0.6);

%# also we can specify a colormap
colormap hsv

enter image description here enter image description here enter image description here

Amro
  • 123,847
  • 25
  • 243
  • 454
  • if you use a 2-D matrix with `'AlphaData'`, then you can apply colormap at selective places, without affecting other regions in the image. – Autonomous Apr 14 '14 at 00:28
  • 1
    @ParagS.Chandakkar: yes, in the example above we can use the mask matrix `Z` itself as the alpha map: `h=imshow(I); set(h, 'AlphaData',Z)` (in which case the image becomes transparent showing the figure gray background behind it) – Amro Apr 14 '14 at 06:34
1

the loaded png will be a three dimensional matrix. You can convert the 2d binary matrix into a 3d one with repmat. Then resize the binary matrix so it is the same size as the png with imresize. Finally, you can show the two matrices blended with something like imshow(alpha(myPng) + (1-alpha)*(myBinaryMat)) where alpha is a blending parameter between 0 and 1.

BlessedKey
  • 1,615
  • 1
  • 10
  • 16