How do you find the centroid of a particular Blob?

조회 수: 28 (최근 30일)
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2021년 1월 26일
댓글: KLETECH MOTORSPORTS 2021년 1월 29일
I have the following code:
clc;
clear all;
% This part reads and shows the image selected
I = imread('115.2.jpg');
imshow(I)
% Ginput is used to select the Hinge position
[x,y] = ginput(1)
% drawpoint is used to mark the Hinge position
h = drawpoint('Position',[x y]);
% Convert the RGB image to a Grayscale image
G=im2gray(I);
level = graythresh(G)
% Binarize the grayscale image
BW = imbinarize(G,level);
% Display the Grayscale image
figure
title('Grayscale Image')
imshow(G)
% Display the Binarized image
figure
title('Binarized')
imshow(BW)
% Fill holes
BW2 = imfill(BW,'holes');
% Display the Image after filling holes
figure
imshow(BW2)
title('Filled Image')
% Mark the Hinge here as well
h = drawpoint('Position',[x y]);
imwrite(BW2, 'BW2.jpg')
% Find the centroid
measurements = regionprops(BW2, 'Centroid');
imshow(BW2);
centroids = cat(1,measurements.Centroid);
hold on;
plot(centroids(:,1), centroids(:,2), 'r+', 'MarkerSize', 30, 'LineWidth', 1);
hold off;
where the image 115.2 is
And on running that code, I get:
How do I find the coordinates of the centroid of the rectangular blob? How do i neglect all centroids except the centroid of the rectangular blob?

채택된 답변

Gatech AE
Gatech AE 2021년 1월 26일
Since you're working with a binary image here, BW2, you can use the bwpropfilt function to clean up the small areas.
BW2 = bwpropfilt(BW2, 'Area', 1);
The last input argument determines the largest # of regions to keep, so it can be lowered to just the one. From there regionprops would only detect the single centroid.
You could also pass area as an input to regionprops as it is and use the centroid of the largest area.
measurements = regionprops(BW2, 'Centroid', 'Area');
[~,ind] = max(cat(1,measurements.Area));
centroid = measurements(ind).Centroid;
  댓글 수: 1
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2021년 1월 29일
Thanks, I made some edits and it worked.
I wanted to know how to save the centroid coordinates in the (x1,y1) format?
This is what i've done so far:
clc;
clear all;
% This part reads and shows the image selected
I = imread('115.2.png');
imshow(I)
% Ginput is used to select the Hinge position
[x,y] = ginput(1)
% drawpoint is used to mark the Hinge position
h = drawpoint('Position',[x y]);
% Convert the RGB image to a Grayscale image
G=im2gray(I);
level = graythresh(G)
% Binarize the grayscale image
BW = imbinarize(G,level);
% Display the Grayscale image
figure
title('Grayscale Image')
imshow(G)
% Display the Binarized image
figure
title('Binarized')
imshow(BW)
% Fill holes
BWfill = imfill(BW,'holes');
% Display the Image after filling holes
figure
imshow(BWfill)
title('Filled Image')
% Mark the Hinge here as well
h = drawpoint('Position',[x y]);
imwrite(BWfill, 'BWfill.png')
%Filter the rectangular bar from the image by Area
BWarea = imread('BWfill.png');
BW2 = bwareafilt(BWarea,[83800 83900]);
imwrite(BW2, 'BW2.png')
imshow("BW2.png")
% Find the centroid
measurements = regionprops(BW2, 'Centroid');
imshow("BW2.png");
centroids = cat(1,measurements.Centroid);
hold on;
plot(centroids(:,1), centroids(:,2), 'r+', 'MarkerSize', 30, 'LineWidth', 1);
h = drawpoint('Position',[x y]);
[A] = centroids %how do i save the centroid position as coordinates, and not a variable?
hold off;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 1월 26일
편집: Image Analyst 2021년 1월 26일
I think I already did this swinging pendulum panel thing before when you had a video. Not sure why you're posting it again. But anyway to get the centroid of the largest blob
% Get largest blob
bw2 = bwareafilt(bw2, 1);
% Find the centroid
measurements = regionprops(BW2, 'Centroid');
x = measurements.Centroid(1);
y = measurements.Centroid(2);
hold on;
plot(x, y, 'r+', 'LineWidth', 2, 'MarkerSize', 50)
  댓글 수: 2
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2021년 1월 29일
I'm getting unrecognized function or variable "bw2"
KLETECH MOTORSPORTS
KLETECH MOTORSPORTS 2021년 1월 29일
Tried BW2, got the same error

댓글을 달려면 로그인하십시오.

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by