Get indices of feature position on image mask in python,opencv2,numpy
<p>I am trying to extract index / position of two areas of a image mask and I am using a combination of python, opencv2, numpy and scipy.</p>

<p>I have a binary <code>mask</code> with the same size as the image. 
After generating labels and sizes of it with</p>

<pre><code>label_im, nb_labels = ndimage.label(mask)
sizes = ndimage.sum(mask, label_im, range(nb_labels + 1))
</code></pre>

<p>With this information, I am able to extract the biggest areas.</p>

<p>Let's say we have a 10x10 matrix:</p>

<pre><code> 1     1     1     0     0     0     0     0     0     0
 1     1     1     0     0     0     0     0     0     0
 1     1     1     0     0     0     0     2     2     0
 0     0     0     0     0     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0
</code></pre>

<p>I have 3 different areas (1,2,3) and I already know, that I need to further analyse area 2 and 3 (because those are the two biggest).</p>

<p>Now I want to find the index of the pixels which are </p>

<ol>
<li>topmost leftmost of the regions - marked below as (n)</li>
<li>lowermost rightmost of the regions - marked below with [n]</li>
</ol>

<p>same matrix as above:</p>

<pre><code> 1     1     1     0     0     0     0     0     0     0
 1     1     1     0     0     0     0     0     0     0
 1     1     1     0     0     0     0    (2)    2     0
 0     0     0     0     0     0     0     2     2     0
 0    (3)    3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2     2     0
 0     3     3     3     3     0     0     2    [2]    0
 0     3     3     3    [3]    0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0
</code></pre>

<p>How is the fastest way to do that?</p>

<p><strong>Problem 1:</strong>
I'm struggling with the syntax for those matrix conditionals.
For example, I try: </p>

<pre><code>mask2 = mask[label_im == 2]
</code></pre>

<p>It should get a binary mask again, but with only the cells set on which the original mask has a 2. 
I'm not exactly sure what the result of the above generates. And I'm therefor not able to process it further.</p>

<p><strong>Problem 2:</strong>
After that I will have to do something like finding the the indices of Row/Column where the topmost leftmost pixel is set.
Same to lowermost point.</p>

<p>For example for region 2, the results should be: (like the matrix above)</p>

<pre><code>point1-X = 7
point1-Y = 2
point2-X = 8
point2-Y = 7
</code></pre>

<p>where X stands for column, and Y for row (starting at index 0) </p>

<p>Or is there a better way to solve a problem like that with the combination of python/opencv2/numpy/scipy?</p>