How do I find the centre of an image?

16 ビュー (過去 30 日間)
Kantachai Chamnarnkit
Kantachai Chamnarnkit 2023 年 2 月 8 日
コメント済み: DGM 2023 年 3 月 15 日
I want to find the centre of the white area and measure the length of the x and y axes.
  2 件のコメント
Sarvesh Kale
Sarvesh Kale 2023 年 2 月 8 日
It would be nice if you upload the image as an attachment
DGM
DGM 2023 年 3 月 15 日
As I have a feeling that the question being asked isn't what's actually needed, see also:
Jonas' answer gives the section widths intersecting the blob centroid -- what the question asks for.
The above link also covers similar size metrics:
  • height and width of bounding box
  • equivalent diameters
  • feret diameters
Sarvesh's answer below already touched on two of those.

サインインしてコメントする。

回答 (2 件)

Jonas
Jonas 2023 年 2 月 8 日
you can use regionprops for that and let the function return the centroid. This may be the cetner you are searching for.
If you are then interested in the horizontal and vertical extent trough this point, you could just search for the first/last white pixel in the row/column given by the coordinates of the centroid. given centroidX,centroidY you could use something like
find(img(:,centroidX),1,'last')-find(img(:,centroidX),1,'last')
% and
find(img(centroidY,:),1,'last')-find(img(centroidY,:),1,'last')
  2 件のコメント
Kantachai Chamnarnkit
Kantachai Chamnarnkit 2023 年 2 月 9 日
I don't know how to search for the first/last white pixel in the row/column
DGM
DGM 2023 年 2 月 9 日
編集済み: DGM 2023 年 2 月 9 日
You use find() like Jonas mentioned.
img = imread('tiltellipse.png')>128;
% get the centroid
S = regionprops(img,'centroid');
centroidX = round(S.Centroid(1)); % round it for indexing purposes
centroidY = round(S.Centroid(2));
% i'm doing it this way so that i can plot the results
yrange = [find(img(:,centroidX),1,'first') find(img(:,centroidX),1,'last')];
xrange = [find(img(centroidY,:),1,'first') find(img(centroidY,:),1,'last')];
blobheight = diff(yrange)
blobheight = 128
blobwidth = diff(xrange)
blobwidth = 121
% plot the lines that are being measured
imshow(img); hold on
plot(xrange,[1 1]*centroidY,'r-o');
plot([1 1]*centroidX,yrange,'b-o');
Note that this gives the maximum width along each line. Consider how this would behave with an object like this:
It's up to you to decide what you want to measure.

サインインしてコメントする。


Sarvesh Kale
Sarvesh Kale 2023 年 2 月 8 日
編集済み: Sarvesh Kale 2023 年 2 月 8 日
the following code snippet might help you. Assuming the image in binary in nature
i = imread('image.jpeg'); % name of your image should be inside ' '
blobs=regionprops(i,"Centroid","MinorAxisLength","MajorAxisLength","BoundingBox");
for more information on regionprops
  1 件のコメント
Jonas
Jonas 2023 年 2 月 8 日
MinorAxisLength / MajorAxisLength does not give horizontal and vertical size of the blob. it assumes an ellipse and takes the axes of the ellipse, which can be rotated
the bounding box has the problem that the respective line will not necessarily go through the center

サインインしてコメントする。

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by