How to programatically process the image to black and white and separate out the polygon

萝らか妹 提交于 2019-12-11 04:54:51

问题


I have an image that represents a polygon.

I want to process it in matlab and generate the image below.

Basically i am asking to separate the polygon from the rest of the image out. This question got inspired here.


回答1:


We only interested in the red pixels we can use the first channel(Red) to extract coordinates centroid of each scaled pixel. Since there may be slight differences between the same coordinates we can use third output of the uniquetol function to convert absolute coordinates to relative coordinates then use accumarray to convert coordinates to a binary image.

[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:




回答2:


I think function contourc will get the ploygon:

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

The format of output C is a little tricky. But for one level contour, it should be easy. You can read the documentation for contourc to construct the polygon.



来源:https://stackoverflow.com/questions/44398320/how-to-programatically-process-the-image-to-black-and-white-and-separate-out-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!