Overlaying Transparent Image with Clear Background on Original

12 views (last 30 days)
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
end
end
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'
...property
imagesc(cimg,'Parent',handles.axes1); % Display original image in
...GUI axes
colormap('gray'); % Set colormap to grayscale
axis off; % Turn off axis numbering
% OVERLAY SEMI-TRANSPARENT SEGMENTATION RESULT ON ORIGINAL IMAGE
hold on;
himage = imagesc(Lrgb,'Parent',handles.axes1);
set(himage,'AlphaData',0.2);

Accepted Answer

Susan
Susan on 29 Mar 2013
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.

More Answers (2)

Image Analyst
Image Analyst on 28 Mar 2013
  1 Comment
Susan
Susan on 28 Mar 2013
I am still unable to get it to work properly. When I follow the second example in the blog, I can no longer see the original image underneath. I used the segmented image as the 'influence image' for the solid green image and as such, am only able to see the solid green segmented area and a binary mask of my original image. I'm not sure what's happening. Here's the code:
L3 = bwlabel(L2); % Labels the segmented regions in L2
% Re-Display Image
imshow(originalimg,'Parent',handles.axes1,'InitialMag','fit');
axis off; % Turn off axis numbering
% Plot segmented regions transparently on top of original image
green = cat(3, zeros(size(originalimg)), ones(size(originalimg)), zeros(size(originalimg)));
hold on;
himage = imshow(green,'Parent',handles.axes1);
set(himage,'AlphaData',L3);
Is there any way to overlay specific regions of images semi-transparently so that the background doesn't distort the contrast?

Sign in to comment.


Muhammad Zeeshan Ahmed Khan
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)

Community Treasure Hunt

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

Start Hunting!