1

I have an image that represents a polygon.

enter image description here

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

enter image description here

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

user_1_1_1
  • 903
  • 1
  • 12
  • 27
  • 1
    Isn't the matrix of `n x n` enough? That's already telling you what belongs to the polygon and what isn't. You can use that directly as a binary image to generate your second image in the question. – rayryeng Jun 06 '17 at 19:32
  • I actually framed the question incorrectly. I wanted to generate the second image by only giving first image as input not the matrix. Otherwise as pointed out the problem becomes trivial. – user_1_1_1 Jun 06 '17 at 21:17
  • 1
    @user_1_1_1 That's why we were all confused. :) – beaker Jun 06 '17 at 21:21

2 Answers2

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:

Scaled result

Unscaled:

Unscaled

rahnema1
  • 15,264
  • 3
  • 15
  • 27
0

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.

Xiangrui Li
  • 2,386
  • 2
  • 13
  • 17