Image threshold

Mechboy·2024년 3월 24일
0

OPENCV

목록 보기
1/9

IMAGE THRESHOLD

IMAGE Threshold의 정의

  • image Threshold는 이미지 배열의 기준값을 기준으로 0,255로 분류하는 이미지 전처리 기술
  • 이미지의 Sagmentation 및 classification을 명확하게 하기 위해서 사용을 함

Global Thresold

  • 이미지의 모든 영역을 특정 픽셀 기준값 기준으로 밝기 레벨을 2단계로 분류 하는 방법

    g(x,y)={255if f(x,y)>T0if f(x,y)Tg(x, y) = \begin{cases} 255 & \text{if } f(x, y) > T \\ 0 & \text{if } f(x, y) \leq T \end{cases}
  • g(x,y)g(x,y) 는 이미지 픽셀 위치에서의 밝기 값이고 T는 기준값으로 해당값 이상이면 255, 미만이면 0으로 처리

  • OPENCV Thershold

    retval, dst = cv2.threshold(src, thresh, maxval, type[, dst])

    dst: 출력 이미지는 입력 이미지와 동일 사이즈로 출력
    src : 입력 이미지 (multiple-channel, 8-bit or 32-bit floating point).
    thresh : threshold 기준 값
    maxval : 이미지에서 Threshold type에서 처리하기 위한 최대 값
    type: threshold 처리 방식 (THRESH_BINARY, THRESH_BINARY_INV 등)

  • 코드 예시

import cv2
import numpy as np
import matplotlib.pyplot as plt 

img_highway = cv2.imread('high_way_example.jpg',cv2.IMREAD_COLOR)
img_highway_gray = cv2.cvtColor(img_highway,cv2.COLOR_BGR2GRAY)

_, img_highway_thresh = cv2.threshold(img_highway_gray,80,255,cv2.THRESH_BINARY)

plt.imshow(img_highway_thresh,cmap='gray'); plt.title('Global Threshold')

  • 위의 고속도로 이미지를 global threshold로 처리한 이미지로 나타 낼 수 있다
  • Global Threshold를 이용하면 각 부분에 대한 음영 처리에 한계가 있음

Adaptive Threshold

  • 이미지의 음영 문제를 해결 하기 위해서 도입된 방법으로 특정 이미지 영역에서만 Threshold를 하는 연산 방법
  • Adaptive 연산을 이용해서 Thershold 값을 구하면 이미지 간의 음영차를 보정하여 Threshold 연산을 할 수 있음
  • Adaptive Threshold
    dst = cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])

    출력 이미지의 크기는 입력 이미지와 동일
    src : 8bit GRAY scale 이미지.
    maxValue : 이미지에서 Threshold type에서 처리하기 위한 최대 값
    adaptiveMethod: Adaptive thresholding algorithm
    thresholdType : threshold 처리 방식 (THRESH_BINARY, THRESH_BINARY_INV 등)
    blockSize: Adaptive Threshold 를 진행할 영역 크기 (홀수로 지정 해야 함)
    C: adaptiveMethod로 계산된 Threshould 값의 가중치

_, img_highway_thresh = cv2.threshold(img_highway_gray,80,255,cv2.THRESH_BINARY)
img_highway_adp = cv2.adaptiveThreshold(img_highway_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C ,cv2.THRESH_BINARY,31,-30)

plt.figure(figsize = [20, 8])
plt.subplot(121);plt.imshow(img_highway_thresh,cmap='gray'); plt.title('Global Threshold')
plt.subplot(122);plt.imshow(img_highway_adp,cmap='gray'); plt.title('Adaptive Threshold')

profile
imageprocessing and Data science

0개의 댓글

관련 채용 정보