[python] 파이썬 OpenCV 4 오츠의 이진화 샘플
파이썬과 OpenCV 4를 활용한 오츠의 이진화(Otsu's Binarization) 샘플코드입니다. 원초적인 thresholding은 threshold 상수값을 사용자가 임의로 지정해야 하지만 오츠의 이진화 방법을 사용하면 자동으로 threshold 값을 자동으로 찾아줍니다. 아래는 샘플코드입니다. import cv2 as cv import numpy as np from matplotlib import pyplot as plt img = cv.imread('noisy2.png',0) # global thresholding ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY) # 오츠 이진화 ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU) # 오츠 이진화 + 가우시안 필터 blur = cv.GaussianBlur(img,(5,5),0) ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU) # plot all the images and their histograms images = [img, 0, th1, img, 0, th2, blur, 0, th3] titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)', 'Original Noisy Image','Histogram',"Otsu's Thresholding", 'Gaussian filtered Image','Histogram',"Otsu's Thresholding"] for i in range(3):...