color Segmentation

Mechboy·2024년 4월 23일
0

OPENCV

목록 보기
3/9

color segmentation

color 표현 방식

  • color segmentation이란 이미지를 활용해서 색을 분류 하는 처리방법을 이야기 함

  • color segmentation 처리를 진행 할 때는 주로 HSV scale을 활용한다.

  • RGB의 경우 표현방법은 쉽지만 이미지의 특정 좌표값을 추출 하기 매우 어렵다. 똑같은 빨간색이라도 어두운 빨간색과 짙은 빨간색을 RGB스케일로 나타내기가 어려움

    이미지 출처

  • HSV값을 활용하면 Hue 좌표의 범위만 지정하여도 특정색의 모든 특색을 추출 할 수 있기 때문에 color segmentation에서 이점이 있음

example

  • color segmentation을 활용하여 여러색깔의 과일을 분류 하는 코드 예시

source code

# 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')

이미지 출처

실행결과

  • 이미지를 보면 초록색,노란색,빨간색 계통의 과일로 분류하여 이미지 표시
  • 하지만 과일 이미지는 똑같은 원색이더라도 완전한 빨강,초록,노랑의 좁은 색범위를 가지지 않기 때문에 과일의 전체적인 윤곽의 확인이 어렵다.

opencv class 설명

cv2.inRagne

dst = cv2.inRange(src, lowerb, upperb[, dst]) 

dst: 출력 배열형으로 이미지가 출력, 자료형은 CV_8U type

  1. src: 이미지 배열
  2. lowerb: 최소값을 가지는 원소 값
  3. upperb: 최대값을 가지는 원소 값

OpenCV Documentation

inRange()

cv2.cvtColor

dst = cv2.cvtColor(src, code, dstCn=0) 

dst: 출력 배열형으로 입력이미지와 동일한 크기와 변경된 이미지 표현법으로 출력

  1. src: 입력 이미지 배열
  2. code: 이미지 공간 전환 코드
  3. dstcn: 출력 이미지의 채널 개수, 기본으로는 0으로 표현되고 자동으로 연산

OpenCV Documentation

cvtColor()

profile
imageprocessing and Data science

0개의 댓글

관련 채용 정보