title: Finding furthest away pixel (Open CV)
body: <p>I've been struggling with a small problem for some time now and just can't figure out what is wrong. </p>
  
  <p>So I have a black 126 x 126 image with a 1 pixel blue border ( [B,G,R] = [255, 0, 0] ). 
  What I want, is the pixel which is furthest away from all blue pixels (such as the border). I understand how this is done. Iterate through every pixel, if it is black then compute distance to every other pixel which is blue looking for the minimum, then select the black pixel with the largest minimum distance to any blue. </p>
  
  <p>Note: I don't need to actually know the true distance, so when doing the sum of the squares for distance I don't square root, I only want to know which distance is larger (less expensive).</p>
  
  <p>First thing I do is loop through every pixel and if it is blue, add the row and column to a vector. I can confirm this part works correctly. Next, I loop through all pixels again and compare every black pixel's distance to every pixel in the blue pixel vector.</p>
  
  <p>Where blue is a vector of Blue objects (has row and column)
  region is the image</p>
  
  <pre><code>int distance;
  int localShortest = 0;
  int bestDist = 0;
  int posX = 0;
  int posY = 0;
  
  for(int i = 0; i &lt; image.rows; i++)
  {
      for(int j = 0; j &lt; image.cols; j++)
      {
          //Make sure pixel is black
          if(image.at&lt;cv::Vec3b&gt;(i,j)[0] == 0
              &amp;&amp; image.at&lt;cv::Vec3b&gt;(i,j)[1] == 0 
              &amp;&amp; image.at&lt;cv::Vec3b&gt;(i,j)[2] == 0)
          {
                  for(int k = 0; k &lt; blue.size(); k++)
                  {
                      //Distance between pixels
                      distance = (i - blue.at(k).row)*(i - blue.at(k).row) + (j - blue.at(k).col)*(j - blue.at(k).col);
  
                      if(k == 0)
                      {
                          localShortest = distance;
                      }
  
                      if(distance &lt; localShortest)
                      {
                          localShortest = distance;
                      }
                  }
  
              if(localShortest &gt; bestDist)
              {
                  posX = i;
                  posY = j;
                  bestDistance = localShortest;
  
              }
         }
      }
  }
  </code></pre>
  
  <p>This works absolutely fine for a 1 pixel border around the edge.
  <a href="https://dl.dropboxusercontent.com/u/3879939/works.PNG" rel="nofollow">https://dl.dropboxusercontent.com/u/3879939/works.PNG</a></p>
  
  <p>Similarly, if I add more blue but keep a square ish black region, then it also works.
  <a href="https://dl.dropboxusercontent.com/u/3879939/alsoWorks.PNG" rel="nofollow">https://dl.dropboxusercontent.com/u/3879939/alsoWorks.PNG</a></p>
  
  <p>But as soon as I make the image not have a square black portion, but maybe rectangular. Then the 'furthest away' is off. Sometimes it even says a blue pixel is the furthest away from blue, which is just not right.</p>
  
  <p><a href="https://dl.dropboxusercontent.com/u/3879939/off.PNG" rel="nofollow">https://dl.dropboxusercontent.com/u/3879939/off.PNG</a></p>
  
  <p>Any help much appreciated! Hurting my head a bit.</p>