www.pudn.com > canny.rar > canny.cpp


 
#include  
#include  
#include  
#include  
#include  
#include  
 
int nFrmNum=0; 
IplImage *image = 0 ; //原始图像 
IplImage *image2 = 0 ; //原始图像copy 
IplImage *pFrame = 0 ; //原始图像copy 
using namespace std; 
int Thresholdness = 141; 
int ialpha = 20; 
int ibeta=20;  
int igamma=20;  
 
void onChange(int pos) 
{ 
    int i; 
    //if(image2) cvReleaseImage(&image2); 
    //if(image) cvReleaseImage(&image); 
 
    //image2 = cvLoadImage("c://fruitfs.bmp",1); //显示图片 
    //image= cvLoadImage("c://fruitfs.bmp",0); 
 
    cvThreshold(image,image,Thresholdness,255,CV_THRESH_BINARY); //分割域值     
 
    CvMemStorage* storage = cvCreateMemStorage(0); 
    CvSeq* contours = 0; 
 
    cvFindContours( image, storage, &contours, sizeof(CvContour), //寻找初始化轮廓 
        CV_RETR_EXTERNAL , CV_CHAIN_APPROX_SIMPLE ); 
 
    if(!contours) return ;  
    int length = contours->total;     
    if(length<10) return ;  
    CvPoint* point = new CvPoint[length]; //分配轮廓点 
 
    CvSeqReader reader; 
    CvPoint pt= cvPoint(0,0);;     
    CvSeq *contour2=contours;     
 
    cvStartReadSeq(contour2, &reader); 
    for (i = 0; i < length; i++) 
    { 
        CV_READ_SEQ_ELEM(pt, reader); 
        point[i]=pt; 
    } 
    cvReleaseMemStorage(&storage); 
 
    //显示轮廓曲线 
    for(i=0;iwidth, pFrame->height),  IPL_DEPTH_8U,1); 
	  image2 = cvCreateImage(cvSize(pFrame->width, pFrame->height),  IPL_DEPTH_8U,3); 
	} 
      else 
	{	//cvCvtColor(pFrame, image, CV_BGR2GRAY); 
		cvCvtColor(pFrame, image, CV_BGR2GRAY); 
		//cvCopy(pFrame, image, NULL); 
		//cvThreshold(image, image, 60, 255.0, CV_THRESH_BINARY); 
		cvCanny(image, image, 50, 150, 3); 
		image2->origin=1; 
		cvCopy(pFrame, image2,NULL); 
 
        if(cvWaitKey(40)==27) break; 
        cvShowImage("win1",image2); 
	} 
	  } 
 
	/*image=cvLoadImage("c://fruitfs.bmp",0); 
	image2 = cvCreateImage(cvSize(image->width, image->height),  IPL_DEPTH_8U,1); 
	cvCopy(image,image2,NULL); 
	cvNamedWindow("win1",1); 
	cvShowImage("win1",image2); 
	cvWaitKey(0);*/ 
    return 0; 
}