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

45 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.
GrayFig00RefFull=im2gray(Fig00RefFull);
GrayFig00RefEmpty=im2gray(Fig00RefEmpty);
% Check differences between images
figure()
Diff01=imshowpair(GrayFig00RefFull,GrayFig00RefEmpty,'diff');
title('Difference between (grayscale) Ref images Full and Empty');
% Save difference image as jpg to then process it as grayscale.
saveas(Diff01,'Diff01','jpg');
FigDiff01=imread('Diff01.jpg');
GrayFigDiff01=im2gray(FigDiff01);
%% Binarize
% Binarize a 2D grayscale image
Bi01= imbinarize(GrayFigDiff01);
imwrite(Bi01,'BiGrayFigDiff01.jpg');
%% Edge Detection Binary Image
% Read Binary image first
FigBiGrayFigDiff01=imread('BiGrayFigDiff01.jpg');
% Edge detection on graysc. image, as outcome from imshowpair-diff, with Canny method
EDBiC=edge(FigBiGrayFigDiff01,'Canny');
imwrite(EDBiC,'EDBiC.jpg'); BiC=imread('EDBiC.jpg');
%% Resize images
img2=BiC;
resizedimage = imresize(img2,[3024 4032]);
figure(),imshow(resizedimage);
axis('on', 'image');
%% Overlay images
I1 = GrayFig00RefFull;
I2 = imread('EDBiC.jpg') ;
image(I1)
hold on
image(I2);
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]);
figure
imshow(diffImg)
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');
figure
imshow(binaryDiffImg)
title('Binarized difference image')
% Trace binary objects
[B, L] = bwboundaries(binaryDiffImg,'noholes');
figure
imshow(fig00RefEmpty)
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'red', 'LineWidth', 4)
end
title('Overlayed images')
hold off
  3 Comments
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!