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

OpenCV Tutorials —— Sobel Derivatives

时间:2014-11-19 00:08:24      阅读:308      评论:0      收藏:0      [点我收藏+]

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

图像边缘 —— 像素灰度值变换剧烈的点

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.

 

  1. To be more graphical, let’s assume we have a 1D-image. An edge is shown by the “jump” in intensity in the plot below:

    bubuko.com,布布扣

 

  1. The edge “jump” can be seen more easily if we take the first derivative (actually, here appears as a maximum)

    bubuko.com,布布扣

 

Sobel Operator

  1. 1,The Sobel Operator is a discrete differentiation operator. It computes an approximation of the gradient of an image intensity function.

2,The Sobel Operator combines Gaussian smoothing and differentiation.

包括高斯滤波和差分(求导)

 

Formulation

Assuming that the image to be operated is bubuko.com,布布扣:

  1. We calculate two derivatives:

    1. Horizontal changes: This is computed by convolving bubuko.com,布布扣 with a kernel bubuko.com,布布扣 with odd size. For example for a kernel size of 3, bubuko.com,布布扣 would be computed as:

      bubuko.com,布布扣

    2. Vertical changes: This is computed by convolving bubuko.com,布布扣 with a kernel bubuko.com,布布扣 with odd size. For example for a kernel size of 3, bubuko.com,布布扣 would be computed as:

      bubuko.com,布布扣

  2. At each point of the image we calculate an approximation of the gradient in that point by combining both results above:

    bubuko.com,布布扣

    Although sometimes the following simpler equation is used:

    bubuko.com,布布扣

 

OpenCV addresses this inaccuracy for kernels of size 3 by using the Scharr function. This is as fast but more accurate than the standar Sobel function.

It implements the following kernels:

bubuko.com,布布扣

 

 

calculate the “derivatives” in x and y directions.

Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;

/// Gradient X
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
/// Gradient Y
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

The function takes the following arguments:

  • src_gray: In our example, the input image. Here it is CV_8U
  • grad_x/grad_y: The output image.
  • ddepth: The depth of the output image. We set it to CV_16S to avoid overflow.
  • x_order: The order of the derivative in x direction.
  • y_order: The order of the derivative in y direction.
  • scale, delta and BORDER_DEFAULT: We use default values.

 

 

convert our partial results back to CV_8U:

convertScaleAbs( grad_x, abs_grad_x );
convertScaleAbs( grad_y, abs_grad_y );

 

 

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 )
{

	Mat src, src_gray;
	Mat grad;
	char* window_name = "Sobel Demo - Simple Edge Detector";
	int scale = 1;
	int delta = 0;
	int ddepth = CV_16S;

	int c;

	/// Load an image
	src = imread( argv[1] );

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

	GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );

	/// Convert it to gray
	cvtColor( src, src_gray, CV_RGB2GRAY );

	/// Create window
	namedWindow( window_name, CV_WINDOW_AUTOSIZE );

	/// Generate grad_x and grad_y
	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y;

	/// Gradient X
	//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
	Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_x, abs_grad_x );

	/// Gradient Y
	//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
	Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
	convertScaleAbs( grad_y, abs_grad_y );

	/// Total Gradient (approximate)
	addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );

	imshow( window_name, grad );

	waitKey(0);

	return 0;
}

OpenCV Tutorials —— Sobel Derivatives

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

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

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