obtaining boundary of polygon embedded in a matrix and other post-processing techniques
<p>I have a 100x100 Boolean matrix called <code>mat</code>. All cells have false value except a continuous patch of polygonal area. I can read the cells belonging to this polygon by running through each cell of the matrix and finding true cells.</p>

<pre><code>region_of_interest=false(size(mat));
for i=1:size(mat,1)
    for j=1:size(mat,2)
        if mat(i,j)
            region_of_interest(i,j)=true;
        end
    end
end
</code></pre>

<p>Now I want to do further processing of this polygon like store only the boundary cells. How to do this? I tried visiting each polygonal cell and seeing if all its four neighbors are in the polygon or not. But this did not seem very efficient. Are there better algorithms out there?</p>

<p>If there are other post-processing methods that can be run in this scenario please suggest. Suggestions outside of Matlab are also welcome.</p>

<p>This is neither Python nor a convex-hull problem.</p>

<p>However, I would point out that boundary cells inside the polygon are true and have at least one neighbor which is false, and boundary cells outside the polygon are false and have at least one neighbor which is true.</p>

<p>It's up to you to decide whether you want inner or outer boundary cells, and what a "neighbor" is. For example, are the neighbors of a cell the four neighbors in cardinal directions, or the eight neighbors in diagonal directions too? If it's the former, then you'll still have to search through the eight neighbors to use the algorithm I describe below to get from cell 1 to cell 2 in cases like the following:</p>

<pre><code>...XX
...2X
XX1XX
XXXXX
</code></pre>

<p>That being said, it would depend on what your data is like as to how you'd want to write the algorithm. If you know that there is a single contiguous block with no holes in it, which seems to be what your question implies, then once you find a boundary cell, you simply need to walk along the boundary until you find the first cell again. So search neighbors until you find the next boundary cell, and then do it again.</p>

<p>The problem is finding that first boundary cell.</p>

<p>One method would be just searching randomly until you find a cell within the patch, and, then walking in any direction until you find a boundary.</p>

<p>Another would be to do some sort of uniform search, perhaps in a grid, until you find that same first cell as described above. If you know that your patch size is always at least x-cells large, you could adjust your grid size based on that.</p>

<p>Without more specific information, these ideas should at least help you reach a solution. Good luck!</p>

python matlab convex-hull