OpenCV imrite gives blurry results for jpeg images

I use OpenCV 3.0, and whenever I read an image and write it, the result is a blurry image.

code:

cv::Mat img = cv::imread("dir/frogImage.jpg",-1); cv::imwrite("dir/result.jpg",img); 

Does anyone know what causes this?

Original: enter image description here

Result: enter image description here

+5
source share
3 answers

You can try to increase the compression quality parameter, as shown in the OpenCV cv :: imwrite documentation :

 cv::Mat img = cv::imread("dir/frogImage.jpg",-1); std::vector<int> compression_params; compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); compression_params.push_back(100); cv::imwrite("dir/result.jpg",img, compression_params); 

Without specifying the compression quality manually, 95% quality will be applied.

but 1. you don’t know what jpeg compression quality the original image has (maybe you can increase the image size) and 2. it (afayk) still introduces additional minor artifacts, because in the end it is a loss of the compression method.

UPDATE Your problem does not seem to be due to compression artifacts, but because of the image with the Adobe RGB 1998 color format. OpenCV interprets the color values ​​as they are, but instead, it must scale the color values ​​to match the β€œreal” RGB color space. The browser and some image viewers correctly apply the color format, while others do not (for example, irfanView). I used GIMP for verification. Using GIMP, you can decide to launch how to interpret color values ​​by format, or get what you want, or your β€œwashed” image. OpenCV definitely does not care about such things, since it is not a photo editing library, therefore, neither reading nor writing, the color format will not be processed.

+2
source

This is because you save the image as a jpg. At the same time, OpenCV compresses the image. try saving it as PNG or BMP and there will be no difference.

However, IMPORTANT QUESTION : I upload the image as jpg and save it as JPG . So how is the difference ?!

Yes, this is because for JPG there are many different compression / decompression algorithms.

if you want to get acquainted with some details, see this question: Reading a jpg file in OpenCV vs C # Bitmap


EDIT: You can see what I mean here:

 auto bmp(cv::imread("c:/Testing/stack.bmp")); cv::imwrite("c:/Testing/stack_OpenCV.jpg", bmp); auto jpg_opencv(cv::imread("c:/Testing/stack_OpenCV.jpg")); auto jpg_mspaint(cv::imread("c:/Testing/stack_mspaint.jpg")); cv::imwrite("c:/Testing/stack_mspaint_opencv.jpg", jpg_mspaint); jpg_mspaint=(cv::imread("c:/Testing/stack_mspaint_opencv.jpg")); cv::Mat jpg_diff; cv::absdiff(jpg_mspaint, jpg_opencv, jpg_diff); std::cout << cv::mean(jpg_diff); 

Result: [0.576938, 0.466718, 0.495106, 0]

+2
source

As @Micha commented:

 cv::Mat img = cv::imread("dir/frogImage.jpg",-1); cv::imwrite("dir/result.bmp",img); 

I was always annoyed when mspaint.exe did the same with jpeg images. Especially for screenshots ... he destroyed them every time.

0
source

Source: https://habr.com/ru/post/1233760/


All Articles