OpenCV滤镜效果

OpenCV滤镜效果_第1张图片

在medium上看到一篇介绍opencv滤镜效果的博客,这里做一下记录和分享.

卷积操作

OpenCV滤镜效果_第2张图片

高斯模糊

pip3 install opencv-python
pip3 install scipy

def gaussianBlur(image):
    return cv2.GaussianBlur(image, (35, 35), 0)

initialImage = cv2.imread("image1.jpg")
blurredImage = gaussianBlur(copy.deepcopy(initialImage))
cv2.imwrite("blurred.jpg", blurredImage)

OpenCV滤镜效果_第3张图片

 

锐化

def sharpen(image):
    kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
    return cv2.filter2D(image, -1, kernel)

OpenCV滤镜效果_第4张图片

 

Sepia

def sepia(image):
    kernel = np.array([[0.272, 0.534, 0.131],
                       [0.349, 0.686, 0.168],
                       [0.393, 0.769, 0.189]])
    return cv2.filter2D(image, -1, kernel)

 

OpenCV滤镜效果_第5张图片

 emboss

def emboss(image):
    kernel = np.array([[0,-1,-1],
                            [1,0,-1],
                            [1,1,0]])
    return cv2.filter2D(image, -1, kernel)

 

OpenCV滤镜效果_第6张图片

像素操作

def brightnessControl(image, level):
    return cv2.convertScaleAbs(image, beta=level)

OpenCV滤镜效果_第7张图片

 

查找表操作

def spreadLookupTable(x, y):
  spline = UnivariateSpline(x, y)
  return spline(range(256))
def warmImage(image):
    increaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 80, 160, 256])
    decreaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 50, 100, 256])
    red_channel, green_channel, blue_channel = cv2.split(image)
    red_channel = cv2.LUT(red_channel, increaseLookupTable).astype(np.uint8)
    blue_channel = cv2.LUT(blue_channel, decreaseLookupTable).astype(np.uint8)
    return cv2.merge((red_channel, green_channel, blue_channel))
def coldImage(image):
    increaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 80, 160, 256])
    decreaseLookupTable = spreadLookupTable([0, 64, 128, 256], [0, 50, 100, 256])
    red_channel, green_channel, blue_channel = cv2.split(image)
    red_channel = cv2.LUT(red_channel, decreaseLookupTable).astype(np.uint8)
    blue_channel = cv2.LUT(blue_channel, increaseLookupTable).astype(np.uint8)
    return cv2.merge((red_channel, green_channel, blue_channel))

暖色调效果

OpenCV滤镜效果_第8张图片

冷色调

OpenCV滤镜效果_第9张图片

 

你可能感兴趣的:(计算机视觉知识点,opencv)