2017-06-06 15 views
1

我有一張表示多邊形的圖像。如何以編程方式將圖像處理爲黑色和白色並分離出多邊形

enter image description here

我要處理它在MATLAB和生成下面的圖片。

enter image description here

基本上我要求多邊形從圖像出來的其餘部分分離。這個問題得到靈感here

+1

是不是'的N×N'足夠的矩陣?這已經告訴你什麼屬於多邊形,什麼不是。您可以直接將它用作二進制圖像來生成問題中的第二張圖像。 – rayryeng

+0

我實際上錯誤地構思了這個問題。我想通過僅將第一圖像作爲輸入而不是矩陣來生成第二圖像。否則,正如所指出的那樣,問題變得微不足道。 –

+1

@ user_1_1_1這就是爲什麼我們都感到困惑。 :) – beaker

回答

1

我們只對紅色像素感興趣,我們可以使用第一個通道(紅色)來提取每個縮放像素的座標質心。由於相同座標之間可能存在細微差異,因此我們可以使用uniquetol函數的第三個輸出將絕對座標轉換爲相對座標,然後使用準確度座標將座標轉換爲二進制圖像。

[a,m]=imread('KfXkR.png');        %read the indexed image 
rgb = ind2rgb(a,m);         %convert it to rgb 
region = rgb(:,:,1)>.5;        %extract red cannel convert to binary to contrast red pixels 
cen = regionprops(region,'Centroid');     %find absolute coordinates of centeroid of each pixel 
colrow = reshape([cen.Centroid],2,[]);     %reformat/reshape 
[~,~,col] = uniquetol(colrow(1,:),0.1,'DataScale',1); %convert absolute coordinated to relative coordinates correcting possible slight variations 
[~,~,row] = uniquetol(colrow(2,:),0.1,'DataScale',1); 
result = accumarray([row col],1);      %make the binary image from coordinates of pixels 
imwrite(result,'result.png') 

換算的結果:

Scaled result

未調整:

Unscaled

0

我認爲功能contourc將得到ploygon:

C = contourc(img, [1 1]); % img is 2-D double in range [0 1] 

C輸出的格式是有點棘手。但對於一個等級輪廓來說,它應該很容易。您可以閱讀contourc的文檔來構造多邊形。

相關問題