Once centroid is calculated, how to assign x & y coordinates to it to then put into further calculations
2 views (last 30 days)
Show older comments
So as an overview, I have a picture with me holding two tags, one red one yellow.
I have then calulcated centroid to annotate the image with and caluclate the distance (City Block/Chessboard/Euclidean), as shown here:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1196963/image.png)
The only problem I am facing is that to get the centroid coordinates of each tag, I am using the following code:
redCentroid = regionprops(redMorph, 'centroid');
While this does work, it forces me to obtain the centroid coordinates through the command window. then assign them manually to x,y or i,j to the put into my distance calculations.
I am just wondering if there is a way to automatically designate the centroid coordinates to x & y (or i & j), instead of manually calculating through current method in command window then assigning to variable.
My full code is below:
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;
%Red Tag
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;
%Calculate Red Tag Centroid Coordinates
redSE = strel("rectangle",[5 4]);
redOpen = imopen(redBW, redSE);
redMorph = imclose(redOpen, ones(25));
redCentroid = regionprops(redMorph, 'centroid');
%Calculate Yellow Tag Centroid Coordinates
yellowMorph = imclose(yellowBW, ones(25));
yellowCentroid = regionprops(yellowMorph, 'centroid');
%Yellow/Red centroid coordinates
ri = 255.9213;
rj = 89.2483;
yi = 258.3529;
yj = 498.7177;
dCityBlock = (abs(ri - yi)) + (abs(rj - yj));
dChessboard = max(abs(ri - yi), abs(rj - yj));
dEuclidean = sqrt((ri - yi)^2 + (rj - yj)^2);
disp(['City Block Distance: ' num2str(dCityBlock)]);
disp(['Chessboard Distance: ' num2str(dChessboard)]);
disp(['Euclidean Distance: ' num2str(dEuclidean)]);
figure;
imshow(RGB);
hold on;
plot(89.2483,255.9213, 'g-x', 'LineWidth',5)
plot(498.7177,258.3529, 'g-x', 'LineWidth',5)
plot([89.2483,498.7177],[255.9213,258.3529],'b','LineWidth',2);
Thanks in advance for any help!
0 Comments
Accepted Answer
DGM
on 17 Nov 2022
You should just be able to get them from the struct.
%Yellow/Red centroid coordinates
ri = redCentroid.Centroid(2);
rj = redCentroid.Centroid(1);
yi = yellowCentroid.Centroid(2);
yj = yellowCentroid.Centroid(1);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!