RenHao

20130523_Lab_HW02_EdgeDetect

May 22nd, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #include "cv.h"
  6. #include "highgui.h"
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.   IplImage *img1; IplImage *img2;
  11.   int w,h,nc,step;
  12.  
  13.  
  14.  
  15.   if((img1 = cvLoadImage("step_up_rev.jpg", CV_LOAD_IMAGE_GRAYSCALE ))== 0) //CV_LOAD_IMAGE_GRAYSCALE ==> gray
  16.          { printf("Can't opne image file\n");system("PAUSE"); return -1; }
  17.  
  18.  
  19.    printf("Channels %d depth %d widthStep %d \n",
  20.                          img1->nChannels, img1->depth, img1->widthStep);
  21.    printf("The image Width by Height is %d * %d \n", img1->width,img1->height);
  22.    printf("The image origin is  %d\n", img1->origin);
  23.  
  24.   w = img1 -> width ;
  25.   h = img1 -> height ;
  26.   nc = img1 -> nChannels ;
  27.   step = img1 -> widthStep ;
  28.  
  29.   img2 = cvCreateImage(
  30.                        cvSize(w, h), //CvSize size,
  31.                        IPL_DEPTH_8U , //int depth,
  32.                        nc ); //int channel
  33.  
  34.  
  35.   cvNamedWindow( "Image 2", 1 );
  36.   cvShowImage( "Image 2", img2 );
  37.   cvNamedWindow( "Image 1", 1 );
  38.   cvShowImage( "Image 1", img1 );
  39.   cvWaitKey(0);
  40.  
  41.   uchar* ptr ;
  42.   uchar* ptr2;
  43.   int s=0,sum1 ,sum2;
  44.   int x,y;
  45.  
  46.   for(x = 1 ; x < w-1 ; x ++)
  47.    for(y = 1 ; y < h-1 ; y ++)
  48.    {
  49.     ptr = &CV_IMAGE_ELEM(img1, uchar, y, x * img1->nChannels);
  50.     ptr2 = &CV_IMAGE_ELEM(img2, uchar, y, x * img2->nChannels);    
  51.    
  52.     sum1 =
  53.         (-1)*ptr[-1-step]  +  0*ptr[-step]  +  ptr[1-step]  +
  54.         (-1)*ptr[-1]       +  0*ptr[0]      +  ptr[1]       +
  55.         (-1)*ptr[-1+step]  +  0*ptr[step]   +  ptr[step+1]  ;
  56.     sum2=
  57.         (-1)*ptr[-1-step]  +  (-1)*ptr[-step]  +  (-1)*ptr[1-step]  +
  58.         0*ptr[-1]          +  0*ptr[0]         +  0*ptr[1]          +
  59.         ptr[-1+step]       +  ptr[step]      +  ptr[step+1]  ;    
  60.        
  61.      
  62.     if(sum1>255) sum1 = 255 ;
  63.     else if(sum1<0) sum1 = -sum1 ;    
  64.    
  65.     if(sum2>255) sum2 = 255 ;
  66.     else if(sum2<0) sum2 = -sum2 ;
  67.      
  68.     sum1 = sum1 ;
  69.     sum2 = sum2 ;
  70.    
  71.     s = sum1+sum2 ;
  72.     ptr2[0] = s ;
  73.    
  74.          
  75.    }
  76.  
  77.  
  78.         cvShowImage( "Image 2", img2 );      
  79.            
  80.         cvWaitKey(0);
  81.         cvDestroyWindow( "Image 2" );
  82.         cvDestroyWindow( "Image 1" );
  83.         cvReleaseImage( &img1 );
  84.         cvReleaseImage( &img2 );
  85.         return 0;
  86.    
  87.    
  88.  
  89.   system("PAUSE"); 
  90.   return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment