RenHao

20130512

May 12th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "cv.h"
  5. #include "highgui.h"
  6.  
  7.  
  8. void temp_sort(int *a,int n)
  9. {
  10.  int i,j,temp;
  11.  for(i=0 ; i<n ; i++)
  12.  {
  13.          for(j=0 ; j<n-1; j++)
  14.          {
  15.           if(a[j]>a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
  16.          }
  17.  }
  18. }
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22.     int w, h, nc;  int step;
  23.     int x, y, i;  
  24.     IplImage* img; IplImage* img2;
  25.     int Y, R, G, B;
  26.     int *ary[9]={0};
  27.      
  28.     //if( argc == 2 && (img = cvLoadImage(argv[1], -1)) != 0 )
  29.     if( (img = cvLoadImage("step_up_rev.jpg", CV_LOAD_IMAGE_GRAYSCALE )) != 0 ) //
  30.     {
  31.         printf("Channels %d depth %d widthStep %d \n",
  32.                          img->nChannels, img->depth, img->widthStep);
  33.         printf("The image Width by Height is %d * %d \n", img->width,img->height);
  34.         printf("The image origin is  %d\n", img->origin);
  35.        
  36.         step =  img->widthStep;
  37.         w = img->width;
  38.         h = img->height;
  39.         nc = img->nChannels;
  40.        
  41.         img2 = cvCreateImage(
  42.                        cvSize(w, h), //CvSize size,
  43.                        IPL_DEPTH_8U , //int depth,
  44.                        nc  //int channels
  45.         );
  46.        
  47.         cvNamedWindow( "Image 2", 1 );
  48.         cvShowImage( "Image 2", img2 );
  49.         cvNamedWindow( "Image 1", 1 );
  50.         cvShowImage( "Image 1", img );
  51.         cvWaitKey(0);
  52.    
  53.  
  54.         uchar* ptr;
  55.         uchar* ptr2;  
  56.  
  57.         int temp;
  58.      
  59.         for (y = 1; y < h-1; y++)  // using macro
  60.         {
  61.            for (x = 1; x < w-1; x++)
  62.            {
  63.               ptr = &CV_IMAGE_ELEM(img, uchar, y, x * img->nChannels);
  64.               ptr2 = &CV_IMAGE_ELEM(img2, uchar, y, x * img2->nChannels);
  65.               //CV_IMAGE_ELEM( image, elemtype, row, col )
  66.              
  67.               ary[0] =  ptr[-1-step] ;
  68.               ary[1] =  ptr[0-step]  ;
  69.               ary[2] =  ptr[1-step]  ;
  70.               ary[3] =  ptr[-1]      ;
  71.               ary[4] =  ptr[0]       ;
  72.               ary[5] =  ptr[1]       ;
  73.               ary[6] =  ptr[step-1]  ;
  74.               ary[7] =  ptr[step+0]  ;
  75.               ary[8] =  ptr[step+1]  ;
  76.              
  77.               temp_sort(ary,9);
  78.               ptr[0] = ary[4] ;
  79.                      
  80.              
  81.               /*
  82.               temp =  
  83.                ptr[-1-step] + ptr[0-step] + ptr[1-step] +
  84.                ptr[-1]      + ptr[0]      + ptr[1] +
  85.                ptr[step-1]  + ptr[step+0] + ptr[step+1];
  86.               ptr2[0] = temp/9;
  87.               */
  88.            }    
  89.         }
  90.         cvShowImage( "Image 2", img2 );      
  91.            
  92.         cvWaitKey(0);
  93.         cvDestroyWindow( "Image 2" );
  94.         cvDestroyWindow( "Image 1" );
  95.         cvReleaseImage( &img );
  96.         cvReleaseImage( &img2 );
  97.         return 0;
  98.      
  99.      
  100.  
  101.         for (y = 1; y < h-1; y++)  // using pointer
  102.         {
  103.            for (x = 1; x < w-1; x++)
  104.            {
  105.               ptr = img->imageData;
  106.               ptr = ptr + y * img->widthStep + x * img->nChannels;
  107.              
  108.               //ptr2 = img2->imageData + y * img2->widthStep + x * img2->nChannels;              
  109.              
  110.               B = ptr[0]; G = ptr[1]; R = ptr[2];
  111.              
  112.               Y =   0.299 * R + 0.587*G + 0.114*B;
  113.               ptr[0] = 0;
  114.               ptr[0] = 0;
  115.               ptr[1] = y/10 * 10;
  116.               ptr[2] = 0;
  117.             }    // remove Red component
  118.              
  119.         }
  120.         cvShowImage( "Image 2", img );      
  121.         //cvSaveImage( "Gray.bmp" , img );      
  122.         cvWaitKey(0); // very important, contains event processing loop inside
  123.         cvDestroyWindow( "Image 2" );
  124.         cvReleaseImage( &img );
  125.         cvReleaseImage( &img2 );
  126.         return 0;
  127.  
  128.    uchar *ptr_1, *ptr_2;
  129.    
  130.         for(x=1; x<w/2-1; x++)
  131.         {
  132.            ptr = img->imageData;
  133.            ptr = ptr + 100 * img->widthStep +  x * img->nChannels;    
  134.            ptr[0] = 0; ptr[1] = 255; ptr[2] = 0;
  135.         }  
  136.         cvShowImage( "Image 2", img );
  137.         cvWaitKey(0);
  138.    
  139.        
  140.         ptr = img->imageData;
  141.         ptr2 = img2->imageData;
  142.         for (y = 1; y < h-1; y++)  // using macro
  143.         {
  144.            ptr_1 = ptr;
  145.            ptr_2 = ptr2;
  146.            for (x = 1; x < w-1; x++)
  147.            {
  148.              
  149.               //ptr = ptr + y * img->widthStep + x * img->nChannels;
  150.              
  151.               //ptr2 = img2->imageData + y * img2->widthStep + x * img2->nChannels;              
  152.              
  153.               B = ptr_1[0]; G = ptr_1[1]; R = ptr_1[2];
  154.               //B = *ptr_1++; G = *ptr_1++; R = *ptr_1++;
  155.               Y =   0.299 * R + 0.587 * G + 0.114 * B;
  156.               ptr_2[0] = Y;
  157.              
  158.               ptr_1 += img->nChannels;
  159.               ptr_2 += img2->nChannels;
  160.             }    // remove Red component
  161.             ptr += img->widthStep ;
  162.             ptr2 += img2->widthStep;  
  163.         }
  164.                
  165.         cvNamedWindow( "Image 1", 1 );
  166.         cvShowImage( "Image 1", img2 );      
  167.         //cvSaveImage( "Gray.bmp" , img );      
  168.         cvWaitKey(0); // very important, contains event processing loop inside
  169.         cvDestroyWindow( "Image 2" );
  170.         cvReleaseImage( &img );
  171.         cvReleaseImage( &img2 );
  172.         return 0;
  173.        
  174.     }
  175.     printf("Can't opne image file\n");system("PAUSE"); return -1;  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment