OpenCV3-Python滤波器详细介绍
admin 于 2018年04月15日 发表在 计算机视觉

1. 不同色彩空间

OpenCV中有数百种关于不同色彩空间之间转换的方法。当前,在计算机视觉中由三种常用的色彩空间:灰度、BGR以及HSV(Hue,Saturation,Value)。

(1)灰度色彩空间是通过去除色彩信息来将其转换成灰阶,灰度色彩空间对中间处理特别有效,比如:人脸识别。

(2)BGR,即蓝-绿-红色彩空间,每一个像素点由一个三元数组来表示。

(3)HSV,H(Hue)色调,S(Saturation)饱和度,V(Value)表示黑暗的程度(或光谱另一端的明亮程度)。

2. 傅里叶变换

核概念:

指一组权重的集合,它会应用在源图像的一个区域,并由此生成目标图像的一个像素。比如,大小为7的核意味着每49(7x7)个源图像的像素会产生目标图像的一个像素。可把核看作是一块覆盖在源图像上可移动的毛玻璃片,玻璃片覆盖区域的光线会按某种方式进行扩散混合后透出来。

(1) 图像的幅度谱

把一幅图像中最明亮的像素放在图像中央,然后逐渐变暗,在边缘上的像素最暗。这样可以发现图像中由多少亮的像素和暗的像素,以及它们分布的百分比。

(2) 高通滤波器(HPF)

检测图像的某个区域,根据像素与周围像素的亮度差值来提升(boost)该像素的亮度的滤波器。

高通滤波器举例:

通过自定义卷积核实现两个高通滤波器,其中,用到SciPy模块下ndimage中的convolve()来处理多维数组的卷积运算。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  1 16:17:02 2018

@author: lu
"""

import numpy as np
import cv2
from scipy import ndimage

#sum =0 
kernel_3x3 = np.array([[-1,-1,-1],
                       [-1,8,-1],
                       [-1,-1,-1]])

kernel_5x5 = np.array([[-1,-1,-1,-1,-1],
                       [-1, 1, 2, 1,-1],
                       [-1, 2, 4, 2,-1],
                       [-1,-1,-1,-1,-1]])

img = cv2.imread("./00.png",0)

k3 = ndimage.convolve(img, kernel_3x3)
k5 = ndimage.convolve(img, kernel_5x5)

cv2.imshow("img",img)
cv2.imshow("3x3", k3)
cv2.imshow("5x5", k5)
cv2.waitKey()
cv2.destroyAllWindows()

(3) 低通滤波器

与高通滤波器相反,低通滤波器(Low Pass Filter, LPF)则是在图像与周围像素的亮度差值小于一个特定值时,平滑该像素的亮度。关于cv2.blur()函数详细介绍,查看官网

cv2.blur(src, ksize[, dst[, anchor[, borderType]]]) → dst

Parameters:

src – input image; it can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.

dst – output image of the same size and type as src.

ksize – blurring kernel size.

anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.

borderType – border mode used to extrapolate pixels outside of the image.

The function smoothes an image using the kernel:

img = cv2.imread("./00.png",0)
blurred = cv2.blur(img,(5,5))

cv2.imshow("img",img)
cv2.imshow("blurred",blurred)
cv2.waitKey()
cv2.destroyAllWindows()

(4) 高斯滤波器

高斯模糊的本质是低通滤波器,通过不同"核"将原图像的像素点与其周围的像素点进行加权和后,作为输出图像的像素点。关于cv2.GaussianBlur()函数进详细介绍,查看官网

cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) → dst

Parameters:

src – input image; the image can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.

ksize – Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. 

sigmaX – Gaussian kernel standard deviation in X direction; if both sigmas are zeros, they are computed from ksize.width and ksize.height.

img = cv2.imread("./00.png",0)
blurred = cv2.GaussianBlur(img,(5,5),0) #高斯滤波

g_hpf = img - blurred    #转为高通滤波

cv2.imshow("img",img)
cv2.imshow("gauss",blurred)  #高斯滤波
cv2.imshow("g_hpf",g_hpf)    #高通滤波
cv2.waitKey()
cv2.destroyAllWindows()

下一篇:《OpenCV3-Python边缘检测详细介绍》

注意:本站所有文章除特别说明外,均为原创,转载请务必以超链接方式并注明作者出处。 标签:opencv3,python