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')

Réponses (2)

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
Image Analyst le 14 Déc 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 commentaires

Is there a way to loop the centroid for a 1-minute video of webcam snapshot?
Image Analyst
Image Analyst le 14 Déc 2022
I don't know what it means to "loop the centroid".
It is like tracking the motion between the blue and yellow tag while moving the tags in webcam, with the X, Y position, and distance changing relatively.
Image Analyst
Image Analyst le 14 Déc 2022
You can determine (locate) the centroids in a 1 minute video if that's what you mean. See attached demo.
Can I just apply the Color Thresholder app, for creating the mask for blue and yellow?
Image Analyst
Image Analyst le 14 Déc 2022
Yes, of course. I gave you a full demo of that in your duplicate question:
Did you not see it?
clc
clear all
close all
warning off
centroids=[];
c=webcam;
while true
e=c.snapshot;
mkdir=createMask(e);
mkdir=imfill(mkdir,'holes');
mkdir=bwareaopen(mkdir,20);
BW = bwareafilt(mkdir, 1);
L = bwlabel(BW);
s = regionprops(L, 'centroid');
centroids =[centroids;cat(1, s.Centroid)];
if(~isempty(centroids))
subplot(1,2,1);
imshow(e);
subplot(1,2,2);
imshow(e);
hold on
plot(centroids(:,1), centroids(:,2),'b-','linewidth',3);
else
subplot(1,2,1);
imshow(e);
subplot(1,2,2);
imshow(e);
end
drawnow;
hold off
end
Okay, thanks for that, now I have to do some light drawing, like draw object (house, etc.) functionality between two tags in the video webcam feed.
Walter Roberson
Walter Roberson le 15 Déc 2022
insertShape if you need to "draw" into an array.
Ahmad Muzaffar Zafriy
Ahmad Muzaffar Zafriy le 15 Déc 2022
Modifié(e) : Ahmad Muzaffar Zafriy le 15 Déc 2022
I have to do it in a webcam feed, not a static image.
Image Analyst
Image Analyst le 15 Déc 2022
You can use plot() to plot stuff in the overlay and it will remain in place even for a live image.
Walter Roberson
Walter Roberson le 15 Déc 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.
Any ideas, to capture the webcam image, after the drawing process?
Image Analyst
Image Analyst le 16 Déc 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
Walter Roberson le 16 Déc 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

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by