2011-03-22 440 views
2

我正在使用Matlab的圖像工具箱。特別是,在對圖像進行二值化和標記後,我運行MatLab - 找到最接近的值的最佳方法

props = regionprops(labeledImage, 'Centroid'); 

以獲得所有連接對象的質心。現在,我想找到一個更接近一對座標(即圖像的中心)。當然,我知道我可以使用一個for循環檢查每個道具[I] .Centroid對座標,但這是緩慢的,並且必須有這樣做的matlaby方式...

這是...?

預先感謝

回答

3

REGIONPROPS從輸出將是一個N×1結構陣列與一個場'Centroid'包含1×2陣列。您可以首先使用函數VERTCAT將所有這些數組連接成一個N×2數組。然後,您可以使用函數REPMAT複製圖像中心座標(假設爲1乘2陣列),以便它變爲N乘2陣列。現在,您可以使用矢量運算計算距離和使用功能MIN找到的最小距離值的索引:

props = regionprops(labeledImage, 'Centroid'); 
centers = vertcat(props.Centroid); %# Vertically concatenate the centroids 
imageCenter = [x y];    %# Your image center coordinates 
origin = repmat(imageCenter,numel(props),1);  %# Replicate the coordinates 
squaredDistance = sum(abs(centers-origin).^2,2); %# Compute the squared distance 
[~,minIndex] = min(squaredDistance);    %# Find index of the minimum 

注意,因爲你只是想的最小距離,你可以用平方距離和避免不必要的電話SQRT。另請注意,函數BSXFUN可用作複製圖像中心座標以從對象質心中減去它們的替代方法。

+0

是啊!很高興,非常感謝你。 – 2011-03-22 20:42:07