码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Making your own linear filters

时间:2014-11-18 23:31:24      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   io   ar   color   os   sp   for   

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.

bubuko.com,布布扣

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:

bubuko.com,布布扣

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: bubuko.com,布布扣. The second line actually builds the kernel by setting its value to a matrix filled with bubuko.com,布布扣 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 bubuko.com,布布扣) 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 bubuko.com,布布扣
  • 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;
    }

    OpenCV Tutorials —— Making your own linear filters

    标签:des   style   http   io   ar   color   os   sp   for   

    原文地址:http://www.cnblogs.com/sprint1989/p/4106553.html

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!