How to annotate border around original RGB image

조회 수: 9 (최근 30일)
Ted
Ted 2022년 11월 3일
댓글: Shaista K 2022년 12월 14일
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
Shaista K
Shaista K 2022년 12월 7일
hi good day can you please tell me how to find the centroid and region props readin on both these tags?
DGM
DGM 2022년 12월 7일
% 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
S = 2×1 struct array with fields:
Area Centroid
vertcat(S.Area) % for example, show the areas
ans = 2×1
2795 2947
vertcat(S.Centroid) % for example, show the centroids
ans = 2×2
89.2483 255.9213 498.7177 258.3529

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2022년 11월 3일
편집: DGM 2022년 11월 3일
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
DGM
DGM 2022년 12월 11일
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.
Shaista K
Shaista K 2022년 12월 14일
thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by