我使用以下代码检测gray scale image中的白点。
Gimg=imread('hi.tif','tif');
BW=Gimg>150;
rp=regionprops(BW,Gimg,'WeightedCentroid');
disp('Centroiding is done.');
figure(1); image(Gimg); axis image; hold on;
n=numel(rp);
pos = rp.WeightedCentroid; %all positions
for ct = size(pos,1):-1:1 %check them backwards
d = sum((pos-pos(ct,:)).^2); %distance to all other points (kd-trees could be used for speedup)
if min(d)<1^2,pos(ct,:)=[];end %remove if any point is too close
end
for i=1:pos
plot(rp(i).WeightedCentroid(1), rp(i).WeightedCentroid(2), 'wX', 'markers',15)
end
但是,我只想保留我用红色勾勒出的图像部分的白点:
因此,所有prev_*
变量。
我该怎么做?
答案 0 :(得分:0)
这种溶解怎么样?检查所有(平方)距离,如果另一个距离太近,则移除该点。
pos = rp.WeightedCentroid; %all positions
for ct = size(pos,1):-1:1 %check them backwards
d = sum((pos-pos(ct,:)).^2); %distance to all other points (kd-trees could be used for speedup)
if min(d)<50^2,pos(ct,:)=[];end %remove if any point is too close
end
答案 1 :(得分:0)
您可以使用kmeans群集。阅读baout kmeans聚类。
clc ; clear all ;
I=imread('r7oR4.jpg');
I = rgb2gray(I) ;
%% Remove borders
n = 30 ;
I1 = I(n:end-n,n:end-n) ;
[y,x,val] = find(I1) ;
%%
N = 5 ;
[idx,C] = kmeans([x,y],N) ;
%% figure
figure
imshow(I1)
hold on
Cm = {'.r' '.b' '.g' '.y' '.m'} ;
for i = 1:N
plot(x(idx==i),y(idx==i),Cm{i})
end