title: Combining image RGB channels
body: <p>I have some problems with combining image channels in one rgb image. I use skimage and numpy for solving my problems. On input we have photo like this <a href="http://rghost.ru/8gYDcq2T6" rel="nofollow">http://rghost.ru/8gYDcq2T6</a>. With <code>numpy.array</code> slicing, I slice image in 3 parts by its height, then I slice edges of image (5% of height and width). Now I am ready to calculate mean squared error for two parts (part1 and part2, then for part1 and part3) for best image combining. I shift one part relatively to another on 15 pixels (left, right, up, down) and take minimum of all shifts, then I knew two shifts (x,y coordinates) for part1 and part2 and part1 and part3, then I use<code>numpy.dstack</code> for combining channels, and it's all. But quality in end is not good, so what's my mistake? I think problems with combining images in end using dstack, but cant solve it, coz dont understand, how to do it. There is my code:</p>
  
  <pre><code>    from skimage import data, io
      from numpy import *
  
      def metrics(first, second, x, y):
      reshaped_second = roll(second, x,0)
      reshaped_second = roll(reshaped_second, y, 1)
      reshaped_first = first
      mse = (((reshaped_first - reshaped_second) ** 2).sum())/(reshaped_first.size)
      return (mse, ncc, x, y)
  
      def align(path):
          image = data.imread(path)
          size = image.shape
          part1 = image[0 : size[0]/3, : ]
          part2 = image[size[0]/3 : 2*size[0]/3 , :]
          part3 = image[2*size[0]/3 : size[0], :]
          new_size = [min(part1.shape[0], part2.shape[0], part3.shape[0]), min(part1.shape[1], part2.shape[1], part3.shape[1])]
          part1 = part1[new_size[0]/100*5 : new_size[0] - new_size[0]/100*5, new_size[1]/100*5 : new_size[1] - new_size[1]/100*5]
          part2 = part2[new_size[0]/100*5 : new_size[0] - new_size[0]/100*5, new_size[1]/100*5 : new_size[1] - new_size[1]/100*5]
          part3 = part3[new_size[0]/100*5 : new_size[0] - new_size[0]/100*5, new_size[1]/100*5 : new_size[1] - new_size[1]/100*5]
          min_mse = 1000000000
          xx_1 = None
          yy_1 = None
          for x in range(-15, 16):
              for y in range(-15, 16):
                  mse = metrics(part1, part2,x,y)
                  if mse[0] &lt;= min_mse:
                      xx_1 = mse[2]
                      yy_1 = mse[3]
                      min_mse = mse[0]
  
          min_mse = 1000000000
          xx_2 = None
          yy_2 = None
          for x in range(-15, 16):
              for y in range(-15, 16):
                  mse = metrics(part1, part3,x,y)
                  if mse[0] &lt;= min_mse:
                      xx_2 = mse[2]
                      yy_2 = mse[3]
                      min_mse = mse[0]
          part2 = roll(part2, xx_1, 0) # numpy.roll()
          part2 = roll(part2, yy_1, 1)
          part3 = roll(part3, xx_2, 0)
          part3 = roll(part3, yy_2, 1)
          photo = dstack((part3,part2,part1))
          io.imshow(photo)
          io.show(
  </code></pre>
  
  <p>After execution of program I get such photo: <a href="http://rghost.ru/6fqqmFCnM" rel="nofollow">http://rghost.ru/6fqqmFCnM</a>, its first test image, on others worse. But want like better quality</p>
  
  <p>What can I do? Thank you for help.</p>
  
  <p><strong>PROBLEM FOUND: problem was with type of reshaped_first and reshaped_second in metrics function, they were 'uint8', and when I calculated reshaped_first - reshaped_second it was undefined behavior and I get unrepresentative metric. So 5/6 test photos have good combining, but last its about 15-20 pixels error. So new question is about what metrics I should choose for this problem? I tried normalized cross-correlation, but it's worse than mean squared error, that I use now</strong></p>