color segmentation이란 이미지를 활용해서 색을 분류 하는 처리방법을 이야기 함
color segmentation 처리를 진행 할 때는 주로 HSV scale을 활용한다.
RGB의 경우 표현방법은 쉽지만 이미지의 특정 좌표값을 추출 하기 매우 어렵다. 똑같은 빨간색이라도 어두운 빨간색과 짙은 빨간색을 RGB스케일로 나타내기가 어려움
HSV값을 활용하면 Hue 좌표의 범위만 지정하여도 특정색의 모든 특색을 추출 할 수 있기 때문에 color segmentation에서 이점이 있음
# Read image
img = cv2.imread('fruit.jpg',cv2.IMREAD_COLOR)
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# Find Green Fruit
g_lb = np.array([28, 50, 50], np.uint8)
g_ub = np.array([80, 255, 255], np.uint8)
# Find Yellow Fruit
y_lb = np.array([20, 50, 50], np.uint8)
y_ub = np.array([32, 255, 255], np.uint8)
# Find Yellow Fruit
r_lb = np.array([0, 50, 50], np.uint8)
r_ub = np.array([22, 255, 255], np.uint8)
# Create a color mask
mask_green = cv2.inRange(img_hsv, g_lb, g_ub)
mask_yellow = cv2.inRange(img_hsv, y_lb, y_ub)
mask_red = cv2.inRange(img_hsv, r_lb, r_ub)
# Segment the fruit
img_green_fruit = cv2.bitwise_and(img,img,mask=mask_green)
img_yellow_fruit = cv2.bitwise_and(img,img,mask=mask_yellow)
img_red_fruit = cv2.bitwise_and(img,img,mask=mask_red)
# Display the original image and the segmented fruits.
plt.figure(figsize = (10, 8))
plt.subplot(221); plt.imshow(img[:, :, ::-1]); plt.title('fruits')
plt.subplot(222); plt.imshow(img_green_fruit[:, :, ::-1]); plt.title('Green fruits')
plt.subplot(223); plt.imshow(img_yellow_fruit[:, :, ::-1]); plt.title('yellow fruits')
plt.subplot(224); plt.imshow(img_red_fruit[:, :, ::-1]); plt.title('red fruits')
cv2.inRagne
dst = cv2.inRange(src, lowerb, upperb[, dst])
dst
: 출력 배열형으로 이미지가 출력, 자료형은 CV_8U type
src
: 이미지 배열lowerb
: 최소값을 가지는 원소 값upperb
: 최대값을 가지는 원소 값OpenCV Documentation
cv2.cvtColor
dst = cv2.cvtColor(src, code, dstCn=0)
dst
: 출력 배열형으로 입력이미지와 동일한 크기와 변경된 이미지 표현법으로 출력
src
: 입력 이미지 배열code
: 이미지 공간 전환 코드dstcn
: 출력 이미지의 채널 개수, 기본으로는 0으로 표현되고 자동으로 연산OpenCV Documentation