-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
[request] New parameter to imwrite JPEG to disable chroma subsampling #22052
Description
System information (version)
- OpenCV => all versions and OS's
Detailed description
cv::imwrite() has several JPEG quality-related parameters but I often miss a flag to disable chroma subsampling. This can make a significant difference in images with pure color details (e.g. with superimposed red, green or blue lines or circles where features have been found). Just setting JPEG quality to 100 does not help because standard chroma subsampling comes before and can ruin the image (chroma subsampling is good for real photographic content).
This is a normal JPEG (q 95) (uses YUV 4:2:0 subsampling). See the fuzzy borders on the red and magenta lines.

This is the same JPEG without chroma subsampling (uses RGB color space with no subsampling)

The file grows a little, but can improve significantly in some kinds of images. A PNG would be better of course, but is usually much larger and slower to compress.
Implementation
I've looked at the code of grfmt_jpeg.cpp and libjpeg and I think I've found how to do this. It would be a very little change, with a new parameter that I'd call IMWRITE_JPEG_FULL_COLOR or IMWRITE_JPEG_NO_SUBSAMPLE. Possibly just adding something like this after the line jpeg_set_defaults(&cinfo); (and something more to get the flag value from the params list)
if (IMWRITE_JPEG_NO_SUBSAMPLE && channels > 1)
jpeg_set_colorspace(&cinfo, JCS_RGB);I have not tried yet. And I'm not sure I would know how to do it and create a pull request but I'll try.
Issue submission checklist
- I report the issue, it's not a question
- I checked the problem with documentation, FAQ, open issues,
forum.opencv.org, Stack Overflow, etc and have not found any solution - I updated to the latest OpenCV version and the issue is still there
- There is reproducer code and related data files: videos, images, onnx, etc