opencv 笔记14 Imgproc_Filter2D

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

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()
\texttt{dst} (x,y) =  \sum _{ \stackrel{0\leq x' < \texttt{kernel.cols},}{0\leq y' < \texttt{kernel.rows}} }  \texttt{kernel} (x',y')* \texttt{src} (x+x'- \texttt{anchor.x} ,y+y'- \texttt{anchor.y} )


 卷积

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

 核是什么?

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

kernel example

 如何用核实现卷积?

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

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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值