I have below a contiguous patch of cells plotted in Matlab.
The outer cells of the red patch have to be determined and then a polygon joining the centers of these cells will give me a polygon. How do i compute the outer cells of the contiguous patch?
I have an array of integers whose elements denote the cell in the red patch, for example,
a=[1;64;23;456;345];
Each element , say 64 corresponds to a cell in the image, and it is the cell belonging to the red patch.
The motivation to solve the problem is to deal with a polygon with minimal number of edges rather than so many cells. it slows down computation. Convex hull is not good enough. I don't want the resulting polygon to overlap with the brown area at all.
What i am suggesting is the case on the left in image below but it seems ugly. So a better way would be as in right to just skip the cells only sharing a single point with the outer brown area. I would like my outer cells to then be only those that share more than just a single point with the outer brown area.
But we want to avoid large number of edges in the resultant polygon!
Using Image Processing toolbox You can apply
dilation
on the image and than applyand operator
between result of dilation and the original image.Although the answer by @rahnema1 is really cool, I think the OP is asking more how to extract the set of edges according to the described rules.
Here is my approach identifying all the 10 patterns of 2x2 pixels that contain edges. Assuming the matrix
A
has the image with1
s and0
s (A = zeros(ny, nx); A(a) = 1
):And here the result applying it to your image (I used GIMP for capture, resize and filter, so maybe the image is not exactly the same):
I am assuming that obtaining an ordered sequence of edges describing the polygon (or polygons) is not the main problem here once you have the aforementioned set.
I first processed the sample image from your question to create a logical mask (you already have an example of how to do that here).
Once you have this mask, there is a really easy way to generate the polygon you want using the
bwtraceboundary
function from the Image Processing Toolbox. This will give you a set of pixel indices in order around the perimeter of your masked region:And we can visualize it like so:
The coordinates for the red line are ordered starting from the green circle and moving clockwise around the perimeter pixels of the masked region.
If you would prefer to generate a boundary outline that follows along the perimeter pixel edges of the region instead of the perimeter pixel centers, you can use the solution from my answer to a related question. This will yield the following: