opencv 笔记14 Imgproc_Filter2D

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include 
#include 

using namespace cv;

/** @函数main */
int main ( int argc, char** argv )
{
  /// 声明变量
  Mat src, dst;

  Mat kernel;
  Point anchor;
  double delta;
  int ddepth;
  int kernel_size;
  char* window_name = "filter2D Demo";

  int c;

  /// 载入图像
  src = imread( argv[1] );

  if( !src.data )
  { return -1; }

  /// 创建窗口
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );

  /// 初始化滤波器参数
  anchor = Point( -1, -1 );
  delta = 0;
  ddepth = -1;

  /// 循环 - 每隔0.5秒,用一个不同的核来对图像进行滤波
  int ind = 0;
  while( true )
    {
      c = waitKey(500);
      /// 按'ESC'可退出程序
      if( (char)c == 27 )
        { break; }

      /// 更新归一化块滤波器的核大小
      kernel_size = 3 + 2*( ind%5 );
      kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);

      /// 使用滤波器
      filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
      imshow( window_name, dst );
      ind++;
    }

  return 0;
}
void  filter2D ( const  Mat &  src Mat &  dst , int  ddepth , const  Mat &  kernel , Point  anchor=Point(-1 , -1), double  delta=0 , int borderType=BORDER_DEFAULT )

Parameters:               
  • src – The source image
  • dst – The destination image. It will have the same size and the same number of channels as src
  • ddepth – The desired depth of the destination image. If it is negative, it will be the same as src.depth()
  • 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 – The anchor of the kernel that indicates the relative position of a filtered point within the kernel. The anchor should lie within the kernel. The special default value (-1,-1) means that the anchor is at the kernel center
  • delta – The optional value added to the filtered pixels before storing them in dst
  • borderType – The pixel extrapolation method; see borderInterpolate()


 卷积¶

高度概括地说,卷积是在每一个图像块与某个算子(核)之间进行的运算。

 核是什么?

核说白了就是一个固定大小的数值数组。该数组带有一个 锚点 ,一般位于数组中央。

kernel example

 如何用核实现卷积?

假如你想得到图像的某个特定位置的卷积值,可用下列方法计算:

  1. 将核的锚点放在该特定位置的像素上,同时,核内的其他值与该像素邻域的各像素重合;
  2. 将核内各值与相应像素值相乘,并将乘积相加;
  3. 将所得结果放到与锚点对应的像素上;
  4. 对图像所有像素重复上述过程。



你可能感兴趣的:(OpenCV)