import cv2
if __name__ == '__main__':
org_img = cv2.imread("unnamed.png")
# 원본 복사해두기 혹시 모르니까
img = org_img.copy()
# # cv2 내장 split ---> 권장안함
# b, g, r = cv2.split(img)
numpy 방식으로 split
b = img[:, :, 0]
g = img[:, :, 1]
r = img[:, :, 2]
cv2.imshow("Lena_org", img)
cv2.imshow("Lena_Blue", b)
cv2.imshow("Lena_Green", g)
cv2.imshow("Lena_Red", r)
# 다시 합치기
img = cv2.merge((g,b,r))
cv2.imshow("Lena_merge", img)
- RGB
white = [255, 255, 255]
blue = [255, 0, 0]
green = [0, 255, 0]
red = [0, 0, 255]
color = [168, 200, 180]
RGB = 3 or 4 채널 --> 세로, 가로, 채널수
Grey = 1채널 --> 세로, 가로
# img.shape --> row, col, channel
# RGB (row, col, channel)
# Greyscale (row, col)
print("img :", img.shape)
cv2.imshow("Lena", img)
BGR to Greyscale
# BGR -> Greyscale
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print("img_grey :", grey.shape)
cv2.imshow("Lena_grey", grey)
Greyscale to BGR
# Greyscale -> BGR
color = cv2.cvtColor(grey, cv2.COLOR_GRAY2BGR)
print("img_greyToGBR :", color.shape)
cv2.imshow("Lena_greytobgr", color)
BGR to HSV
# BGR -> HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
print("img_hsv :", hsv.shape)
cv2.imshow("Lena_hsv", hsv)
v = hsv[:,:,2]
cv2.imshow("Lena_v", v)
cv2.waitKey(0)
cv2.destroyWindow("Lena")