2015-05-27 37 views
1

我有一个图像画线连接质心

enter image description here

我处理后找到质心,它有四个质心。 我的目标是我想用线连接它们并测量这个区域之间的角度。要明确质心和我的目标,您可以打开enter image description here

这是我的代码实现了质心

I = imread('22c.jpg'); 
Ibw = im2bw(I); 
Ibw = imfill(Ibw,'holes'); 

Ilabel = bwlabel(Ibw); 
stat = regionprops(Ilabel,'centroid'); 
imshow(I); hold on; 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro'); 
end 

的问题是,我仍然感到困惑做下(连接各重心和测量的角度)。我需要你的帮助,感谢

+1

你的任务不清楚。 “测量这个区域之间的角度”是什么意思? –

+0

图片中您也在广场内圈了圈,但是当我运行您的代码时,我没有看到那个圈子。你是怎么得到这个的? –

+0

这个问题和你[上一个问题](http://stackoverflow.com/q/30462831/2545927)有什么区别? – kkuilla

回答

1

这里是一个file exchange linkbresenham.m

改变了你的代码来获取所有的4个重心

%// read your input image 
im = imread('http://i.stack.imgur.com/xeqe8.jpg'); 

BW = im>220; 

CC = bwconncomp(BW); 
stat = regionprops(CC,'Centroid'); 

figure; imshow(BW); hold on 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro'); 
end 

这里是输出:

enter image description here

进一步实施:

%// putting all the Centroid coordinates into corresponding x,y variable 
x = [stat(1).Centroid(1),stat(2).Centroid(1),stat(3).Centroid(1),stat(4).Centroid(1)]; 
y = [stat(1).Centroid(2),stat(2).Centroid(2),stat(3).Centroid(2),stat(4).Centroid(2)]; 

%// obtain row and col dim 
[r,c] = size(BW); 

%// get all x,y values connecting the centroid points 
[xAll{1},yAll{1}] = bresenham(x(1),y(1),x(4),y(4)); 
[xAll{2},yAll{2}] = bresenham(x(2),y(2),x(3),y(3)); 
[xAll{3},yAll{3}] = bresenham(x(3),y(3),x(4),y(4)); 

%// change row and col subs to linear index 
for ii = 1:3 
    idx{ii} = sub2ind(size(BW),yAll{ii},xAll{ii}); 
end 

%// change grayscale image to 3D (as you want red line) 
out = repmat(im,[1,1,3]); 

%// obtaining corresponding index of all 3 slices 
for ii = 1:3 
    idxall{ii} = bsxfun(@plus, idx{ii},[0:2].*(r*c)); 
end 

%// keep only the index of 1st slice to 255 and changing rest to 0 to obtain a red line. 
%// Similar process for blue line except keep the index in the 3rd slice to 255 
out(cat(1,idxall{:})) = 0; 
out(idx{1}) = 255; 
out(idx{2}) = 255; 
out(idx{3}+2*(r*c)) = 255; 

%// see what you have obtained 
figure; imshow(out);hold on 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'bo'); 
end 

结果:

注:行可能看起来带点由于图片的尺寸较大,但其持续

enter image description here

最后一个数字放大看实线:

enter image description here

进一步说:

您可能必须采取@Spektre的意见,发现倾斜使用atan2角度。有关更多说明,请参阅his answer

+0

@Arsene我认为Arsene我们得到了4分(2个质心和2个骨骼边缘峰值),所以他有2条线/轴而不是1条。在这个解决方案中需要两条线/轴之间的角度。问题在于质心不精确,并不一定代表骨骼轴上的点,这使得它不能用于此任务(除非骨骼是对称的并且水平或垂直对齐)角度计算很容易,正如我在他之前的问题中所写的那样(通过使用2 x'atan2或atanxy'如'ang = | atan2(dir1)-atan2(dir2)|'或单个'acos(dot(dir1,dir2))' – Spektre

+0

@Spektre感谢您的建议,找到解决方案,但是你对角度的建议和Salai的答案可以结合起来作为比较技术。谢谢 – ArseneWe

+0

@ArseneWe如果你发现我们的答案很有帮助,请考虑接受我们的答案,如果可能的话,如你所说,你可以发表你自己的答案。结合我们的答案,并告诉我们你的结果:) –