OpenCV encode CHW format image failed
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?