cv2.imencode / cv2.imdecode output issue

asked 2018-03-13 08:01:55 -0500

Meded gravatar image

Can someone advise why is there difference between input and output array after it gets encoded / decoded?.

Below is my code snippet. When I use both arrays to display image, there is no visible difference between two images. What bothers me is that I don't know where the difference is coming from.

Thanks.

Blockquote

import cv2
import socket
import pickle
import numpy as np
import time
import sys

# Capture picture
cap = cv2.VideoCapture('sample.jpg')
ret, frame = cap.read()

#print frame as is
print (frame)

#encode data ready for sending
pack_data = cv2.imencode('.jpeg', frame)[1].tobytes()

# just a simple output separator 
print('--------------------------------------------')

#deode encoded data back into array
i = cv2.imdecode(np.fromstring(pack_data, dtype=np.uint8),cv2.IMREAD_COLOR)

#print array after it is decoded
print (i)


----------


BEFORE ENCODING
[[[237 244 243]
  [249 255 255]
  [255 246 250]
  ..., 
  [242 242 242]
  [242 242 242]
  [242 242 242]]

 [[249 255 255]
  [249 255 255]
  [255 244 248]
  ..., 
  [242 242 242]
  [242 242 242]
  [242 242 242

AFTER ENCODING

[[[223 245 243]
  [255 253 255]
  [255 243 248]
  ..., 
  [242 242 242]
  [242 242 242]
  [242 242 242]]

 [[235 255 255]
  [255 252 255]
  [255 242 247]
  ..., 
  [242 242 242]
  [242 242 242]
  [242 242 242]]
edit retag flag offensive close merge delete

Comments

jpeg is a lossy format, you'll never get back, what you put in. (nothing special about opencv here)

try with png, if you need reproducable results.

berak gravatar imageberak ( 2018-03-13 08:13:52 -0500 )edit

THANK YOU!!!! It worked!

Meded gravatar imageMeded ( 2018-03-13 10:25:57 -0500 )edit