Why my centroid are at different position?
3 views (last 30 days)
Show older comments
Why my centroid is at different position?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1230497/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1230502/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1230507/image.jpeg)
clear all; close all; clc;
RGB = imread('sample4.jpg');
Ibw = imread('taskC_tags.tif');
%Ibw = imfill(Ibw,'holes');
Ilabel = logical(Ibw);
stat = regionprops(Ilabel,'centroid');
imshow(RGB); hold on;
% for x = 1: num1(stat)
% plot(stat(x).Centroid(1),stat(x).Centroid(2), 'ro');
% text(stat(x).Centroid(1),stat(x).Centroid(2), num2str(x), 'Color', 'b')
% end
% plot(stat(12).Centroid(1),stat(12).Centroid(2),'ro'); %mark centroid1
% plot(stat(13).Centroid(1),stat(13).Centroid(2),'ro'); %mark centroid2
% plot([stat(12).Centroid(1),stat(13).Centroid(1),[stat(12).Centroid(2),stat(13).Centroid(2)]); %draw line
% text(100, 100, [strcat('centroid1(', num2str(stat(12).Centroid(1)),',',num2str(stat(12).Centroid(2)),')') ),'Color',
x1 = stat(66).Centroid(1,1); y1 = stat(66).Centroid(1,1);
x2 = stat(1).Centroid(1,1); y2 = stat(1).Centroid(1,1);
plot(x1,y1,'ro'); %mark centroid1
plot(x2,y2,'ro'); %mark centroid2
plot([x1,x2],[y1,y2]); %draw line
text(100, 100, (strcat('centroid1: (', num2str(x1),' , ',num2str(y1),')') ), 'Color', 'b')
text(100, 130, (strcat('centroid2: (', num2str(x2),' , ',num2str(y2),')') ), 'Color', 'b')
dchessboard = max(abs(x1-x2),abs(y1-y2));
text(100, 160, (strcat('Distance(chessboard) : ', num2str(dchessboard)) ),'Color', 'b')
0 Comments
Answers (2)
Walter Roberson
on 14 Dec 2022
x1 = stat(66).Centroid(1,1); y1 = stat(66).Centroid(1,1);
x2 = stat(1).Centroid(1,1); y2 = stat(1).Centroid(1,1);
Your x1 and y1 are assigned the same values. You should be extracting (1,2) for y
Image Analyst
on 14 Dec 2022
There is so much wrong with that I don't know where to start. But anyway, to answer your immediate question you need to change this
x1 = stat(66).Centroid(1,1); y1 = stat(66).Centroid(1,1);
x2 = stat(1).Centroid(1,1); y2 = stat(1).Centroid(1,1);
to this
x1 = stat(66).Centroid(1,1);
y1 = stat(66).Centroid(1,2);
x2 = stat(1).Centroid(1,1);
y2 = stat(1).Centroid(1,2);
If it's still no good then perhaps 1 and 66 are not always the indexes you want. They most certainly will NOT be if you have a different image. You need to improve your color segmentation algorithm to get only one blob for each colored card. Use the Color Threshold on the Apps tab of the tool ribbon. Threshold in HCV color space and then click the "Export function" button and call that function in your code. You'll have two of those functions, one for each color you want to detect.
14 Comments
Image Analyst
on 16 Dec 2022
What do you mean by "capture"? You already have the webcam image. Then if you burned some graphics into it, like with insertShape, then you have that RGB image as well. If you want to save it to disk, you can use imwrite.
Walter Roberson
on 16 Dec 2022
you are reading a webcam image into memory, processing the memory in various ways. You want to then draw on top of the image. But it is no longer the webcam image so capturing the webcam image after the drawing does not make sense.
If the goal is to capture the result of drawing on top of what was the webcam image, then that could make sense. If you use the insert* routines that I mentioned then their output *is * the captured results
See Also
Categories
Find more on Image and Video Ground Truth Labeling 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!