I was able to fix my problem by using the regionprops and patch functions to find object extrema and plot patch regions semi-transparently on the original image.
Overlaying Transparent Image with Clear Background on Original
45 views (last 30 days)
Show older comments
Susan on 27 Mar 2013
Commented: Walter Roberson on 12 Mar 2021
Hello, I have a problem with creating a semi-transparent overlay that I hope someone can help me with. I am performing marker-controlled watershed segmentation for small regions in an image, and I'd like to overlay the result on the original image. However, when I do the overlay, the black background of the segmentation result is overlayed as well, making it very difficult to see the features in the original image and determine how well the segmentation worked.
Is there a way to create a semi-transparent overlay of segmented regions with the background of that overlay being clear instead of black or white? My code is below. If you would like to see sample images, I can e-mail them.
% REMOVE REGIONS IN LABEL MATRIX L THAT ARE LARGER THAN A CERTAIN SIZE
stats = regionprops(L,'Area');
L2 = zeros(s1,s2);
for z = 1:length(stats)
ind = find(L == z); % Find the locations of all pixels with the label z
if stats(z).Area <= 50
L2(ind) = 1; % Set all pixels with label z for regions smaller than 50 to white
L3 = bwlabel(L2); % Re-label the new regions so that the labels are sequential
Lrgb = label2rgb(L3,'jet','k','shuffle'); % Convert label matrix to RGB image
% RE-DISPLAY ORIGINAL IMAGE
title = horzcat('Slice ',num2str(slice),' of ',num2str(Nslc));
set(handles.axesttl,'String',title); % Reset the slicetitle 'String'
imagesc(cimg,'Parent',handles.axes1); % Display original image in
colormap('gray'); % Set colormap to grayscale
axis off; % Turn off axis numbering
% OVERLAY SEMI-TRANSPARENT SEGMENTATION RESULT ON ORIGINAL IMAGE
himage = imagesc(Lrgb,'Parent',handles.axes1);
More Answers (2)
Image Analyst on 28 Mar 2013
Please read Steve's blog on the topic: http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/
Muhammad Zeeshan Ahmed Khan on 12 Mar 2021
When you image() or imagesc() or imshow(), pass an option 'AlphaData' that is an array with the desired transparency, with 1 meaning to show the image completely and 0 meaning not showing the image at all (that is, show the background completely there.)
If the PNG had alpha information stored with it that you read with imread() then you should be able to use that transparency information directly.
[YourImage, ~, ImageAlpha] = imread('YourFile.png');
image(YourImage, 'AlphaData', ImageAlpha)
Walter Roberson on 12 Mar 2021
Copied without attribution from my answer to https://www.mathworks.com/matlabcentral/answers/365228-can-a-png-image-be-displayed-without-displaying-the-black-fill-in-the-guide#answer_289605
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!