Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV imencode method channel number assertion failed

I am using OpenCV python API, the image is HWC at first(by default) and I use

img = cv2.imread(image_path) image_bytes = cv2.imencode(".jpg", img)[1]

it works well

Then I transpose the image to CHW format, using

img = img.transpose((2, 0, 1)) image_bytes = cv2.imencode(".jpg", img)[1]

And it raised error

Traceback: File "..." image_bytes = cv2.imencode(".jpg", img)[1] cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:871: error: (-215:Assertion failed) channels == 1 || channels == 3 || channels == 4 in function 'imencode'

Does image encoding support CHW? How to do it correctly?

OpenCV imencode method channel number assertion encode CHW format image failed

I am using OpenCV python API, the image is HWC HWC(Height, Width, Channel) at first(by default) and I use

img = cv2.imread(image_path) image_bytes = cv2.imencode(".jpg", img)[1]

it works wellwell, the img variable is a ndarray with [224, 224, 3] shape

Then I transpose the image to CHW CHW(Channel, Height, Width) format, using

img = img.transpose((2, 0, 1)) image_bytes = cv2.imencode(".jpg", img)[1]

And it raised error

Traceback: File "..." image_bytes = cv2.imencode(".jpg", img)[1] cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgcodecs/src/loadsave.cpp:871: error: (-215:Assertion failed) channels == 1 || channels == 3 || channels == 4 in function 'imencode'

Does image encoding support CHW? How to do it correctly?