In OpenCV it is possible to save an image to disk with a certain jpeg compression. Is there also a way to do this in memory? Or should I write a function using cv2.imsave()
that loads the file and removes it again from disk? If anyone knows a better way that is also fine.
The use case is real-time data augmentation. Using something else than OpenCV would cause possibly unnecessary overhead.
Example of desired function im = cv2.imjpgcompress(90)
This is because cv2. imwrite() has the ability to compress the image while writing it into the file.
JPEG typically achieves 10:1 compression with little perceptible loss in image quality.
JPEG compression attempts to create patterns in the color values in order to reduce the amount of data that needs to be recorded, thereby reducing the file size. In order to create these patterns, some color values are approximated to match those of nearby pixels.
imread() Loads an image from a file. The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).
You can use imencode
:
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
result, encimg = cv2.imencode('.jpg', img, encode_param)
(The default value for IMWRITE_JPEG_QUALITY
is 95.)
You can decode it back with:
decimg = cv2.imdecode(encimg, 1)
Snippet from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With