RenHao

20130512_Lab_HW01

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