alyssaeliyah
alyssaeliyah

Reputation: 2244

Convert ColorSpaces using OpenCv in Python

How do I convert an image within BGRA color space to HSV and BGRA to YCbCR? I only know convertion from BGR and not BGRA. Is the approach different or the same?

hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
ycrcb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2YCrCb)

Your help will be very much appreciated.

Upvotes: 1

Views: 3481

Answers (1)

Dan Mašek
Dan Mašek

Reputation: 19041

Neither HSV, nor YCbCr include a transparency plane, so the only thing the conversion could do with it is ignore it. As it turns out (after little digging through the implementation), cvtColor accepts both 3 and 4 channel input images for those conversion method, and correctly ignores the 4th (transparency) if present.

Little script to verify that:

import cv2
a = cv2.imread('car_1.png')
b = cv2.cvtColor(a, cv2.COLOR_BGR2BGRA)
c = b.copy()
c[:,:,3] = 0

aa = cv2.cvtColor(a, cv2.COLOR_BGR2HSV)
bb = cv2.cvtColor(b, cv2.COLOR_BGR2HSV)
cc = cv2.cvtColor(c, cv2.COLOR_BGR2HSV)

print (aa == bb).all()
print (aa == cc).all()

Prints True and True.

Same thing for BGR -> YCrCb

Upvotes: 3

Related Questions