Finding the centroid of a binary image
Afficher commentaires plus anciens
vid_c111=read(v,1);
J = imcrop( vid_c111,[766 212 80 150]);
fontSize = 20;
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage > 150;
[rows, columns, numberOfColorBands] = size(J);
if numberOfColorBands > 1
grayImage = J(:, :, 3);
end
binaryImage = bwareaopen(binaryImage, 1000);
subplot(2, 1, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
labeledImage = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, 'Centroid');
numberOfBlobs = size(blobMeasurements, 1);
hold on;
for k = 1 : length(blobMeasurements)
x = blobMeasurements(k).Centroid(1);
y = blobMeasurements(k).Centroid(2);
plot(x, y, 'r+', 'MarkerSize', 30, 'LineWidth', 3);
end
The code above was what I used to try and find the centroid of two blobs in my image. Instead of finding the center, it only found the center of the entire image. Any help???
3 commentaires
Guillaume
le 17 Avr 2018
What do the grayImage and binaryImage look like in your figure?
Also, you are aware that you're not using the video frame you've just loaded, but some other image that is already in grayImage before you load that frame?
Guillaume
le 17 Avr 2018
Please don't edit your question to remove the useful parts nor remove your comments as it is necessary context to the answer you've accepted.
Rena Berman
le 15 Mai 2018
(Answers Dev) Restored edit
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Image Arithmetic dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!