# assigning centroids to xy coordinates

8 views (last 30 days)
C.G. on 21 May 2020
Commented: C.G. on 26 May 2020
I have identified the centroids of a series of circles on 2 frames of a video.
I have tried to compute the distance between these centroids and then assign these to x and y coordinates for each frame, however the centroids do not match up the the locations; they are supposed to be on the black spots on the ball.
Could anybody help?
%% Crop the images to only 1 ball per image and compute pentagon velocity
r = 21; %radius of whole ball
thresholdvalue = 13; %use as threshold when binarizing
for k = 1:numFrames
%subimage the balls in frames 1 and 2 by shrinking them to the coordinates of the balls
%should produce a series of images, where each image only shows 1 ball,for all the balls in the video
subframe1 = frame_1(round(yb_1{k})-r:round(yb_1{k})+r, round(xb_1{k})-r:round(xb_1{k})+r);
subframe2 = frame_2(round(yb_2{k})-r:round(yb_2{k})+r, round(xb_2{k})-r:round(xb_2{k})+r);
figure(4)
subplot(2,1,1)
imshow(subframe1)
subplot(2,1,2)
imshow(subframe2)
end
for k = 1:numFrames
%identify the circles in frames 1 and 2 with radii between the defined min and max
%imfindcircles is a function in matlab that finds circles between a
%radius range
pentagons_1 = subframe1 < thresholdvalue; % if thresholdvalue = <, searches for dark objects in the video frames, > searches for light objects
pentagons_2 = subframe2 < thresholdvalue;
figure(5)
imshow(pentagons_1)
pentagons_1 = imclearborder(pentagons_1); % get rid of black background in video frames
pentagons_2 = imclearborder(pentagons_2);
pentagons_1 = bwareafilt(pentagons_1, [5, inf]); % find blobs >5 pixels (i.e. pentagons on the balls)
pentagons_2 = bwareafilt(pentagons_2, [5, inf]);
props_1 = regionprops(pentagons_1, 'Centroid'); % get the centroids of the pentagons in frame 1
props_2 = regionprops(pentagons_2, 'Centroid'); % get the centroids of the pentagons in frame 2
centroids_1 = cat(1,props_1.Centroid); %store the centroid of each blob
centroids_2 = cat(1,props_2.Centroid);
%check centroids in both subframes
figure(6)
subplot(2,1,1)
imshow(pentagons_1)
hold on
plot(centroids_1(:,1),centroids_1(:,2),'b*')
hold off
subplot(2,1,2)
imshow(pentagons_2)
hold on
plot(centroids_2(:,1),centroids_2(:,2),'r*')
%identify where each circle has moved between frames 1 and 2
%returns the distance from each point in centres_2 to the corresponding point in centres_1
[indexp,distp] = dsearchn(centroids_2,centroids_1);
%here we have the distances not in order
%assign the circles from frames 1 and 2 to x and y coordinate variables
xp_1{k} = centroids_1(:,1);
xp_2{k} = centroids_2(:,1);
yp_1{k} = centroids_1(:,2);
yp_2{k} = centroids_2(:,2);
%check its worked
figure(7)
subplot(2,1,1)
imshow(frame_1)
hold on
scatter(xp_1{k},yp_1{k},'r*')
subplot(2,1,2)
imshow(frame_2)
hold on
scatter(xp_2{k},yp_2{k},'b*')
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Image Analyst on 21 May 2020
You need to combine those two for loops into one. Otherwise subframe1 and subframe2 are only the very last ones from your first loop.
##### 8 CommentsShowHide 7 older comments
C.G. on 26 May 2020
Hello just wondering if you were still able to help me with this issue?

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!