title: compare pixels and sort
body: <p>This is kind of continue from <a href="https://stackoverflow.com/questions/34134252/from-float-array-to-mat-concatenate-blocks-of-image">here</a>.</p>
  
  <p>I have an image 800x800 which is broken down to 16 blocks of 200x200.</p>
  
  <p>I want to compare all the blocks between them in order to sort them in an order ( one image/block is continue of another ).</p>
  
  <p>So , I thought to compare the pixels at the boundaries between blocks.</p>
  
  <p>Since we have 16 blocks , they are laid as 4 rows x 4 cols.</p>
  
  <p>For example, for first row we will have 4 blocks , so I am comparing the first with the second.I am comparing the last column of first block with the first column of second block ( their boundaries ) to see if they match ( if one image is the continuing of the other).Then , I am sorting them.</p>
  
  <p>Because we have 4x4 dimensions , and because I am doing sort ( hence , I am using idx + 1 for the next block and idx for current ) I am applying the sort in every row.</p>
  
  <p>(Right now , I am sorting only through rows) </p>
  
  <p>The code is:</p>
  
  <pre><code>    vector&lt;Mat&gt; smallImages;
      vector&lt;Rect&gt; smallImageRois;
  
      Mat big1(800, 800, CV_32F); //write the blocks to this big image
      int big1step = big1.step1();
      float* pbig1 = big1.ptr&lt;float&gt;(0);
  
      // scan all the rows ( used for sort )
      for ( int k = 0; k &lt; blocks_width * blocks_height; k++ )//width=height=4
      {
          for (int idx = 0; idx &lt; blocks_width * blocks_height; ++idx)
          {
              float* pdata = (float*)smallImages[idx].data;
              int step = smallImages[idx].step1();
              Rect roi = smallImageRois[idx];
  
              for (int i = 0; i &lt; smallSize.height; ++i)//height is 200
              {
                  for (int j = 0; j &lt; smallSize.width; ++j)//width is 200
                  { 
                      //load the subimages to every roi
                      pbig1[(roi.y + i) * big1step + (roi.x + j)] = smallImages[ idx ].at&lt;int&gt;(i*step+j);
  
                      //exclude indices 3,7,11,15 ( since we are already using idx + 1 ) in order to jump for every row
                      if ( idx != (blocks_width -1) &amp;&amp; idx != (2 * blocks_width-1) &amp;&amp; idx != (3 * blocks_width -1) 
                      &amp;&amp; idx != blocks_width * blocks_height -1 )
                      {
  
                           //compare pixels at boundaries
                           // i am scaning for every row the first column (roi.x + 0 ) of the next image to the last column (roi.x + (subImageSize.width-1) of the previous
                           if ( abs( sqrt( pow( (pbig1+1)[(roi.y + i) * big1step + (roi.x + 0)] , 2 ) -  pow(  pbig1[(roi.y + i) * big1step + (roi.x + (subImageSize.width-1))] , 2 ) ) ) &lt; 4 )
                              {
  
                                  //do sort
                                  int temp =  (pbig1+1)[(roi.y + i) * big1step + (roi.x + j)];
                                  (pbig1+1)[(roi.y + i) * big1step + (roi.x + j)] = pbig1[(roi.y + i) * big1step + (roi.x + j)];
                                  pbig1[(roi.y + i) * big1step + (roi.x + j)] = temp;
  
                              }
                         }
  
                  }
              }
          }
      }
  </code></pre>
  
  <p>1) I am receiving exactly the same image as the original.No sort , no comparing!</p>
  
  <p>2) I am using <code>smallImages[ idx ].at&lt;int&gt;(i*step+j);</code>  and not <code>pdata</code> that I declared in the loop because I can't understand why these two give different results:</p>
  
  <pre><code>smallImages[ 2 ].at&lt;int&gt;(i*step+j)
   (pdata+ 2)[ i * step + j ]  //doesn't this point to idx = 2?
  </code></pre>