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.
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.
The BGR frame comes from a video stream created through ffmpeg
in bgr24
pixel format that then gets processed like this:
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])
And now things get interesting, the bgr_frame
is sent to another function that cuts out regions of interests and then tries to match each region against a lib_image
from a library of images, like this
res = cv2.matchTemplate(region, lib_image, 'cv2.TM_CCOEFF_NORMED')
the value of res
returned for the bgr_frame
hovers around the .86
region which is .1
lower than if the same image is read back from disk like this:
image = cv2.imread("%s/%s" % (input_directory, file))
image = image[:,:,::-1] # Flip BGR to RGB
Pushing the image
read from disk through the same function to match the region against the library results in matches that are in .96
range!
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 PNG
with 0
compression, and yet clearly what is in memory and what gets written/read to/from disk differs.