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