《OpenCV:Sobel算子理论与OpenCV代码实现》

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《OpenCV:Sobel算子理论与OpenCV代码实现》相关的知识,希望对你有一定的参考价值。

索贝尔算子(Sobeloperator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值。在图像的任何一点使用此算子,将会产生对应的灰度矢量或是其法矢量

Sobel卷积因子为:

技术分享

该算子包含两组3x3的矩阵,分别为横向及纵向,将之与图像作平面卷积,即可分别得出横向及纵向的亮度差分近似值。如果以A代表原始图像,Gx及Gy分别代表经横向及纵向边缘检测的图像灰度值,其公式如下:

技术分享

具体计算如下:

图像的每一个像素的横向及纵向灰度值通过以下公式结合,来计算该点灰度的大小:

技术分享

通常,为了提高效率使用不开平方的近似值:

技术分享

 

然后可用以下公式计算梯度方向:

技术分享

若图像为: 

 技术分享

则使用近似公式的计算的结果为:

技术分享

   Sobel算子另一种形式是各向同性Sobel(Isotropic Sobel)算子,也有两个,一个是检测水平边沿的,另一个是检测垂直边沿的 。各向同性Sobel算子和普通Sobel算子相比,它的位置加权系数更为准确,在检测不同方向的边沿时梯度的幅度一致。将Sobel算子矩阵中的所有2改为根号2,就能得到各向同性Sobel的矩阵。

  由于Sobel算子是滤波算子的形式,用于提取边缘,可以利用快速卷积函数, 简单有效,因此应用广泛。美中不足的是,Sobel算子并没有将图像的主体与背景严格地区分开来,即Sobel算子没有严格地模拟人的视觉生理特征,所以提取的图像轮廓有时并不能令人满意。

 

参考:http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm

http://blog.csdn.net/tianhai110/article/details/5663756

 

除此之外:由于基础核具有关于0,0,0所在的中轴正负对称,所以通过对基础核的旋转,和图像做卷积,可以获得灰度图的边缘图,同时消去旋转角方向+180°上的边缘,迭代多个方向即可消去多个方向的边缘,但是为消去的边缘会加倍。

基础核:

-1

0

1

-2

0

2

-1

0

1

 

旋转后的核(顺时针为正)

 

45°

-2

-1

0

-1

0

1

0

1

2

 90°

-1

-2

-1

0

0

0

1

2

1

 135°

0

-1

-2

1

0

-1

2

1

0

180°

1

0

-1

2

0

-2

1

0

-1

 225°

2

1

0

1

0

-1

0

-1

-2

                  

 

 

 

  270°

1

2

1

0

0

0

-1

-2

-1

 

原图:

技术分享                

 结果图如下,按0°,45°,90°,135°,180°,225°,270°排序

 技术分享

技术分享

技术分享

 

技术分享

 

技术分享

 

技术分享

 

技术分享

 

代码如下:

#include "cv.h"
#include "cxmisc.h"
#include "highgui.h"
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <ctype.h>

#pragma comment(lib, "G:\\OpenCV-2.1.0\\vc2008\\lib\\cxcore210d.lib")
#pragma comment(lib, "G:\\OpenCV-2.1.0\\vc2008\\lib\\cv210d.lib")
#pragma comment(lib, "G:\\OpenCV-2.1.0\\vc2008\\lib\\highgui210d.lib")

//对不同深度图片和较大的图片进行放缩,以至于可以在显示器上完全显示

void ShowConvertImage(char name[200],IplImage* Image) {

    cvNamedWindow(name,1);
    char savename[350];
    sprintf(savename,"%s.jpg",name);
 
    cvSaveImage(savename,Image);
    if(Image->width<1280)
     {
  
          if(Image->depth!=IPL_DEPTH_8U)
      {   
    IplImage* NormalizeImage=NULL;
    NormalizeImage=cvCreateImage(cvGetSize(Image),IPL_DEPTH_8U,1);
    cvConvertScale(Image,NormalizeImage,1,0);//将图转为0-256,用于图片显示,
    cvShowImage(name,NormalizeImage);
    cvReleaseImage(&NormalizeImage);
  }
  else
  {
             cvShowImage(name,Image);
  }
 }
 else
 {
  IplImage* ImageResize=cvCreateImage(cvSize(1280,Image->height/(Image->width/1280)),Image->depth ,Image->nChannels);
  cvResize(Image,ImageResize,1);
     if(ImageResize->depth!=IPL_DEPTH_8U)
  {   
    IplImage* NormalizeImage=NULL;
    NormalizeImage=cvCreateImage(cvGetSize(ImageResize),IPL_DEPTH_8U,1);
    cvConvertScale(Image,NormalizeImage,1,0);//将图转为0-256,用于图片显示,
    cvShowImage(name,NormalizeImage);
    cvReleaseImage(&NormalizeImage);
  }
  else
  {
     cvShowImage(name,ImageResize);
  }
 
  cvReleaseImage(&ImageResize); 
 }
 
}
//对较大的图片缩放,不然显示器分辨率不支持,只能部分显示,具体见http://blog.csdn.net/yanmy2012/article/details/8110516
int MaxImageWidth=2650;
float Scale=1;
int MinPicWidth=640;
int MinPicHeight=428*MinPicWidth/640;
int Maxradius_self=68*MinPicWidth/640;
int Minradius_self=50*MinPicWidth/640;
int Radius_dist=20*MinPicWidth/640;
int MaxPicWidth=MinPicWidth*Scale;
int MaxPicHeight=MinPicHeight*Scale;

void main()
{

    IplImage * pictemp=NULL;
    IplImage * pic=NULL;
    char *imgpath="12.jpg"; 
    pictemp=cvLoadImage(imgpath,-1);///获取图片,原色获取
   //pictemp=cvLoadImage("IMG_02071.jpg",-1);///获取图片,原色获取
    /////////////////改变图片的像素大小
 
 
 if(pic!=NULL)
 {
  cvReleaseImage(&pic);
 }
   
 if(pictemp->width>MaxImageWidth)
 {
     pic=cvCreateImage(cvSize(MaxPicWidth,MaxPicHeight),pictemp->depth ,3);
   
     cvResize(pictemp,pic,CV_INTER_AREA );
 }
 else
 {
 
    pic=cvCloneImage(pictemp);
 
 }  
    ShowConvertImage("pic",pic);
    cvReleaseImage(&pictemp);
 
 IplImage * Gray_pic=cvCreateImage(cvGetSize(pic),pic->depth ,1);
 cvCvtColor(pic,Gray_pic, CV_BGR2GRAY );    //////将Image变成灰度图片保存在gray中
 cvCanny(Gray_pic,Gray_pic,50,150,3);
 IplImage * Result_pic=cvCreateImage(cvGetSize(pic),IPL_DEPTH_16S ,1);
 // IplImage * Result_pic=cvCreateImage(cvGetSize(pic),IPL_DEPTH_8U ,1);

 CvMat *kernel=cvCreateMat(3,3,CV_32FC1);
 ///卷积核的初始化
 ////90度模板卷积核
 {
    cvSetReal2D(kernel,0,0, 1);  cvSetReal2D(kernel,0,1, 2); cvSetReal2D(kernel,0,2, 1);
    cvSetReal2D(kernel,1,0, 0);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2, 0);
   cvSetReal2D(kernel,2,0,-1);  cvSetReal2D(kernel,2,1,-2); cvSetReal2D(kernel,2,2,-1);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果90°",Result_pic);

 ////225度模板卷积核
 {
    cvSetReal2D(kernel,0,0, 2);  cvSetReal2D(kernel,0,1, 1); cvSetReal2D(kernel,0,2, 0);
    cvSetReal2D(kernel,1,0, 1);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2,-1);
   cvSetReal2D(kernel,2,0, 0);  cvSetReal2D(kernel,2,1,-1); cvSetReal2D(kernel,2,2,-2);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果225°",Result_pic);
 ////180度模板卷积核
 {
    cvSetReal2D(kernel,0,0, 1);  cvSetReal2D(kernel,0,1, 0); cvSetReal2D(kernel,0,2,-1);
    cvSetReal2D(kernel,1,0, 2);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2,-2);
   cvSetReal2D(kernel,2,0, 1);  cvSetReal2D(kernel,2,1, 0); cvSetReal2D(kernel,2,2,-1);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果180°",Result_pic);
 ////135度模板卷积核
 {
    cvSetReal2D(kernel,0,0, 0);  cvSetReal2D(kernel,0,1,-1); cvSetReal2D(kernel,0,2,-2);
    cvSetReal2D(kernel,1,0, 1);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2,-1);
   cvSetReal2D(kernel,2,0, 2);  cvSetReal2D(kernel,2,1, 1); cvSetReal2D(kernel,2,2, 0);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果135°",Result_pic);
 //90度模板卷积核
 {
    cvSetReal2D(kernel,0,0,-1);  cvSetReal2D(kernel,0,1,-2); cvSetReal2D(kernel,0,2,-1);
    cvSetReal2D(kernel,1,0, 0);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2, 0);
   cvSetReal2D(kernel,2,0, 1);  cvSetReal2D(kernel,2,1, 2); cvSetReal2D(kernel,2,2, 1);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果90°",Result_pic);
 ////45度模板卷积核
 {
    cvSetReal2D(kernel,0,0,-2);  cvSetReal2D(kernel,0,1,-1); cvSetReal2D(kernel,0,2, 0);
    cvSetReal2D(kernel,1,0,-1);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2, 1);
   cvSetReal2D(kernel,2,0, 0);  cvSetReal2D(kernel,2,1, 1); cvSetReal2D(kernel,2,2, 2);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果45°",Result_pic);
 ////0度模板卷积核
 {
    cvSetReal2D(kernel,0,0,-1);  cvSetReal2D(kernel,0,1, 0); cvSetReal2D(kernel,0,2, 1);
    cvSetReal2D(kernel,1,0,-2);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2, 2);
   cvSetReal2D(kernel,2,0,-1);  cvSetReal2D(kernel,2,1, 0); cvSetReal2D(kernel,2,2, 1);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(1,1));
 ShowConvertImage("卷积结果0°",Result_pic);
   
 //315度模板卷积核
 {
    cvSetReal2D(kernel,0,0, 0);  cvSetReal2D(kernel,0,1, 1); cvSetReal2D(kernel,0,2, 2);
    cvSetReal2D(kernel,1,0,-1);  cvSetReal2D(kernel,1,1, 0); cvSetReal2D(kernel,1,2, 1);
   cvSetReal2D(kernel,2,0,-2);  cvSetReal2D(kernel,2,1,-1); cvSetReal2D(kernel,2,2, 0);
 }
 ////////////进行卷积核计算
 cvFilter2D(Gray_pic,Result_pic,kernel,cvPoint(-1,-1));
 ShowConvertImage("卷积结果315",Result_pic);
    
 
 cvSobel(Gray_pic,Result_pic,0,1,3);
 ShowConvertImage("Sobel结果X=0,Y=1",Result_pic);
 cvSobel(Gray_pic,Result_pic,0,2,3);
 ShowConvertImage("Sobel结果X=0,Y=2",Result_pic);
 cvSobel(Gray_pic,Result_pic,1,0,3);
 ShowConvertImage("Sobel结果X=1,Y=0",Result_pic);
 cvSobel(Gray_pic,Result_pic,1,1,3);
 ShowConvertImage("Sobel结果X=1,Y=1",Result_pic);
 cvSobel(Gray_pic,Result_pic,1,2,3);
 ShowConvertImage("Sobel结果X=1,Y=2",Result_pic);
 cvSobel(Gray_pic,Result_pic,2,0,3);
 ShowConvertImage("Sobel结果X=2,Y=0",Result_pic);
 cvSobel(Gray_pic,Result_pic,2,1,3);
 ShowConvertImage("Sobel结果X=2,Y=1",Result_pic);
 cvSobel(Gray_pic,Result_pic,2,2,3);
 ShowConvertImage("Sobel结果X=2,Y=2",Result_pic);

 cvWaitKey(0);
 cvReleaseImage(&Result_pic);
 cvReleaseImage(&Gray_pic);
 cvReleaseImage(&pic);
 cvReleaseMat(&kernel);
}    

 

以上是关于《OpenCV:Sobel算子理论与OpenCV代码实现》的主要内容,如果未能解决你的问题,请参考以下文章

Opencv 2.1 Sobel检测器通过错误

OpenCV——Sobel边缘检测

OpenCV Sobel()函数

OpenCV Sobel函数的手动实现

Opencv-图像处理理论与实例操作

OpenCV 边缘检测之Laplance算子