0

I did some processing on an input image and saved the final image using cv2.imwrite method, input image size in 1.2MB (3120 * 4160 pixels) and the saved image is of size 2.8MB (3111 * 4151), which is more than double the size of input image.

After some research I've found that, opencv saves the image in 16bit rate, so I used scipy.misc.imsave for saving the image, this solves my problem, the resultant size decreased to 1.1MB but the colormap changes, original and processed images are not in the same color.

Can anyone please shed some light on why this happens? why did the image size almost double incase of cv2 write method and why did i lose the color using scipy write method?

from matplotlib import pyplot
import cv2
from scipy.misc import imsave
'''
read the input image here (jpg format) and image segmentation code here
'''
pyplot.imsave('py_result.jpg', final_img) #this one doubles the image size and also changes the color, why is there a size bump?   
cv2.imwrite('cv2_result.jpg', final_img) #this one almost doubles the image size
imsave('scipy_result.jpg', final_img) #this one doesnot bump the image size but changes the color in output image

Here are the images for reference:

This is the input image.

inputimage

The processed image saved using scipy.imsave() as you can see the blue text in the input image has changed to red.

scipy written image

Beeti Sushruth
  • 191
  • 1
  • 8
  • 1
    1) You haven't shown any code, that's most likely why nobody seems to care to answer your question. Please see: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) 2) I guess, your input image is in JPG format!? In OpenCV, do you also save as JPG? If so, which compression do you use? Or, do you save as PNG, which most likely generates larger files? 3) For the `scipy.imsave()`, it seems that the blue and red channels are switched. OpenCV uses BGR color order, maybe scipy uses RGB. – HansHirse Sep 17 '19 at 04:24
  • @HansHirse Sorry I didnot attach the code, I did it now, I think you're right about why the color changes, openCV treats images as BGR while the other RGB. But why the size bump? Both input and output images are in jpg format. Thanks for taking your time. – Beeti Sushruth Sep 17 '19 at 04:43
  • 1
    OpenCV documenation on [`ImWriteFlags`](https://docs.opencv.org/4.1.1/d4/da8/group__imgcodecs.html#ga292d81be8d76901bff7988d18d2b42ac): _For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95._ Maybe, the compression of your input is lower!? Also, from your code it's not clear, what happens between reading and saving. Do you possibly do some operations (implicitly) converting from `CV_8UC3` to `CV_16UC3`!? Please provide a [mre], everything else is further guesswork. – HansHirse Sep 17 '19 at 04:52
  • The processing would be this code : [Image Segmentation Using Thresholding](https://stackoverflow.com/a/47898407/10337002) , except I applied thresholding on the grayscale image, I didnot change CV_8UC3 to CV_16UC3 anywhere, according to one of the answers (I cannot find the post), I've read that opencv writes image in 16bitrate, that's why the bump. But I'm not sure. And somewhere in my code, if I had made something that may have increased the image size, `scipy.misc.imsave()` shouldn't have reduced the size. So I think it's the problem with how opencv saves it's images. – Beeti Sushruth Sep 17 '19 at 05:09
  • Please read [mre] again. If we can’t reproduce your problem, we can’t figure out what happens or why it happens. – Cris Luengo Sep 17 '19 at 14:05
  • you will need to swap red and blue channel in order to save with scipy. e.g. `imsave('scipy_result.jpg', final_img[:,:,::-1])` – user8190410 Sep 17 '19 at 17:27
  • Thanks Guys! Swapping BGR to RGB and saving the image worked. – Beeti Sushruth Sep 18 '19 at 12:59

0 Answers0