원본 : https://github.com/sabin5105/openCV-tutorial
소스코드
lena.png는 아래를 참고하셔서 저장하시길 바랍니다.
""" image_blur.py: image blur with trackbars
__author__: sabin lee
__license__: MIT
__version__: 0.1
"""
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
class blurrr:
def __init__(self, path="./data/lena.png", style="gaussian") -> None:
"""
Args:
path (str, optional):
path to the image.
Defaults to "img/cat.jpeg".
style (str, optional):
style of the blur, can be "gaussian", "median", "bilateral".
Attributes:
process (function):
do gaussian blur and display the result compared to the original image
"""
original_image = cv.imread(path)
self.img = original_image
# self.img = cv.cvtColor(original_image,cv.COLOR_BGR2RGB)
self.vectorized = self.img.reshape((-1,3))
self.vectorized = np.float32(self.vectorized)
self.style = style
def process(self) -> None:
"""_summary_: do blur following style with trackbars
"""
def nothing(x):
pass
cv.namedWindow('image')
cv.createTrackbar('ksize','image',1,30,nothing)
cv.createTrackbar('sigmaX','image',0,30,nothing)
cv.createTrackbar('sigmaY','image',0,30,nothing)
while(1):
ksize = cv.getTrackbarPos('ksize','image')
sigmaX = cv.getTrackbarPos('sigmaX','image')
sigmaY = cv.getTrackbarPos('sigmaY','image')
if ksize % 2 == 0:
ksize += 1
if ksize == 1:
ksize = 3
if sigmaX == 0:
sigmaX = 1
if sigmaY == 0:
sigmaY = 1
if self.style == "gaussian":
blur = cv.GaussianBlur(self.img,(ksize,ksize),sigmaX,sigmaY)
elif self.style == "median":
blur = cv.medianBlur(self.img,ksize)
elif self.style == "bilateral":
blur = cv.bilateralFilter(self.img,ksize,sigmaX,sigmaY)
cv.imshow('image',blur)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
cv.destroyAllWindows()
if __name__ == "__main__":
blurr = blurrr(style="gaussian")
blurr.process()
결과
반응형
'MACHINE LEARNING' 카테고리의 다른 글
LDA / QDA 차이 비교 (0) | 2023.04.01 |
---|---|
통계 / Chi-Squared distribution VS Poisson distribution (멍청일기) (0) | 2023.03.23 |
openCV / K-means clustering 시각화 (1) | 2023.03.15 |
Approximate inference 정리 (0) | 2023.02.21 |
Bias-Variance tradeoff 관계 해석 (0) | 2023.02.19 |