如何在Matlab中查找甜甜圈(环形)形状的质心

问题描述 投票:-1回答:1

图像中有一个甜甜圈形状,我想用Matlab找到它的质心。

任何帮助将不胜感激。

matlab image-processing object-detection edge-detection centroid
1个回答
0
投票
clear; close all; clc
% load downloaded image
img = imread('centroid.jpg');
% crop the plotted part
%img = img(32:374, 74:507, :);
% threshold the image
gray = rgb2gray(img);
bin = gray > 65;
% number of pixels above threshold in each column
colwgt = sum(bin);
% number of pixels above threshold in each row
rowwgt = sum(bin, 2)';
% number of pixels above threshold
wgt = sum(bin(:));
% vertical position of the centroid
x = sum(colwgt .* (1:length(colwgt))) / wgt;
% horizontal position of the centroid
y = sum(rowwgt .* (1:length(rowwgt))) / wgt;
% visualize the result
img = insertMarker(img, [x y]);
imshow(img)
© www.soinside.com 2019 - 2024. All rights reserved.