[OpenCV Practice 10 - 1] 이미지 필터링 (Image Filtering & Blurring)

OpenCV: Smoothing Images Goals Learn to: Blur images with various low pass filters Apply custom-made filters to images (2D convolution) 2D Convolution ( Image Filtering ) As in one-dimensional signa..

dsbook.tistory.com

def onChange(x):
    pass

def blurringTrackbar():
    img = cv2.imread('atom-4.png')
    cv2.namedWindow('blurring', cv2.WINDOW_NORMAL)
    cv2.createTrackbar('MODE', 'blurring', 0, 3, onChange)
    cv2.createTrackbar('size', 'blurring', 1, 10, onChange)
    cv2.imshow('blurring', img)
    
    while True:
        k = cv2.waitKey(0) & 0xFF
        if k == 27:
            break
        
        mode = cv2.getTrackbarPos('MODE', 'blurring')
        size = cv2.getTrackbarPos('size', 'blurring')
        size = size * 2 + 1
        if size == 0:
            size = 1
        
        try:
            # Averaging
            if mode == 0:
                blur = cv2.blur(img, (size, size))
                # boxfilter = cv2.boxFilter(img, -1, (size, size))

            # Gaussian Filtering
            elif mode == 1:
                # 백색 잡음 생성
                img_noise = img.copy()
                img_noise = np.clip((img / 255 + np.random.normal(scale = 0.1, size = img.shape)) * 255, 0, 255).astype('uint8')
                cv2.imshow('blurring', img_noise)
                blur = cv2.GaussianBlur(img_noise, (size, size), 0)

            # Median Filtering
            elif mode == 2:
                # 점 잡음 생성
                img_noise = img.copy()
                np.random.seed(0)
                N = 10000
                idx1 = np.random.randint(img_noise.shape[0], size = N)
                idx2 = np.random.randint(img_noise.shape[1], size = N)
                img_noise[idx1, idx2] = 0
                cv2.imshow('blurring', img_noise)
                blur = cv2.medianBlur(img_noise, size)

            # Bilateral Filtering
            elif mode == 3:
                # 백색 잡음 생성
                img_noise = img.copy()
                img_noise = np.clip((img / 255 + np.random.normal(scale = 0.1, size = img.shape)) * 255, 0, 255).astype('uint8')
                cv2.imshow('blurring', img_noise)
                blur = cv2.bilateralFilter(img_noise, size, 75, 75)

            cv2.imshow('blurring', blur)
            
        except:
            break
            
    cv2.destroyAllWindows()
blurringTrackbar()

Average Filtering

Gaussian Filtering

Median Filtering

Bilateral Filtering

 

728x90
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 라이프코리아트위터 공유하기
  • shared
  • 카카오스토리 공유하기