MATLAB Answers

Overlay two images of different type

17 views (last 30 days)
Xen on 15 Jan 2015
Edited: Matt J on 15 Apr 2016
I have a uint8 image and another logical image of the same dimensions, containing edges found from the first one. I want to make the white pixels (edges) of the logical image red (so must be converted to rgb) and then superimpose it over the original uint8 image. Transparency is not a requirement, I want the simplest code possible! Thanks everyone.

Accepted Answer

Xen on 19 Jan 2015
Thanks for your suggestions guys. After some experimentation, I have eventually found out how to do exactly what I needed (and it's not that difficult after all). If you suggest any alterations for coding improvement, I am glad to hear them.
% these 3 conversions from uint8 to RGB are required
% otherwise the images will not overlay
image = uint16(image); % image is the original image
[indexed,map] = gray2ind(image,256);
image2 = ind2rgb(indexed,map); % image2 is the original image in RGB
[rows, columns] = find(edges); % edges is the binary image
pixelcount = size(rows,1);
for i = 1:pixelcount
image2(rows(i),columns(i),1) = 1;
image2(rows(i),columns(i),2) = 0;
image2(rows(i),columns(i),3) = 0;
% ignoring the last two makes red transparent!
imwrite(image2, [mysavefolder, '\overlaid.png']);
Image Analyst
Image Analyst on 19 Jan 2015
Plotting markers is also in the overlay. Any sort of graphics that is overlaid above the image is considered in the overlay. It's not in the overlay if the graphics actually change the value of the image variable. If I overlay an image, or lines, or text, or markers, and then check the pixel value of the variable in my program you'll see the value of the image variable is no different before or after putting up the graphics. All that stuff stays in the overlay and does not change the underlying image, only what you see displayed. This is how overlay is defined though perhaps it doesn't agree with your preconceptions of what it should be.

Sign in to comment.

More Answers (4)

Image Analyst
Image Analyst on 15 Jan 2015
See these two links for demos and code:
Also check out the functions imshowpair() and imfuse() in the Image Processing Toolbox.
Image Analyst
Image Analyst on 18 Jan 2015
Just call export_fig after you've displayed your image and plotted dots over it. Check the file exchange for export_fig().

Sign in to comment.

Image Analyst
Image Analyst on 15 Jan 2015
Another simple way is to just get the coordinates of your binary image and then call plot to lay down a dot.
[rows, columns] = find(binaryImage); % Returns [y, x] since rows = y.
hold on;
plot(columns, rows, 'r.', 'MarkerSize', 1); % Increase marker size if you want.
Image Analyst
Image Analyst on 18 Jan 2015
plot(x, y, 'r.'); % Plot a single dot.

Sign in to comment.

Haripriya Sharma
Haripriya Sharma on 15 Apr 2016
MATLAB R2016a introduced "imoverlay" function in Image Processing Toolbox to accomplish this. For more information please refer to the following documentation page:
Matt J
Matt J on 15 Apr 2016
Hmmm. That's one of the unfortunate hazards of the FEX, I suppose. I've been using a function called imoverlay from the FEX for a few years now and my mfile library is now replete with it. If I now want access to R2016a's native imoverlay, or if I give my code to someone who does, I'll have to do a lot of search/replacing :-(

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by