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
반응형
'데이터 분석 & 시각화 > OpenCV' 카테고리의 다른 글
[OpenCV Practice 12] 이미지 그라디언트 (Image Gradients - Edge Detection) (0) | 2020.09.13 |
---|---|
[OpenCV Practice 11] 이미지 형태학적 변환 (Morphological Transformation) (0) | 2020.09.11 |
[OpenCV Practice 10 - 1] 이미지 필터링 (Image Filtering & Blurring) (1) | 2020.09.05 |
[OpenCV Practice 09 - 3] 이미지 변형 (Perspective) (0) | 2020.08.29 |
[OpenCV Practice 09 - 2] 이미지 변형 (Rotation, Affine, Perspective) (2) | 2020.08.29 |
최근댓글