robert 边缘检测

来源:互联网 发布:听电子书的软件 编辑:程序博客网 时间:2024/04/28 13:02
#include "cv.h"
#include "highgui.h"
#include <math.h>
#include "stdio.h"


#pragma comment(lib, "cv.lib")  
#pragma comment(lib, "cxcore.lib")  
#pragma comment(lib, "highgui.lib")  


void robert(IplImage *src,IplImage *robert)
{
int x,y,i,w,h;
int temp,temp1;
uchar* ptr;


int ptr1[4]={0};
int indexx[4]={0,1,1,0};
int indexy[4]={0,0,1,1};


//IplImage *robert;






//gray=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
   // cvCvtColor(src,gray,CV_RGB2GRAY);
    




/*-------------Roberts算子-------------------*/
robert=cvCloneImage(src);
//robert=cvCreateImage(cvGetSize(src),src->widthStep,1);




ptr=(uchar*)robert->imageData;


    w=robert->width;
h=robert->height;
printf("%d %d",w,h);
for(y=0;y<h-1;y++)
for(x=0;x<w-1;x++)
{


for(i=0;i<4;i++)
{
ptr1[i]= *(ptr+(y+indexy[i])*robert->widthStep+x+indexx[i]);

}
temp=abs(ptr1[0]-ptr1[2]);
temp1=abs(ptr1[1]-ptr1[3]);
temp=(temp>temp1?temp:temp1);
    //temp=(int) sqrt(temp*temp+temp1*temp1);
temp=(int) sqrt(temp*temp+temp1*temp1);
if (temp>25)
temp=255;
else temp=0;  
*(ptr+y*robert->widthStep+x)=temp; 



}

cvNamedWindow("robert",1);
cvShowImage("robert",robert);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&robert);

}


void main()
{
IplImage *src,*dst;
src=cvLoadImage("11.jpg",0);
dst=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvNamedWindow("src",1);
cvShowImage("src",src);
cvSmooth(src,src,CV_GAUSSIAN,3,3,0,0);
cvSmooth(src,src,CV_MEDIAN,3,3,0,0);
cvErode(src,src,NULL,1);
cvDilate(src,src,NULL,1);
cvThreshold(src,src,100,255,CV_THRESH_BINARY_INV);
//cvAdaptiveThreshold(src,src,34,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,3,5);
//cvSmooth(src,src,CV_GAUSSIAN,3,3,0,0);
robert(src,dst);

}
0 0
原创粉丝点击