-2

I need to use open cv functions: cv2.imencode,cv2.imdecode to compress (jpeg) and decompress (jpeg) for different QF values. The picture is 'bridge.ppm' from https://imagecompression.info/test_images/

I've tried:

bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)
bridge_de = cv2.imdecode('.jpeg', bridge_en)
cv2.imshow('image',bridge_de)

but I'm getting an error in the 2nd line saying: "Expected Ptr<cv::UMat> for argument 'buf'".

Also, how can I change and test different QF values?

Community
  • 1
  • 1
Adar Cohen
  • 158
  • 1
  • 13

1 Answers1

2

Please take a look to the documentation for imencode and imdecode

imencode returns two values, the encoded buffer is the second one. And imdecode accepts the encoded buffer and a flag. So:

bridge = cv2.imread('./bridge.ppm')
bridge_en = cv2.imencode('.jpeg', bridge)[1] # you need the second value
bridge_de = cv2.imdecode(bridge_en, cv2.IMREAD_UNCHANGED) # or any other flag, same as 'imread'
cv2.imshow('image',bridge_de)
Miki
  • 40,887
  • 13
  • 123
  • 202