Opencv之图像滤波与图像形态变换

第一部分:Smoothing Imagess

As in one-dimensional signals, images also can be filtered with various low-pass filters(LPF), high-pass filters(HPF) etc. LPF helps in removing noises, blurring the images etc. HPF filters helps in finding edges in the images.

1、2D Convolution(Image Filtering) (2D卷积)

函数:cv2.filter2D()

说明:OpenCV provides a function cv2.filter2D() to convolve a kernel with an image.

专业化术语:

low-pass filters(LPF) 低通滤波器    主要用来消除噪声

high-pass filters(HPF) 高通滤波器   主要用来检测图像边缘

averaging filter  均值滤波器

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/opencv-logo.png')
kernel = np.ones((5,5),np.float32)/25
# keep this kernel above a pixel, add all the 25 pixels below this kernel,
# take its average and replace the central pixel with the new average value.
dst = cv2.filter2D(img,-1,kernel)  # to convolve a kernel with an image
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

2、Image Blurring(Image Smoothing)  图像模糊(图像滤波)

OpenCV provides mainly four types of blurring techniques.

A Averaging (取平均值)

函数1:cv2.blur()

函数2:cv2.boxFilter()

专业术语normalized box filter 归一化块滤波器

# -*- coding: utf-8 -*-
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('../../image/opencv-logo.png')
blur = cv2.blur(img,(5,5))  # This is done by convolving image with a normalized box filter.
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

tips:

If you don't want to use normalized box filter, use cv2.boxFilter(). Pass an argument normalize=False to the function.

B、Gaussian Blurring (高斯模糊)

函数1:cv2.GaussianBlur()

函数2:cv2.getGaussianKernel()               创建一个高斯核

特点:Gaussian blurring is highly effective in removing gaussian noise from the image.

专业术语standard deviation 标准偏差

blur = cv2.GaussianBlur(img,(5,5),0)

C、Median Blurring (中值模糊)

函数:cv2.medianBlur()

特点:It is highly effective against salt-and-pepper noise in the images.  It reduces the noise effectively. Its kernel size should be a positive odd integer.

blur = cv2.medianBlur(img,5)

D、 Bilateral Filtering (双边滤波)

函数:cv2.bilateralFilter()

特点:cv2.bilateralFilter() is highly effective in noise removal while keeping edges sharp. But the operation is slower compared to other filters.

blur = cv2.bilateralFilter(img,9,75,75)

  3、Reference Materials

资料1:A Gentle Introduction to Bilateral Filtering and its Applications

http://people.csail.mit.edu/sparis/bf_course/

资料2(GaussianBlur函数):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1

资料3(getGaussianKernel):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gac05a120c1ae92a6060dd0db190a61afa

资料4(bilateralFilter):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed

 

第二部分:Morphological Transformations

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images. It needs two inputs, one is our original image, second one is called structuring element or kernel which decides the nature of operation.

1、Erosion (削弱)

函数:cv2.erode()

       说明:It is useful for removing small white noises (as we have seen in colorspace chapter), detach two connected objects etc.

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j.png',0)
kernel = np.ones((8,8),np.uint8)
erosion = cv2.erode(img,kernel,iterations = 1)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('Erosion')
plt.xticks([]), plt.yticks([])
plt.show()

2、Dilation(扩大)

函数:cv2.dilate()

       功能说明: It increases the white region in the image or size of foreground object increases

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j.png',0)
kernel = np.ones((8,8),np.uint8)
dilation = cv2.dilate(img,kernel,iterations = 1)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dilation),plt.title('')
plt.xticks([]), plt.yticks([])
plt.show()

3、Opening

函数:cv2.morphologyEx()

       功能说明:Opening is just another name of erosion followed by dilation. It is useful in removing noise

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j2.png',0)
kernel = np.ones((12,12),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(opening),plt.title('Opening')
plt.xticks([]), plt.yticks([])
plt.show()

4、Closing

函数:cv2.morphologyEx()

       功能说明:Closing is reverse of Opening, Dilation followed by Erosion. It is useful in closing small holes inside the foreground objects, or small black points on the object.

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j3.png',0)
kernel = np.ones((8,8),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(closing),plt.title('Closing')
plt.xticks([]), plt.yticks([])
plt.show()

5、Morphological Gradient(形态学梯度算子)

函数:cv2.morphologyEx()

功能说明:It is the difference between dilation and erosion of an image.

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j.png',0)
kernel = np.ones((8,8),np.uint8)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(gradient),plt.title('Morphological Gradient')
plt.xticks([]), plt.yticks([])
plt.show()

6、Top Hat

函数: cv2.morphologyEx()

功能说明:It is the difference between input image and Opening of the image.

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j.png',0)
kernel = np.ones((6,6),np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(tophat),plt.title('Top Hat')
plt.xticks([]), plt.yticks([])
plt.show()

7、Black Hat

函数:cv2.morphologyEx()

功能说明:It is the difference between the closing of the input image and input image.

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/image_j.png',0)
kernel = np.ones((8,8),np.uint8)
blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blackhat),plt.title('Black Hat')
plt.xticks([]), plt.yticks([])
plt.show()

8、Structuring Element

函数:cv2.getStructuringElement()

       说明:in some cases, you may need elliptical/circular shaped kernels. So for this purpose, OpenCV has a function, cv2.getStructuringElement(). You just pass the shape and size of the kernel, you get the desired kernel.

>>> import cv2

>>> cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))

array([[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1]], dtype=uint8)

>>> cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))

array([[0, 0, 1, 0, 0],

[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1],

[1, 1, 1, 1, 1],

[0, 0, 1, 0, 0]], dtype=uint8)

>>> cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5))

array([[0, 0, 1, 0, 0],

[0, 0, 1, 0, 0],

[1, 1, 1, 1, 1],

[0, 0, 1, 0, 0],

[0, 0, 1, 0, 0]], dtype=uint8)

>>> cv2.getStructuringElement(cv2.MORPH_RECT,(8,8))

array([[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1],

[1, 1, 1, 1, 1, 1, 1, 1]], dtype=uint8)

>>>

9、Reference Materials

资料1:http://docs.opencv.org/3.3.0/d9/d61/tutorial_py_morphological_ops.html

资料2(getStructuringElement):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gac342a1bb6eabf6f55c803b09268e36dc

资料3(erode):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gaeb1e0c1033e3f6b891a25d0511362aeb

资料4(dilate):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#ga4ff0f3318642c4f469d0e11f242f3b6c

资料5(morphologyEx):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#ga67493776e3ad1a3df63883829375201f

思考:我测试的图片是黑底白字,怎么通过这些代码编程蓝颜色的底,红颜色的字体了??

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: