Saikiran
Saikiran

Reputation: 776

How do i convert an image read with cv2.imread('img.png',cv2.IMREAD_UNCHANGED) to the format of cv2.imread('img.png',cv2.IMREAD_COLOR)

I'm trying to read an image in unchanged format, do some operations and convert it back to the colored format

im = cv2.imread(fname,cv2.IMREAD_UNCHANGED) # shape(240,240,4)
....
im2 = cv2.imread(im,cv2.IMREAD_COLOR) # required shape(240,240,3)

But, looks like I can't input the result of first numpy array into the second imread. So currently I've created a temporary image after the operations and reading that value to get the required im2 value.

im = cv2.imread(fname,cv2.IMREAD_UNCHANGED) # shape(240,240,4)
....
cv2.imwrite('img.png',im)
im2 = cv2.imread('img.png',cv2.IMREAD_COLOR) # required shape(240,240,3)

However I would like to avoid the step of creating temporary image. How would I achieve the same with a better approach

Upvotes: 0

Views: 3495

Answers (1)

wl2776
wl2776

Reputation: 4327

OpenCV has a function for color conversion cvtColor

https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html

im2 = cv2.cvtColor(im, <conversion code>)

You should figure out conversion code yourself, based on image format you have. Probably, it would be cv2.COLOR_BGRA2BGR

Upvotes: 1

Related Questions