#include #include #include #include //CREATION OF SIMPLE IMAGE int prog1(){ IplImage *cvImg; // image used for visualisation CvSize imgSize; // size of visualisation image int i = 0, j = 0; imgSize.width = 640; // visualisation image is imgSize.height = 480; // 640x480 pixels // creation of a 8 bits depth gray image cvImg = cvCreateImage( imgSize, 8, 1 ); //8 bits (i.e. 256 gray levels), 1 channel (i.e. monochrome) // image is filled with gray values // corresponding to the 256 modulus // of their position in the image for ( i = 0; i < imgSize.width; i++ ) for ( j = 0; j < imgSize.height; j++ ) ((uchar*)(cvImg->imageData + cvImg->widthStep*j))[i] = ( char ) ( ( i + j ) % 256 ); cvNamedWindow( "Testing OpenCV...", 1 ); // creation of a visualisation window cvShowImage( "Testing OpenCV...", cvImg ); // image visualisation cvWaitKey( 0 ); // wait for key cvDestroyWindow( "image" ); // close window cvReleaseImage( &cvImg ); // memory release before exiting the application return( 0 ); } //SIMPLE FILTER int prog2(int argcc, char **argvv){ CvPoint center; double scale=-3; IplImage* image = cvLoadImage(argcc == 2 ? argvv[1] : "lena.jpg"); if(!image) return -1; center = cvPoint(image->width/2,image->height/2); for(int i=0;iheight;i++) for(int j=0;jwidth;j++) { double dx=(double)(j-center.x)/center.x; double dy=(double)(i-center.y)/center.y; double weight=exp((dx*dx+dy*dy)*scale); uchar* ptr = &CV_IMAGE_ELEM(image,uchar,i,j*3); ptr[0] = cvRound(ptr[0]*weight); ptr[1] = cvRound(ptr[1]*weight); ptr[2] = cvRound(ptr[2]*weight); } cvNamedWindow( "test", 1 ); cvShowImage( "test", image ); cvSaveImage("copy.png", image); cvWaitKey(); return (0); } //Testing some filters int prog3(int argcc, char **argvv){ IplImage* image = cvLoadImage(argcc == 2 ? argvv[1] : "lena.jpg"); if(!image) return -1; // IplImage *outputimage=cvCloneImage(image); CvSize imgSize; IplImage *bwimage; imgSize.width = image->width; // visualisation image is imgSize.height = image->height; // 640x480 pixels // creation of a 8 bits depth monochrome image bwimage = cvCreateImage( imgSize, 8, 1 ); //Convert the color image into a gray-scale one cvCvtColor(image, bwimage,CV_BGR2GRAY); // BW //Sobel -->>UNCOMMENT FOR RESULT //IplImage *outputimage=cvCloneImage(bwimage); //cvSobel(bwimage, outputimage, 1, 1, 3); //Laplace in sll 3 color channels -->> UNCOMMENT FOR RESULT /*IplImage *outputimage=cvCreateImage( imgSize, IPL_DEPTH_32F, 3); cvLaplace(image, outputimage, 3);*/ //Laplace in grayscale image -->> UNCOMMENT FOR RESULT //IplImage *outputimage=cvCreateImage( imgSize, IPL_DEPTH_32F, 1); //cvLaplace(bwimage, outputimage, 5); //Corner detector -->>UNCOMMENT FOR RESULT //IplImage *outputimage=cvCreateImage(imgSize, IPL_DEPTH_32F, 1); //cvPreCornerDetect( bwimage, outputimage, 7 ); //Convolution with kernel int nrows=5; int ncols=5; //CvMat* kernelmatrix=cvCreateMat( nrows, ncols, CV_8UC1 ); float a[25]; for (int i=0; i<25; ++i) a[i]=1.0/25.0; CvMat kernelmatrix; cvInitMatHeader( &kernelmatrix, nrows, ncols, CV_32FC1, a ); IplImage *outputimage=cvCreateImage(imgSize, IPL_DEPTH_8U, 1); cvFilter2D( bwimage, outputimage, &kernelmatrix); printf( "input image depth: %d\n", image->depth); printf( "bw image depth: %d\n", bwimage->depth); printf( "outputimage depth: %d\n", outputimage->depth); cvNamedWindow( "input", 1 ); cvShowImage( "input", image ); cvNamedWindow( "bw", 1 ); cvShowImage( "bw", bwimage ); cvNamedWindow( "output", 1 ); cvShowImage( "output", outputimage ); //cvSaveImage("copy.png", outputimage); cvWaitKey(); return (0); } //Canny Edge Detector from Stanford's course void prog_stanford1(){ CvvImage im; // create image variable im.Load("lena.jpg"); // load image from file system cvNamedWindow("main", 0); // create display window im.Show("main"); // show image in window CvvImage imBW; // create image variable imBW.Create(im.Width(), im.Height(), 8); // allocate cvCvtColor(im.GetImage(), imBW.GetImage(),CV_BGR2GRAY); // BW cvNamedWindow("bw", 0); // create display window imBW.Show("bw"); // show image in window CvvImage imCanny; // create image variable imCanny.Create(imBW.Width(), imBW.Height(), 8); // allocate cvCanny(imBW.GetImage(), imCanny.GetImage(), 25, 75, 3); // Canny edges cvNamedWindow("canny", 0); // create display window imCanny.Show("canny"); // show image in window imCanny.Save("canny.jpg"); // save the result for (;;) cvWaitKey(0); } int main( int argc, char** argv ) { //return prog1(); //return prog2(argc, argv); //return prog3(argc, argv); prog_stanford1(); }