OpenCV Tutorials —— Making your own linear filters

kernel

A kernel is essentially a fixed size array of numerical coefficeints along with an anchor point in that array, which is tipically located at the center.

The value of the convolution is calculated in the following way:

  1. 1,Place the kernel anchor on top of a determined pixel, with the rest of the kernel overlaying the corresponding local pixels in the image.

2,Multiply the kernel coefficients by the corresponding image pixel values and sum the result.

  1. 3,Place the result to the location of the anchor in the input image.

4,Repeat the process for all pixels by scanning the kernel over the entire image.

 

Expressing the procedure above in the form of an equation we would have:

Fortunately, OpenCV provides you with the function filter2D so you do not have to code all these operations.

 

kernel_size = 3 + 2*( ind%5 );
kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);

The first line is to update the kernel_size to odd values in the range: . The second line actually builds the kernel by setting its value to a matrix filled with and normalizing it by dividing it between the number of elements.

 

filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
  •     src: Source image

  • dst: Destination image
  • ddepth: The depth of dst. A negative value (such as ) indicates that the depth is the same as the source.
  • kernel: The kernel to be scanned through the image
  • anchor: The position of the anchor relative to its kernel. The location Point(-1, -1) indicates the center by default.
  • delta: A value to be added to each pixel during the convolution. By default it is
  • BORDER_DEFAULT: We let this value by default (more details in the following tutorial)
  •  

    Code

    #include "stdafx.h"
    
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/highgui/highgui.hpp"
    #include <stdlib.h>
    #include <stdio.h>
    
    using namespace cv;
    
    /** @function main */
    int main ( int argc, char** argv )
    {
    	/// Declare variables
    	Mat src, dst;
    
    	Mat kernel;
    	Point anchor;
    	double delta;
    	int ddepth;
    	int kernel_size;
    	char* window_name = "filter2D Demo";
    
    	int c;
    
    	/// Load an image
    	src = imread( "img2.jpg" );
    
    	if( !src.data )
    	{ return -1; }
    
    	/// Create window
    	namedWindow( window_name, CV_WINDOW_AUTOSIZE );
    
    	/// Initialize arguments for the filter
    	anchor = Point( -1, -1 );
    	delta = 0;
    	ddepth = -1;
    
    	/// Loop - Will filter the image with different kernel sizes each 0.5 seconds
    	int ind = 0;
    	while( true )
    	{
    		c = waitKey(500);
    		/// Press ‘ESC‘ to exit the program
    		if( (char)c == 27 )
    		{ break; }
    
    		/// Update kernel size for a normalized box filter
    		kernel_size = 3 + 2*( ind%5 );
    		kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size);
    
    		/// Apply filter
    		filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT );
    		imshow( window_name, dst );
    		ind++;
    	}
    
    	return 0;
    }
    时间: 2024-08-02 18:14:38

    OpenCV Tutorials —— Making your own linear filters的相关文章

    学习opencv tutorials

    1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

    OpenCV Tutorials &mdash;&mdash; Changing the contrast and brightness of an image

    Brightness and contrast adjustments Two commonly used point processes are multiplication and addition with a constant: The parameters and are often called the gain and bias parameters; sometimes these parameters are said to control contrast and brigh

    OpenCV Tutorials &mdash;&mdash; Hough Line Transform

    霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

    OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

    Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

    OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

    写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

    OpenCV Tutorials &mdash;&mdash; Image Moments

    图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

    OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

    Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

    OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

    图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,

    OpenCV Tutorials &mdash;&mdash; Basic Drawing

    Point It represents a 2D point, specified by its image coordinates and . We can define it as: Point pt;pt.x = 10;pt.y = 8; or Point pt = Point(10, 8); Scalar Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel va