How to annotate border around original RGB image
9 views (last 30 days)
Show older comments
So I have a RGB image with two tags, I have binarized the image, removed the noise so it is just the two tags remaining. I have used rangefilt to then extract the border of each tag.
I now want to place the borders of the tag back on the original RGB image, with the border lines having 2 different colours for each tag. I am unsure how to do this, I have tried to use bwboundaries but this is unable to give me the outcome I desire with just the different colour borders alone on top of the original image.
The code:
clear all;
close all;
clc;
RGB = imread('tagPhoto.bmp');
%Yellow Tag
yellowChannel1Min = 0.000;
yellowChannel1Max = 255.000;
yellowChannel2Min = 71.000;
yellowChannel2Max = 255.000;
yellowChannel3Min = 0.000;
yellowChannel3Max = 40.000;
yellowSliderBW = (RGB(:,:,1) >= yellowChannel1Min ) & (RGB(:,:,1) <= yellowChannel1Max) & ...
(RGB(:,:,2) >= yellowChannel2Min ) & (RGB(:,:,2) <= yellowChannel2Max) & ...
(RGB(:,:,3) >= yellowChannel3Min ) & (RGB(:,:,3) <= yellowChannel3Max);
yellowBW = yellowSliderBW;
yellowMorph = imclose(yellowBW, ones(25));
%Red Border
redChannel1Min = 71.000;
redChannel1Max = 255.000;
redChannel2Min = 0.000;
redChannel2Max = 41.000;
redChannel3Min = 0.000;
redChannel3Max = 54.000;
redSliderBW = (RGB(:,:,1) >= redChannel1Min ) & (RGB(:,:,1) <= redChannel1Max) & ...
(RGB(:,:,2) >= redChannel2Min ) & (RGB(:,:,2) <= redChannel2Max) & ...
(RGB(:,:,3) >= redChannel3Min ) & (RGB(:,:,3) <= redChannel3Max);
redBW = redSliderBW;
redSE = strel("rectangle",[5 4]);
redOpen = imopen(redBW, redSE);
redMorph = imclose(redOpen, ones(25));
unionBW = redMorph | yellowMorph;
border = rangefilt(unionBW);
Input Image:
Binarised Image & Removed Noise:
Image with just borders (want to put over Input Image with different colours):
2 Comments
Shaista K
on 7 Dec 2022
hi good day can you please tell me how to find the centroid and region props readin on both these tags?
DGM
on 7 Dec 2022
% this is the mask that would be generated in the given code
% i'm just going to load a copy instead of pasting the entire thing again
unionBW = imread('union.png');
S = regionprops(unionBW,'area','centroid') % S is a struct of properties for all objects
vertcat(S.Area) % for example, show the areas
vertcat(S.Centroid) % for example, show the centroids
Accepted Answer
DGM
on 3 Nov 2022
Edited: DGM
on 3 Nov 2022
This is actually a good case for imoverlay(). The one thing imoverlay() does is render a binary mask as a colored overlay on an image.
RGB = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1179438/image.bmp');
%Yellow Tag
yellowChannel1Min = 0.000;
yellowChannel1Max = 255.000;
yellowChannel2Min = 71.000;
yellowChannel2Max = 255.000;
yellowChannel3Min = 0.000;
yellowChannel3Max = 40.000;
yellowSliderBW = (RGB(:,:,1) >= yellowChannel1Min ) & (RGB(:,:,1) <= yellowChannel1Max) & ...
(RGB(:,:,2) >= yellowChannel2Min ) & (RGB(:,:,2) <= yellowChannel2Max) & ...
(RGB(:,:,3) >= yellowChannel3Min ) & (RGB(:,:,3) <= yellowChannel3Max);
yellowBW = yellowSliderBW;
%Red Border
redChannel1Min = 71.000;
redChannel1Max = 255.000;
redChannel2Min = 0.000;
redChannel2Max = 41.000;
redChannel3Min = 0.000;
redChannel3Max = 54.000;
redSliderBW = (RGB(:,:,1) >= redChannel1Min ) & (RGB(:,:,1) <= redChannel1Max) & ...
(RGB(:,:,2) >= redChannel2Min ) & (RGB(:,:,2) <= redChannel2Max) & ...
(RGB(:,:,3) >= redChannel3Min ) & (RGB(:,:,3) <= redChannel3Max);
redBW = redSliderBW;
yellowMorph = imclose(yellowBW, ones(25));
redSE = strel("rectangle",[5 4]);
redOpen = imopen(redBW, redSE);
redMorph = imclose(redOpen, ones(25));
unionBW = redMorph | yellowMorph;
border = rangefilt(unionBW);
outpict = imoverlay(RGB,border,'g'); % pick any color
imshow(outpict)
If you want each border object to be a different color (i.e. a color from a colormap), you can use labeloverlay() instead.
L = bwlabel(border);
outpict = labeloverlay(RGB,L,'colormap',hsv(6));
imshow(outpict)
4 Comments
DGM
on 11 Dec 2022
Just considering the last part of the above code:
% ...
% create a colormap for all the objects
% this can be literally constructed, or you can use
% colormap tools like jet(), parula(), etc
mycolormap = [0.5 0 1; 1 0 0.5];
% if you want the border to be thicker, you could dilate it
fatborder = imdilate(border,strel('disk',5)); % set size
L = bwlabel(fatborder);
outpict = labeloverlay(RGB,L,'colormap',mycolormap);
imshow(outpict)
The ordering of the colors corresponds to the ordering of the labels in L. Tools like bwlabel(), regionprops(), bwconncomp() order the blobs as they scan the image columnwise from left to right. In this example, blob 1 is the red tag and blob 2 is the yellow tag.
More Answers (0)
See Also
Categories
Find more on Colormaps 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!