Opencv學習筆記(九):sobel &Laplace

sobel &Laplace 
Last Edit 2013/12/30
主要是簡單介紹一點sobel, Laplace 操作的一點不同。

1,sobel 
   用sobel來求圖像中某一點的梯度
1)水平方向                                            2)垂直方向
                     
在圖像中某一點的梯度:,計算過程中利用簡化形式:

void Sobel(InputArray src,  //單通道
           OutputArray dst, 
           int ddepth, //目標圖像的深度
           int xorder, //x方向求導的階數
           int yorder, //y方向求導的階數
           int ksize=3,//sobel算子的大小,必須是1,3,5,7
           double scale=1, 
           double delta=0, //在計算結果後附加的數據
           int borderType=BORDER_DEFAULT )



求絕對值函數convertScaleAbs
void convertScaleAbs(InputArray src, 
                     OutputArray dst, 
                     double alpha=1, 
                     double beta=0)

#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;
}

2,Laplace
求二階導數:

void Laplacian(InputArray src, 
               OutputArray dst, 
               int ddepth, 
               int ksize=1, 
               double scale=1, 
               double delta=0, int borderType=BORDER_DEFAULT )
同上。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章