Ask Your Question

OpenCV encode CHW format image failed

asked 2020-10-11 21:15:43 -0600

Litchy gravatar image

updated 2020-10-12 02:22:48 -0600

I am using OpenCV python API, the image is 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 well, the img variable is a ndarray with [224, 224, 3] shape

Then I transpose the image to 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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-10-12 02:41:36 -0600

berak gravatar image

Does image encoding support CHW?


How to do it correctly?

either keep it HWC or split it into single channels and save those seperately

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2020-10-11 20:36:02 -0600

Seen: 963 times

Last updated: Oct 12 '20