To perform the color conversion between RGB, BGR, HSV, and YCbCr color models.
Anaconda - Python 3.7
Import cv2 library and upload the image or capture an image.
Read the saved image using cv2.imread("filename.jpg").
Convert the image into the given color transformation using cv2.cvtColor(image, cv2.BGR2YCrCb) and similarly for other color formats.
Split and merge the image using cv2.split(hsv) and cv2.merge([h,s,v])
Output the image using cv2.imshow("OUTPUT", image)
import cv2
img = cv2.imread('1.png')
cv2.imshow('original',img)
bgr2hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('BGR To HSV',bgr2hsv)
bgr2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('BGR To GRAY',bgr2gray)
rgb2hsv = cv2.cvtColor(img,cv2.COLOR_RGB2HSV)
cv2.imshow('RGB2HSV',rgb2hsv)
rgb2gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('RGB2GRAY',rgb2gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("1.png")
img1= cv2.resize(img, (270,190))
#converting bgr to hsv so it can be converted into rgb and bgr
hsv = cv2.cvtColor(img1 , cv2.COLOR_BGR2HSV)
cv2.imshow("INITIAL_HSV ", hsv)
hsv_rgb = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
cv2.imshow("HSV2RGB", hsv_rgb)
hsv_bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow("HSV2BGR", hsv_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("1.png")
img1= cv2.resize(img, (270,190))
cv2.imshow("BGR_COLOR ", img1)
img_ycrcb = cv2.cvtColor(img1 , cv2.COLOR_BGR2YCrCb)
cv2.imshow("BGR2YCRCB ", img_ycrcb)
#converting rgb to bgr so it can be converted into YCrCb
img_bgr = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
cv2.imshow("RGB COLOR", img_bgr)
img_bgr_y = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2YCrCb)
cv2.imshow("RGB2YCrCb", img_bgr_y)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
img = cv2.imread("1.png")
img1= cv2.resize(img, (270,190))
#converting bgr to hsv so it can be converted into rgb and bgr
hsv = cv2.cvtColor(img1 , cv2.COLOR_BGR2HSV)
cv2.imshow("INITIAL_HSV ", hsv)
h,s,v = cv2.split(hsv)
cv2.imshow("HUE IMAGE", h)
cv2.imshow("SATURATION IMAGE", s)
cv2.imshow("GRAY IMAGE", v)
#Merge:
mergered_HSV = cv2.merge([h,s,v])
cv2.imshow("MERGED HSV IMAGE", mergered_HSV)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imshow("INITIAL_HSV ", bgr2hsv)
h,s,v = cv2.split(bgr2hsv)
cv2.imshow("RED MODEL", h)
cv2.imshow("GREEN MODEL", s)
cv2.imshow("BLUE MODEL ", v)
merger = cv2.merge([h,s,v])
cv2.imshow("MERGED IMAGE", merger)
cv2.waitKey(0)
cv2.destroyAllWindows()
Thus the color conversion was performed between RGB, HSV and YCbCr color models.