def _swt(self, theta, canny, sobelx, sobely, (xx,yy,w,h), searchDirection):哪里错误

1图像分割原理
1.1灰度阈值分割法
1.2基于区域的分割方法
  区域生长和分裂合并法是两种典型的串行区域技术,其分割过程后续步骤的处理要根据前面步骤的结果进行判断而确定。
基本思想是将具有相似
区域生长需要选择一组能正确代表所需区域的种子像素,确定在生长过程中的相似性准则,制定让生长停止的条件或准则。相似性准则可以是灰度级、彩色、纹理、梯度等特性。选取的种子像素可以是单个像素,也可以是包含若干个像素的小区域。大部分区域生长准则使用图像的局部性质。生长准则可根据不同原则制定,而使用不同的生长准则会影响区域生长的过程。
分裂合并法的关键是分裂合并准则的设计。这种方法对复杂图像的分割效果较好,但算法较复杂,计算量大,分裂还可能破坏区域的边界。
&1.3基于边缘的分割方法
对于灰度变化复杂和细节较丰富图象,边缘检测算子均很难完全检测出边缘,而且一旦有噪声干扰时,上述算子直接处理效果更不理想。这一方法用来分割显微图象的例子不多,因为显微图象中的许多纹理或颗粒会掩盖真正的边缘,虽然可以通过有关算法改进,但效果并不太好。
2. 图像分割的实现
void sobel (unsignedchar* des, constunsignedchar* src, int width, int height)
for (int y=0; y& y++)
for (int x=0; x& x++)
des[y * width + x]=255;
/* Now compute the convolution, scaling */
for (int y=1; y&height-1; y++)
for (int x=1; x&width-1; x++)
double n = (src[(y+1)*width+x-1]+2*src[(y+1)*width+x]+src[(y+1)*width+x+1]) -
(src[(y-1)*width+x-1]+2*src[(y-1)*width+x]+src[(y-1)*width+x+1]);
double m = (src[(y-1)*width+x+1]+2*src[y*width+x+1]+src[(y+1)*width+x+1])-
(src[(y-1)*width+x-1]+2*src[y*width+x-1]+src[(y+1)*width+x-1]);
double k = (int)( sqrt( (double)(n*n + m*m) )/4.0 );
des[y * width + x] =
thresh (des, width,height);
Roberts算子:
void roberts(unsignedchar* des, constunsignedchar* src, int width, int height)
for (int y=0; y& y++)
for (int x=0; x& x++)
des[y * width + x]=255;
/* Now compute the convolution, scaling */
for (int y=1; y&height-1; y++)
for (int x=1; x&width-1; x++)
double n = src[y*width+x] - src[(y+1)*width+x+1];
double m = src[(y+1)*width+x] - src[y*width+x+1];
double k = abs(m)+abs(n);
des[y * width + x] =
thresh (des, width,height);
Kirsch算子:
void kirsch(unsigned char* des, const unsigned char* src, int width, int height)
// TODO: Add your command handler code here
//显示数值
long int i,j,Ns;
static int nWeight[8][3][3];//对一个静态整型数组赋初值,模板
double dGrad[8];
int nTmp[3][3],xx,//每像素点的邻域值
nWeight[0][0][0] = -1 ;
nWeight[0][0][1] =
nWeight[0][0][2] =
nWeight[0][1][0] = -2 ;
nWeight[0][1][1] =
nWeight[0][1][2] =
nWeight[0][2][0] = -1 ;
nWeight[0][2][1] =
nWeight[0][2][2] =
nWeight[1][0][0] = -1 ;
nWeight[1][0][1] = -2 ;
nWeight[1][0][2] = -1 ;
nWeight[1][1][0] =
nWeight[1][1][1] =
nWeight[1][1][2] =
nWeight[1][2][0] =
nWeight[1][2][1] =
nWeight[1][2][2] =
1 ;//负号上下??? 已改成8个方向模板的值
nWeight[2][0][0] =
nWeight[2][0][1] = -1 ;
nWeight[2][0][2] = -2 ;
nWeight[2][1][0] =
nWeight[2][1][1] =
nWeight[2][1][2] = -1 ;
nWeight[2][2][0] =
nWeight[2][2][1] =
nWeight[2][2][2] =
nWeight[3][0][0] =
nWeight[3][0][1] =
nWeight[3][0][2] = -1 ;
nWeight[3][1][0] =
nWeight[3][1][1] =
nWeight[3][1][2] = -2 ;
nWeight[3][2][0] =
nWeight[3][2][1] =
nWeight[3][2][2] = -1 ;
nWeight[4][0][0] =
nWeight[4][0][1] =
nWeight[4][0][2] =
nWeight[4][1][0] =
nWeight[4][1][1] =
nWeight[4][1][2] = -1 ;
nWeight[4][2][0] =
nWeight[4][2][1] = -1 ;
nWeight[4][2][2] = -2 ;
nWeight[5][0][0] =
nWeight[5][0][1] =
nWeight[5][0][2] =
nWeight[5][1][0] =
nWeight[5][1][1] =
nWeight[5][1][2] =
nWeight[5][2][0] = -1 ;
nWeight[5][2][1] = -2 ;
nWeight[5][2][2] = -1 ;
nWeight[6][0][0] =
nWeight[6][0][1] =
nWeight[6][0][2] =
nWeight[6][1][0] = -1 ;
nWeight[6][1][1] =
nWeight[6][1][2] =
nWeight[6][2][0] = -2 ;
nWeight[6][2][1] = -1 ;
nWeight[6][2][2] =
nWeight[7][0][0] = -2 ;
nWeight[7][0][1] = -1 ;
nWeight[7][0][2] =
nWeight[7][1][0] = -1 ;
nWeight[7][1][1] =
nWeight[7][1][2] =
nWeight[7][2][0] =
nWeight[7][2][1] = -1 ;
nWeight[7][2][2] =
//注意:每行的字节数必须是4的整数倍!!!先不考虑
Ns=height*
unsigned char* kk = new unsigned char[width * height];
//开始变换
for(i=0; i& i++ )
//if(i==0)//tt change at 05.05.16
for(j=0 ; j& j++ )
des[i*width + j]=0;//*(pdGrad+y*nWidth+x)
for(i=1; i&height-1 ; i++ )
for(j=1 ; j&width-1 ; j++ )
dGrad[0] = 0 ;
dGrad[1] = 0 ;
dGrad[2] = 0 ;
dGrad[3] = 0 ;
dGrad[4] = 0 ;
dGrad[5] = 0 ;
dGrad[6] = 0 ;
dGrad[7] = 0 ;
// sobel算子需要的各点象素值
// 模板第一行
nTmp[0][0] = src[(i-1)*width + j - 1 ];
nTmp[0][1] = src[(i-1)*width + j
nTmp[0][2] = src[(i-1)*width + j + 1 ] ;
// 模板第二行
nTmp[1][0] = src[i*width + j - 1 ] ;
nTmp[1][1] = src[i*width + j
nTmp[1][2] = src[i*width + j + 1 ] ;
// 模板第三行
nTmp[2][0] = src[(i+1)*width + j - 1 ] ;
nTmp[2][1] = src[(i+1)*width + j
nTmp[2][2] = src[(i+1)*width + j + 1 ] ;
// 计算梯度
for(yy=0; yy&3; yy++)
for(xx=0; xx&3; xx++)
dGrad[0] += nTmp[yy][xx] * nWeight[0][yy][xx] ;
dGrad[1] += nTmp[yy][xx] * nWeight[1][yy][xx] ;
dGrad[2] += nTmp[yy][xx] * nWeight[2][yy][xx] ;
dGrad[3] += nTmp[yy][xx] * nWeight[3][yy][xx] ;
dGrad[4] += nTmp[yy][xx] * nWeight[4][yy][xx] ;
dGrad[5] += nTmp[yy][xx] * nWeight[5][yy][xx] ;
dGrad[6] += nTmp[yy][xx] * nWeight[6][yy][xx] ;
dGrad[7] += nTmp[yy][xx] * nWeight[7][yy][xx] ;
for (xx=1;xx&8;xx++)
if (dGrad[xx]&dGrad[0])
dGrad[0]=dGrad[xx];
des[i*width + j]=dGrad[0];// 梯度值写入src[i]
//设定阈值
int th[5120],newth[5120],shuN,newN,//winframe=32,ii,jj,
double thk,kmin,mvalue[8];
for (i=0;i&Ns;i++)//每层的每个点
if ((i&=width) && (i&(Ns-width)))//若是非边界点,则&&
if ((i%width!=0) && ((i+1)%width!=0))
//每点做变换,首先求kirs(c)h算子
mvalue[0]=fabs(double(des[i+1]+des[i+width+1]+des[i+width]+\
des[i+width-1]+des[i-1]-des[i-width-1]-\
des[i-width]-des[i-width+1]));
mvalue[1]=fabs(double(des[i+width+1]+des[i+width]+\
des[i+width-1]+des[i-1]+des[i-width-1]-\
des[i-width]-des[i-width+1]-des[i+1]));
mvalue[2]=fabs(double(des[i+width]+des[i+width-1]+des[i-1]+\
des[i-width-1]+des[i-width]-\
des[i-width+1]-des[i+1]-des[i+width+1]));
mvalue[3]=fabs(double(des[i+width-1]+des[i-1]+\
des[i-width-1]+des[i-width]+\
des[i-width+1]-des[i+1]-des[i+width+1]-\
des[i+width]));
mvalue[4]=fabs(double(des[i-1]+des[i-width-1]+\
des[i-width]+des[i-width+1]+des[i+1]-\
des[i+width+1]-des[i+width]-\
des[i+width-1]));
mvalue[5]=fabs(double(des[i-width-1]+des[i-width]+\
des[i-width+1]+des[i+1]+des[i+width+1]-\
des[i+width]-des[i+width-1]-des[i-1]));
mvalue[6]=fabs(double(des[i-width]+des[i-width+1]+des[i+1]+\
des[i+width+1]+des[i+width]-\
des[i+width-1]-des[i-1]-des[i-width-1]));
mvalue[7]=fabs(double(des[i-width+1]+des[i+1]+des[i+width+1]+\
des[i+width]+des[i+width-1]-\
des[i-1]-des[i-width-1]-des[i-width]));
for (j=1;j&8;j++)
//比较得出算子,mvalue[0]为最大
if (mvalue[0]&mvalue[j])
mvalue[0]=mvalue[j];
kk[i]=max(1,mvalue[0]/15);
if (shuN==0)
kmin=kk[i];
if (kk[i]&thk)
th[shuN]=i;
kmin=min(kmin,kk[i]);
if (shuN&=5*height)//若大于5*H个点,则重新确定
//AfxMessageBox("lll");
for (j=0;j&shuN;j++)
if (kk[th[j]]&thk)
if (newN==0)
kmin=kk[th[j]];
newth[newN]=th[j];
kmin=min(kmin,kk[th[j]]);
//else des[th[j]]=0;
for (j=0;j&5120;j++)
th[j]=newth[j];
shuN=newN;
}//重新确定完
//非边界的每点变换结束
}//一层结束
for (i=0;i&Ns;i++)//每层的每个点
if (des[i]&thk)
thresh (des, width,height);
//菜单函数结束
下面三图分别为sobel、Roberts、kerish边缘检测的结果:
之后打算用霍夫变换检测直线找矩形框,但是由于光照形成的噪点效果并不是很好,因此最后用自适应直方图均衡去除光照影响加自适应中值滤波再用投影法实现矩形框和数字的检测。具体如下:
int main()
IplImage* src = cvLoadImage("dm5.bmp");
IplImage* gray = cvCreateImage(cvGetSize(src), src-&depth, 1);
cvCvtColor(src,gray,CV_BGR2GRAY);
int width = src-&
int height = src-&
IplImage* dst = cvCreateImage(cvGetSize(src), src-&depth, gray-&nChannels);
IplImage* scr = cvCreateImage(cvGetSize(gray), gray-&depth, gray-&nChannels);
cvSmooth(gray, gray, CV_MEDIAN, 3, 0, 0, 0); //中值滤波,消除小的噪声;
cvSmooth(gray, gray, CV_GAUSSIAN, 9, gray-&nChannels);//高斯滤波
cvCvtColor(src,scr,CV_BGR2GRAY);
cvThreshold( gray, gray, 190, 255, CV_THRESH_BINARY);//二值化
int nChannels =gray-&nC
cvNamedWindow("origin",0);
cvResizeWindow("origin",int(width/2),int(height/2));
cvShowImage("origin", src);
unsigned char* img = new unsigned char[width * height ];
unsigned char* des = new unsigned char[width * height ];
unsigned char* gra = new unsigned char[width * height];
unsigned char* grt = new unsigned char[width * height];
img_data(gray, gra,width,height, nChannels);
img_data(scr, img,width,height,nChannels);
AHE(des, img, width, height,nChannels,10);//自适应直方图均衡
Projection( grt,gra,width,height);
//投影检测表盘区域
img_extract(des,grt,width,height,1);
//表盘区域还原
//kirsch(des,gra, width,height);
data_img( scr, des, width,
height, nChannels);
cvNamedWindow("表盘",0);
cvResizeWindow("表盘",int(width/2),int(height/2));
cvShowImage("表盘", scr);
cvThreshold(scr, scr, 100, 255, CV_THRESH_BINARY); //表盘区域二值化以查找数字
img_data(scr, img,width,height,nChannels);
Adaptivemedianfilter(des, img, width, height,
nChannels);
//自适应中值滤波去噪
ImageDilation( img, des, width, height,nChannels,1);
ImageErosion( des,img,width, height,nChannels,1);
//经过一次膨胀腐蚀去噪
location(img, des, width, height);
//找出数字所在区域
data_img( scr, img, width,
height, nChannels);
cvNamedWindow("数字",0);
cvResizeWindow("数字",int(width/2),int(height/2));
cvSaveImage("123.bmp",scr);
cvShowImage("数字", scr);
data_img( gray,des, width,
height, nChannels);
cvNamedWindow("erzhi",0);
cvResizeWindow("erzhi",int(width/2),int(height/2));
cvShowImage("erzhi", gray);
cvWaitKey(0);
/**************************************************************************
函数名:Projection
能:投影法找出矩形区域
入:目标图像des,
原图像 src,图像宽width, 高height
返回值:no
*************************************************************************/
void Projection(unsigned char* des,
const unsigned char* src,int width, int height)
int* h_sum = new int[height];
int* w_sum = new int[width];
int below=
int left=0;
int right=
for(int y=0;y&y++)
for(int x=0;x&x++)
des[y*width+x]=255;
for(int y=0;y&y++)
h_sum[y]=0;
for(int x=0;x&x++)
//printf("src %d",src[y*width+x]);
h_sum[y]=h_sum[y]+src[y*width+x];
//printf("%d行%d ",y,h_sum[y]);
for(int y=height/2;y&y++)
if((h_sum[y]-h_sum[height/2])&255*60)
for(int y=height/2;y&0;y--)
if((h_sum[y]-h_sum[height/2])&255*60)
for(int x=0;x&x++)
w_sum[x]=0;
for(int y=y&y++)
w_sum[x]=w_sum[x]+src[y*width+x];
//printf("%d列%d ",x,w_sum[x]);
int max_r=0;
int max_l=0;
for(int x=width/2+100;x&x++)
if(w_sum[x]&max_r)
max_r=w_sum[x];
for(int x=width/2-100;x&0;x--)
if(w_sum[x]&max_l)
max_l=w_sum[x];
for(int y=y&y++)
for(int x=x&x++)
des[y*width+x]=0;
printf("up%d below%d left%d right%d",up, below,left, right);
void img_extract(unsigned char* des,
const unsigned char* src,int width, int height, int nChannels)
for (int y=0;y&y++)
for(int x=0;x&x++)
if(src[y*width+x]!=0)
for(int n = 0; n & nC n++)
des[y * width * nChannels + x * nChannels + n ] = 255;
/************************************************************************
函数名:location
能:投影法找出数字
入:目标图像des,
原图像 src,图像宽width, 高height
返回值:no
**********************************************************************/
void location(unsigned char* des,
const unsigned char* src,int width, int height)
int* h_sum = new int[height];
int* w_sum = new int[width];
int below=
int left=0;
int right=
for(int y=0;y&y++)
for(int x=0;x&x++)
des[y*width+x]=255;
for(int y=0;y&y++)
h_sum[y]=0;
for(int x=0;x&x++)
//printf("src %d",src[y*width+x]);
h_sum[y]=h_sum[y]+src[y*width+x];
//printf("%d行%d ",y,h_sum[y]);
int h_mid=(h_sum[height/2]+h_sum[height/2-10]+h_sum[height/2-20]+h_sum[height/2-30]+h_sum[height/2-40]);
h_mid=h_mid/5;
for(int y=height/2;y&y++)
if((h_sum[y]-h_mid)&255*35)
for(int y=height/2;y&0;y--)
if((h_sum[y]-h_mid)&255*37)
for(int x=0;x&x++)
w_sum[x]=0;
for(int y=y&y++)
w_sum[x]=w_sum[x]+src[y*width+x];
//printf("%d列%d ",x,w_sum[x]);
int right_start=width-10;
for(int x=width-10;x&width/2;x--)
if(w_sum[x]!=(below-up)*255)
right_start=x;
for(int x=right_start-45;x&width/2;x--)
if(w_sum[x]&255*(below-up-40))
int left_start=10;
for(int x=10;x&x++)
if(w_sum[x]!=(below-up)*255)
left_start=x;
for(int x=left_start+100;x&x++)
if(w_sum[x]&255*(below-up-20))
for(int y=y&y++)
for(int x=left-5;x&right+5;x++)
des[y*width+x]=src[y*width+x];
printf("up%d below%d left%d right%d left_start%d h_mid%d height/2%d width%d",up, below,left, right,left_start,h_mid,height/2,width);
1.3结果展示
阅读(...) 评论()索贝尔算子(Sobeloperator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值。在图像的任何一点使用此算子,将会产生对应的灰度矢量或是其法矢量
Sobel卷积因子为:
该算子包含两组3x3的矩阵,分别为横向及纵向,将之与图像作平面卷积,即可分别得出横向及纵向的亮度差分近似值。如果以A代表原始图像,Gx及Gy分别代表经横向及纵向边缘检测的图像灰度值,其公式如下:
具体计算如下:
图像的每一个像素的横向及纵向灰度值通过以下公式结合,来计算该点灰度的大小:
通常,为了提高效率使用不开平方的近似值:
然后可用以下公式计算梯度方向:
若图像为:
则使用近似公式的计算的结果为:
Sobel算子另一种形式是各向同性Sobel(Isotropic Sobel)算子,也有两个,一个是检测水平边沿的,另一个是检测垂直边沿的 。各向同性Sobel算子和普通Sobel算子相比,它的位置加权系数更为准确,在检测不同方向的边沿时梯度的幅度一致。将Sobel算子矩阵中的所有2改为根号2,就能得到各向同性Sobel的矩阵。
  由于Sobel算子是滤波算子的形式,用于提取边缘,可以利用快速卷积函数, 简单有效,因此应用广泛。美中不足的是,Sobel算子并没有将图像的主体与背景严格地区分开来,即Sobel算子没有严格地模拟人的视觉生理特征,所以提取的图像轮廓有时并不能令人满意。
除此之外:由于基础核具有关于0,0,0所在的中轴正负对称,所以通过对基础核的旋转,和图像做卷积,可以获得灰度图的边缘图,同时消去旋转角方向+180°上的边缘,迭代多个方向即可消去多个方向的边缘,但是为消去的边缘会加倍。
旋转后的核(顺时针为正)
结果图如下,按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);
cvShowImage(name,Image);
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);
cvShowImage(name,ImageResize);
cvReleaseImage(&ImageResize);
//对较大的图片缩放,不然显示器分辨率不支持,只能部分显示,具体见
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*S
int MaxPicHeight=MinPicHeight*S
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 );
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 for python (15) 图像梯度(Sobel算子、scharr算子与laplacian算子原理及卷积模板)
Sobel算子、scharr算子与laplacian算子
Sobel算子是一阶导数的边缘检测算子,在算法实现过程中,通过3×3模板作为核与图像中的每个像素点做卷积和运算,然后选取合适的阈值以提取边缘...
Sobel算子实现水平边缘检测、垂直边缘检测;45度、135度角边缘检测
%File Discription:
%45°和135°角边缘检测;用于那些边界不明显的图片
%不太适用于复杂图,复杂图用水平和垂直边缘检测
%Author:Zhang Ruiqin...
[图像处理]sobel算子边缘检测算法
我的程序效果:边缘检测算法是图像处理中最为基本的问题。其目的是标志图像出亮度变化明显的点,从而反映出图像中重要变化。先介绍一下Sobel算子:Sobel 算子是像素图像边缘检测中最重要的算子之一,该算...
【图像处理】sobel算子简述
sobel算子是什么在图像处理上,算子一般也可以认为是滤波器filter,滤波器就是在一个像素点上对它进行与邻域之间的运算。如中值滤波,就是在以像素点为中心,上下左右左上左下右上右下为邻域的集合里,如...
OpenCV Sobel算子水平和垂直方向导数问题
sobel算子是一种常用的边缘检测算法,在各种论文或书籍中,我们常常能看到类似这样的话,被检测的对象存在大量的竖直边,所以可以采用sobel算子来找到第一个水平导数,它可以用来在图像中查找竖直边缘。 ...
充分理解数字图像处理中的卷积原理
%用Sobel算子和拉普拉斯对图像锐化:
I=imread('e:\role0\003i.bmp');
subplot(2,2,1),imshow(I); ...
大多数人的生活其实并不如意,与之相同的是大多数人都是一颗得过且过的心,所以说成功的道路并不拥挤,因为努力的人太少了,我活了二十多年没见过几个……我从来都是一个追求上进的人,但最最近一段时期,我的时间利...
Sobel算子原理及OpenCv实现
索贝尔算子(Sobeloperator)主要用于获得数字图像的一阶梯度,常见的应用和物理意义是边缘检测。
在技术上,它是一个离散的一阶差分算子,用来计算图像亮度函数的一阶梯度之近似值,用来运算图...
没有更多推荐了,

我要回帖

更多关于 swtc 的文章

 

随机推荐