Color gets dull: opencv cv2.imread cv2.imwrite

浪子不回头ぞ 提交于 2021-02-08 13:15:35

问题


I am using opencv module to read and write the image. here is the code and below is the image i am reading and second image is after saving it on disk using cv2.imwrite().

import cv2

img = cv2.imread('originalImage.jpg')
cv2.imwrite('test.jpg',img)

It is significantly visible that colors are dull in second image. Is there any workaround to this problem or I am missing on some sort of setting parameters..?


回答1:


The difference is that the initial image (on the left in the diagram) has an attached ICC profile whereas the second one (on the right) does not.

I obtained the above image by running the ImageMagick utility called identify like this:

identify -verbose first.jpg    > 1.txt
identify -verbose second.jpg   > 2.txt

Then I ran the brilliant opendiff tool (which is part of macOS) like this:

opendiff [12].txt

You can extract the ICC profile from the first image also with ImageMagick like this:

convert first.jpg profile.icc



回答2:


I have done a bit of research on the point @mark raised about ICC profile. I have figured out a way to handle this in python PIL module. here is the code that worked for me. I have also learned to use PNG file format rather JPEG to do lossless conversion.

import Image
img = Image.open('originalImage.jpg')
img.save('test.jpg',icc_profile=img.info.get('icc_profile'))

I hope this will help others as well.




回答3:


Your first input image has some icc-Profile associated in the meta-data, which is an optional attribute and most devices may not inject it in the first place. The ICC profile basically performs a sort of color correction, and the correction coefficients are calculated for each unique device during calibration.

Modern Web Browsers, Image Viewing utilities mainly take into account this ICC profile information before rendering the image onto the screen, that is the reason why there is a diff in both the images.

But Unfortunately OpenCV doesn't reads the ICC config from the meta data of the image to perform any color correction.



来源:https://stackoverflow.com/questions/44562776/color-gets-dull-opencv-cv2-imread-cv2-imwrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!