Setting transparency for overlaying images

26 views (last 30 days)
Amadeus
Amadeus on 16 Feb 2013
Hi,
I am trying to overlay an image onto another. Currently I am using the following code however the problem is that, my transparency is always the same (0.75) for all images. However I need to have image specific transparency value. For example for that image, if I2 is more dominant then it should be seen dominant. But if I1 is dominant, then I2 should be seen more transparent. (I guess I need to set something like AlphaData but?)
I hope I could explain what I need. I will be very grateful if you can help me.
Regards, Amadeus
Here is the code:
for slicenum = 1 : size(nii.img,3)
I1 = nii.img(:,:,slicenum);
I2 = uint16(M(:,:,slicenum));
RGB2=ind2rgb(I2,colormap(hot));
figure,imshow(I1,[]);
hold on; h = imshow(RGB2);
alpha(0.75);
save_file_name = strcat(num2str(slicenum),'.jpg');
saveas(gcf,save_file_name);
pause(0.25)
end

Answers (2)

Image Analyst
Image Analyst on 17 Feb 2013
See Steve's blog: http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/ Note how he changes the transparency as a function of location. Steve says:
"Using image data to control transparency
My second transparency example gets a bit more creative. I'll display an image that's a solid color, but I'll use another data set to vary the solid color image's transparency on a pixel-by-pixel basis. "
which sounds like it's exactly what you want to do.

Walter Roberson
Walter Roberson on 17 Feb 2013
Calculate a variable, "thisalpha", as seems suitable, before doing the imshow(RGB2). Then,
imshow(RGB2, 'AlphaData', thisalpha);
And remember to destroy the figure afterwards or else your graphics will get slower and slower as you accumulate figures.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!