数学之路-python计算实战(19)-机器视觉-卷积滤波

filter2D

Convolves an image with the kernel.

C++: void filter2D(InputArray src, OutputArray dst, int ddepth, InputArraykernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )
Python: cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) → dst
C: void cvFilter2D(const CvArr* src, CvArr* dst, const CvMat* kernel, CvPointanchor=cvPoint(-1,-1) )
Python: cv.Filter2D(src, dst, kernel, anchor=(-1, -1)) → None
Parameters:
  • src – input image.
  • dst – output image of the same size and the same number of channels as src.
  • ddepth –
    desired depth of the destination image; if it is negative, it will be the same as src.depth(); the following combinations ofsrc.depth() and ddepth are supported:
    • src.depth() = CV_8Uddepth = -1/CV_16S/CV_32F/CV_64F
    • src.depth() = CV_16U/CV_16Sddepth = -1/CV_32F/CV_64F
    • src.depth() = CV_32Fddepth = -1/CV_32F/CV_64F
    • src.depth() = CV_64Fddepth = -1/CV_64F

    when ddepth=-1, the output image will have the same depth as the source.

  • kernel – convolution kernel (or rather a correlation kernel), a single-channel floating point matrix; if you want to apply different kernels to different channels, split the image into separate color planes using split() and process them individually.
  • anchor – anchor of the kernel that indicates the relative position of a filtered point within the kernel; the anchor should lie within the kernel; default value (-1,-1) means that the anchor is at the kernel center.
  • delta – optional value added to the filtered pixels before storing them in dst.
  • borderType – pixel extrapolation method (seeborderInterpolate() for details).

The function applies an arbitrary linear filter to an image. In-place operation is supported. When the aperture is partially outside the image, the function interpolates outlier pixel values according to the specified border mode.

# -*- coding: utf-8 -*-
#卷积滤波
#code:[email protected]
import cv2
import numpy as np
fn="test2.jpg"
myimg=cv2.imread(fn)
img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
myh=np.array([[0,1,0],[1,-4,1],[0,1,0]])
jgimg=cv2.filter2D(img,-1,myh)
cv2.imshow(‘src‘,img)
cv2.imshow(‘dst‘,jgimg)
cv2.waitKey()
cv2.destroyAllWindows()

本博客所有内容是原创,如果转载请注明来源

http://blog.csdn.net/myhaspl/

数学之路-python计算实战(19)-机器视觉-卷积滤波,布布扣,bubuko.com

时间: 2024-08-02 06:55:31

数学之路-python计算实战(19)-机器视觉-卷积滤波的相关文章

数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )

高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到.高斯滤波的具体操作是:用一个模板(或称卷积.掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值. #滤波去噪 lbimg=cv2.GaussianBlur(newimg,(3,3),1.8) cv2.imshow('src',newimg) cv2.imshow('dst',lbimg) cv2.waitKey() cv2.destroyAllW

数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

# -*- coding: utf-8 -*- #code:[email protected] #邻域平均法滤波,半径为2 import cv2 import numpy as np fn="test3.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上椒盐噪声 param=20 #灰阶范围 w=img.shape[1] h=img.shape[0] newimg=np.array(img)

数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

拉普拉斯线性滤波,.边缘检测   Laplacian Calculates the Laplacian of an image. C++: void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT ) Python: cv2.Laplacian(src, ddepth[, dst[, k

数学之路-python计算实战(22)-机器视觉-sobel非线性滤波

sobel非线性滤波,采用梯度模的近似方式 Sobel Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator. C++: void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, intksize=3, double scale=1, double delta=0, int

数学之路-python计算实战(11)-机器视觉-图像增强

在计算机领域中,灰度(Gray scale)数字图像是每个像素只有一个采样颜色的图像.这类图像通常显示为从最暗黑色到最亮的白色的灰度,尽管理论上这个采样可以任何颜色的不同深浅,甚至可以是不同亮度上的不同颜色.灰度图像与黑白图像不同,在计算机图像领域中黑白图像只有黑白两种颜色,灰度图像在黑色与白色之间还有许多级的颜色深度.用于显示的灰度图像通常用每个采样像素8 bits的非线性尺度来保存,这样可以有256种灰度(8bits就是2的8次方=256).这种精度刚刚能够避免可见的条带失真,并且非常易于编

数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波

拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:[email protected] import cv2 import numpy as np from scipy import signal fn="test6.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) srcimg=np.array(im

数学之路-python计算实战(12)-机器视觉-图像增强

分段线性变换将图像的值域分成多个值域并进行不同线性变换计算,可以压缩某部分灰度区,扩展另一部分灰度区间,下面以2个区间为例: for m in xrange(h): for n in xrange(w): if img[m,n]>Ds_min and img[m,n]<=Ds_internal: newimg[m,n]=int((Dd_internal-Dd_min)/(Ds_internal-Ds_min)*(img[m,n]-Ds_min)+Dd_min) else: newimg[m,n

数学之路-python计算实战(7)-机器视觉-图像产生加性零均值高斯噪声

图像产生加性零均值高斯噪声,在灰度图上加上噪声,加上噪声的方式是每个点的灰度值加上一个噪声值,噪声值的产生方式为Box-Muller算法生成高斯噪声. 在计算机模拟中,经常需要生成正态分布的数值.最基本的一个方法是使用标准的正态累积分布函数的反函数.除此之外还有其他更加高效的方法,Box-Muller变换就是其中之一.另一个更加快捷的方法是ziggurat算法.下面将介绍这两种方法.一个简单可行的并且容易编程的方法是:求12个在(0,1)上均匀分布的和,然后减6(12的一半).这种方法可以用在很

数学之路-python计算实战(8)-机器视觉-图像二值化

二值化 hreshold Applies a fixed-level threshold to each array element. C++: double threshold(InputArray src, OutputArray dst, double thresh, doublemaxval, int type) Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst C: double cvThresh