How can I overlay an edge detected from a binary image onto a RBG image?

37 views (last 30 days)
The main structure of my code is the following. I completed all steps except the last part (in bold)
  • Read two RBG images with imread()
  • Convert to Grayscale with im2gray()
  • Check difference between them imshowpair()
  • Convert resulting difference image to binary with imbinarize()
  • Detect edges from white regions in binary image with edge()
  • Make an image overlaying the edges detected onto one of the original RGB images.
From the latter, I'm struggling with the following:
  • How do I keep only the edges? Should a threshold be applied to keep only the edges detected? How is this done?
  • When overlaying the resulting edges image onto the original, the black part appears in violet color. How is this removed?
  • How can I change the color of the edges when creating the overlaid image? i.e. from white to red.
  • I'm not resizing the image correctly to have them on same scale and have a fitting overlaid result. How is this done correctly?
The images used are attached. My code is shown hereunder and also attached.
Comments to improve the rest of the code are also appreciated, but not necessary.
Thanks in advance!
%% Initialization and read images
Fig00RefFull = imread('00-Ref-Full.jpg');
Fig00RefEmpty = imread('00-Ref-Empty.jpg');
%% Edit images into usable grayscale and find edges.
% Check differences between images
title('Difference between (grayscale) Ref images Full and Empty');
% Save difference image as jpg to then process it as grayscale.
%% Binarize
% Binarize a 2D grayscale image
Bi01= imbinarize(GrayFigDiff01);
%% Edge Detection Binary Image
% Read Binary image first
% Edge detection on graysc. image, as outcome from imshowpair-diff, with Canny method
imwrite(EDBiC,'EDBiC.jpg'); BiC=imread('EDBiC.jpg');
%% Resize images
resizedimage = imresize(img2,[3024 4032]);
axis('on', 'image');
%% Overlay images
I1 = GrayFig00RefFull;
I2 = imread('EDBiC.jpg') ;
hold on
hold off

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 27 Apr 2021
Edited: Subhadeep Koley on 28 Apr 2021
% Read the RGB images
fig00RefFull = imread('00-Ref-Empty.jpg');
fig00RefEmpty = imread('00-Ref-Full.jpg');
% Convert those to grayscale
fig00RefFullGray = im2gray(fig00RefFull);
fig00RefEmptyGray = im2gray(fig00RefEmpty);
% Calculate absolute difference image (tweak the parameters accordingly)
diffImg = imabsdiff(fig00RefFullGray, fig00RefEmptyGray);
diffImg = medfilt2(diffImg, [9 9]);
title('Difference image')
% Binarize and clean the binarized differnece image (tweak the parameters accordingly)
binaryDiffImg = imbinarize(diffImg, 0.1);
binaryDiffImg = medfilt2(binaryDiffImg, [9 9]);
binaryDiffImg = bwmorph(binaryDiffImg, 'open');
title('Binarized difference image')
% Trace binary objects
[B, L] = bwboundaries(binaryDiffImg,'noholes');
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'red', 'LineWidth', 4)
title('Overlayed images')
hold off
Subhadeep Koley
Subhadeep Koley on 28 Apr 2021
@Alejandro Manzano I have edited the previous answer. Have a look.
You can tweak the different parameters according to your requirement for a cleaner image.
Alejandro Manzano
Alejandro Manzano on 28 Apr 2021
@Subhadeep Koley. Thank you! This is what I was looking for. I appreciate it! I will check it with different cases and adjust the parameters. Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!