1. 程式人生 > >OpenCV的影象濾波平滑操作

OpenCV的影象濾波平滑操作

濾波器


cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]]) \rightarrow dst

  • 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 followin
    – src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
    – src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
    – src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
    – src.depth() = CV_64F, ddepth = -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 (see borderInterpolate() for details).

將一個圖片與一個核做卷積

歸一化盒子濾波器


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

  • 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: k = 1 k s i z e . w i d t h k s i z e . h e i g h t [ 1 1 . . . 1 1 1 . . . 1 . . . . . . . . . 1 1 . . . 1 ] k = \frac{1}{ksize.width*ksize.height}*\begin{bmatrix} 1 &1 &... &1 \\ 1 &1 &... &1\\ &......... \\ 1 &1 &... &1 \end{bmatrix}

盒子濾波器


cv2.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]]) \rightarrow dst

  • src – input image.
  • dst – output image of the same size and type as src.
  • ddepth – the output image depth (-1 to use src.depth()).
  • ksize – blurring kernel size.
  • anchor – anchor point; default value Point(-1,-1) means that the anchor is at the kernel center.
  • normalize – flag, specifying whether the kernel is normalized by its area or not.
  • borderType – border mode used to extrapolate pixels outside of the image.
  • The function smoothes an image using the kernel: k = α [ 1 1 . . . 1 1 1 . . . 1 . . . . . . . . . 1 1 . . . 1 ] k = \alpha*\begin{bmatrix} 1 &1 &... &1 \\ 1 &1 &... &1\\ &......... \\ 1 &1 &... &1 \end{bmatrix}
    where
    α = { 1 k s i z e . w i d t h k s i z e . h e i g h t w h e n n o r m a l i z e = T r u e 1 o t h e r w i s e \alpha = \left\{\begin{matrix} \frac{1}{ksize.width*ksize.height}&&when&normalize = True\\ 1&&otherwise \end{matrix}\right.

高斯濾波器


標準的高斯核函式進行濾波。不考慮畫素是否具有相同的灰度值,也不考慮畫素是否位於邊緣,結果則趨向於模糊邊緣,有時不受歡迎的。
cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) \rightarrow dst

  • 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.
  • dst – output image of the same size and type as src.
  • ksize – Gaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. Or, they can be zero’s and then they are computed from sigma* .
  • sigmaX – Gaussian kernel standard deviation in X direction.
  • sigmaY – Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height , respectively (see getGaussianKernel() for details); to fully control the result regardless of possible future modifications of all this semantics, it is recommended to specify all of ksize, sigmaX, and sigmaY.
  • borderType – pixel extrapolation method (see borderInterpolate() for details).

*如果想要建立一個高斯核,你可以用cv2.getGaussianKernel( kszie , sigma [,ktype])函式.
在這裡插入圖片描述

中值濾波


中心元素的畫素值可能是原始影象中不存在的值,而中指濾波器則是原影象的值,有效的減小了噪聲,對於椒鹽噪聲。核大小必須是奇數值。
非線性濾波:Blurs an image using
cv2.medianBlur(src, ksize[, dst]) \rightarrow dst

  • src – input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.
  • dst – destination array of the same size and type as src.
  • ksize – aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 …

雙邊濾波


非線性濾波:是結合影象的空間鄰近度和畫素值相似度的一種折中處理,同時考慮空域資訊和灰度相似性,達到保邊去噪的目的。雙邊濾波器的好處是可以做邊緣儲存(edge preserving),一般過去用的維納濾波或者高斯濾波去降噪,都會較明顯地模糊邊緣,對於高頻細節的保護效果並不明顯。

原理:通過一個顏色差範圍和畫素距離範圍來確定權重,進而影響中心畫素的畫素值。對於空間域(spatial domain )和畫素範圍域(range domain)來分別介紹。

  • 空間域:距離越遠,權重越小。
  • 值域範圍域:畫素與中心差別越大,權重越小。弱化高斯模糊,在邊緣區域畫素值差別較大的區域。

which was defined for, and is highly effective at noise removal while preserving edges.But the operation is slower compared to other filters.

cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]]) \rightarrow dst

  • src – Source 8-bit or floating-point, 1-channel or 3-channel image.
  • dst – Destination image of the same size and type as src .
  • d – Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigmaSpace .
  • sigmaColor – Filter sigma in the color space. A larger value of the parameter means that farther colors within the pixel neighborhood (see sigmaSpace ) will be mixed together, resulting in larger areas of semi-equal color.定義多大的顏色差來起作用
  • sigmaSpace – Filter sigma in the coordinate space. A larger value of the parameter means that farther pixels will influence each other as long as their colors are close enough (see sigmaColor ). When d>0 , it specifies the neighborhood size regardless of sigmaSpace . Otherwise, d is proportional to sigmaSpace .定義多大的距離內的畫素起作用。