Why my centroid are at different position?
Show older comments
Why my centroid is at different position?



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')
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
1 Comment
Ahmad Muzaffar Zafriy
on 14 Dec 2022
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
Ahmad Muzaffar Zafriy
on 14 Dec 2022
Image Analyst
on 14 Dec 2022
I don't know what it means to "loop the centroid".
Ahmad Muzaffar Zafriy
on 14 Dec 2022
Image Analyst
on 14 Dec 2022
You can determine (locate) the centroids in a 1 minute video if that's what you mean. See attached demo.
Ahmad Muzaffar Zafriy
on 14 Dec 2022
Image Analyst
on 14 Dec 2022
Yes, of course. I gave you a full demo of that in your duplicate question:
Did you not see it?
Ahmad Muzaffar Zafriy
on 15 Dec 2022
Edited: Ahmad Muzaffar Zafriy
on 15 Dec 2022
Walter Roberson
on 15 Dec 2022
insertShape if you need to "draw" into an array.
Ahmad Muzaffar Zafriy
on 15 Dec 2022
Edited: Ahmad Muzaffar Zafriy
on 15 Dec 2022
Image Analyst
on 15 Dec 2022
You can use plot() to plot stuff in the overlay and it will remain in place even for a live image.
Walter Roberson
on 15 Dec 2022
You read the image from the camera. That gives you an array.
You process the array, and identify locations of interest in the array. Perhaps you extract sections of the image and put the sections through something like alexnet to identify the pieces. Now you want to output an annotated version of the image.
You take a copy of the image array. You insertShape if you want to "draw" into the copy of the image, such as drawing bounding boxes, or a stylized "house" or whatever. You insertText if you want to draw a text label into the copy of the image.
Now you display the annotated copy of the image.
Ahmad Muzaffar Zafriy
on 16 Dec 2022
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
Categories
Find more on Image Preview and Device Configuration 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!