title: OpenCV imwrite() / imread() does either function modify the image in any way?
body: <p>I have the following situation, my in-memory image and what gets stored to disk and then read back do not equal, and I'd like to understand why and also how to 'fix' the difference.</p>
  
  <p>If anyone wants to know why I have both (in-memory and local disk) its because I was using the stored images to fine tune my image matching/recognition using OpenCV's sliding window without having to constantly wait on a video stream to show what I was looking for.</p>
  
  <p>The BGR frame comes from a video stream created through <code>ffmpeg</code> in <code>bgr24</code> pixel format that then gets processed like this:</p>
  
  <pre><code>self.raw_image = self.pipe.stdout.read(self.byte_length * self.byte_width * self.byte_offset)
  bgr_frame = np.frombuffer(self.raw_image, dtype=np.uint8).reshape((self.byte_width, self.byte_length, self.byte_offset))
  cv2.imwrite("capture/rgb/Frame_%d.png", self.frame_num), bgr_frame, [cv2.IMWRITE_PNG_COMPRESSION, 0])
  </code></pre>
  
  <p>And now things get interesting, the <code>bgr_frame</code> is sent to another function that cuts out regions of interests and then tries to match each region against a <code>lib_image</code> from a library of images, like this</p>
  
  <pre><code>res = cv2.matchTemplate(region, lib_image, 'cv2.TM_CCOEFF_NORMED')
  </code></pre>
  
  <p>the value of <code>res</code> returned for the <code>bgr_frame</code> hovers around the <code>.86</code> region which is <strong><code>.1</code></strong> lower than if the same image is read back from disk like this:</p>
  
  <pre><code>image = cv2.imread("%s/%s" % (input_directory, file))
  image = image[:,:,::-1] # Flip BGR to RGB
  </code></pre>
  
  <p>Pushing the <code>image</code> read from disk through the same function to match the region against the library results in matches that are in <strong><code>.96</code></strong> range!</p>
  
  <p>What am I missing? The OpenCV documentation doesn't mention doing anything to the image beyond encoding it in a specific format in my case <code>PNG</code> with <code>0</code> compression, and yet clearly what is in memory and what gets written/read to/from disk differs.</p>