RenHao

20130515_Lab_HW02

May 14th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "cv.h"
  5. #include "highgui.h"
  6.  
  7. void merge_array(int c[], int a[], int aN, int b[], int bN)
  8. {
  9.      int ai, bi, ci;
  10.      ai = bi = ci = 0;
  11.      while ( (ai<aN) && (bi<bN) )
  12.      {
  13.         if(a[ai]>b[bi])
  14.         {
  15.            c[ci] = a[ai];
  16.            ai++; ci++;
  17.         }
  18.         else
  19.         {
  20.            c[ci] = b[bi];
  21.            bi++; ci++;          
  22.         }
  23.  
  24.      }
  25.  
  26.      if(bi==bN)
  27.      {
  28.        for( ; ai<aN; ai++)
  29.            c[ci++] = a[ai];  
  30.      }
  31.      else
  32.      {
  33.        for( ; bi<bN; bi++)
  34.            c[ci++] = b[bi];  
  35.      }
  36. }
  37. void merge_sort(int a[], int n)
  38. {    
  39.      if(n<=1) return;   // if the array has been divided into one element, then just return.
  40.    
  41.      int *a0, *b0;     // else we have to divide the array into two half-size arrays.
  42.      int aN, bN;  int i;
  43.      aN = n/2;  bN = n - aN;  // Note: aN is n/2, but bN can not be n/2! Why?
  44.      a0 = a;    b0 = a+aN;    // a0 is the first half array, and b0 is the bottom half array.
  45.      // a0 = &a[0];   b0 = &a[aN];
  46.      merge_sort(a0, aN);   // sort the upper half array elements.
  47.      merge_sort(b0, bN);   // sort the lower half array elements.
  48.      // Then we merge a0 and b0 to array c, and copy back from c to a.
  49.      int c[n];  //int *c;     //c = malloc(n *sizeof(int));
  50.      merge_array(c, a0, aN, b0, bN);
  51.      for(i=0; i<n; i++) a[i] = c[i]; // copy back to array a;
  52.      // memcpy(a, c, n*sizeof(int)); // You can use memcpy()  
  53.      //free(c); // if you use malloc();
  54.      
  55. }
  56. int sort(int *ary,int n)
  57. {
  58.  int i,j,temp=0;
  59.      for(i=0;i<n-1;i++)
  60.         for(j=i;j<n;j++)
  61.             if(ary[i]>ary[j])
  62.                {temp=ary[i];ary[i]=ary[j];ary[j]=temp;}
  63.      return ary[4];
  64. }
  65.  
  66. int main(int argc, char* argv[])
  67. {
  68.     int w, h, nc;  int step;
  69.     int x, y, i;  
  70.     IplImage* img; IplImage* img2;
  71.     int Y, R, G, B;
  72.     int *ary[9]={0};
  73.      srand(time(NULL));
  74.     //if( argc == 2 && (img = cvLoadImage(argv[1], -1)) != 0 )
  75.     if( (img = cvLoadImage("step_up_rev.jpg", CV_LOAD_IMAGE_GRAYSCALE )) != 0 ) //
  76.         {
  77.         printf("Channels %d depth %d widthStep %d \n",
  78.                          img->nChannels, img->depth, img->widthStep);
  79.         printf("The image Width by Height is %d * %d \n", img->width,img->height);
  80.         printf("The image origin is  %d\n", img->origin);
  81.        
  82.         step =  img->widthStep;
  83.         w = img->width;
  84.         h = img->height;
  85.         nc = img->nChannels;
  86.        
  87.         img2 = cvCreateImage(
  88.                        cvSize(w, h), //CvSize size,
  89.                        IPL_DEPTH_8U , //int depth,
  90.                        nc ); //int channels
  91.                        
  92.         CvPoint  P;
  93.         for(i = 0 ; i < 200 ; i++)
  94.          {
  95.               P = cvPoint(rand()%w,rand()/h);
  96.                         cvLine(
  97.                                img, //CvArr* img,
  98.                                P,  //CvPoint pt1,
  99.                                P,  //CvPoint pt2,
  100.                                cvScalar(0, 0, 0, 0),  //CvScalar color,
  101.                                    1, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
  102.                         cvWaitKey(1);
  103.         }
  104.        
  105.        
  106.         printf("sss");
  107.        
  108.         cvNamedWindow( "Image 2", 1 );
  109.         cvShowImage( "Image 2", img2 );
  110.         cvNamedWindow( "Image 1", 1 );
  111.         cvShowImage( "Image 1", img );
  112.         cvWaitKey(0);
  113.        
  114.        
  115.         /*
  116.         P1 = cvPoint(100,100);                                    // 設定起點座標
  117.         P2 = cvPoint(400,400);                                    // 設定終點座標
  118.         Color=CV_RGB(0,0,255);                                    // 設定顏色 -- 藍色
  119.         cvLine(Image1,P1,P2,Color,10,CV_AA,0);                    // 畫線
  120.         Color=CV_RGB(255,0,0);                                    // 設定顏色 -- 紅色
  121.         cvLine(Image1,P1,P2,Color,1,CV_AA,0);                     // 畫線
  122.        
  123.         cvLine(
  124.        img, //CvArr* img,
  125.        cvPoint(100, 100),  //CvPoint pt1,
  126.        cvPoint(400, 500),  //CvPoint pt2,
  127.        cvScalar(0, 255, 255, 0),  //CvScalar color,
  128.            4, 8, 0); //int thickness=1, int line_type=8, int shift=0 );
  129.        
  130.        
  131.         void cvLine(IplImage img,
  132.                              CvPoint pt1,
  133.                              CvPoint pt2,
  134.                              CvScalar color,
  135.                              int thickness,
  136.                              LINE_TYPE lineType,  
  137.                              int shift)
  138.        
  139.            
  140.         */
  141.        
  142.        
  143.         uchar* ptr;
  144.         uchar* ptr2;  
  145.  
  146.         int temp;
  147.      
  148.         for (y = 1; y < h-1; y++)  // using macro
  149.         {
  150.            for (x = 1; x < w-1; x++)
  151.            {
  152.               ptr = &CV_IMAGE_ELEM(img, uchar, y, x * img->nChannels);
  153.               ptr2 = &CV_IMAGE_ELEM(img2, uchar, y, x * img2->nChannels);
  154.               //CV_IMAGE_ELEM( image, elemtype, row, col )
  155.              
  156.              
  157.              
  158.              
  159.               ary[0] =  ptr[-1-step] ;
  160.               ary[1] =  ptr[0-step]  ;
  161.               ary[2] =  ptr[1-step]  ;
  162.               ary[3] =  ptr[-1]      ;
  163.               ary[4] =  ptr[0]       ;
  164.               ary[5] =  ptr[1]       ;
  165.               ary[6] =  ptr[step-1]  ;
  166.               ary[7] =  ptr[step+0]  ;
  167.               ary[8] =  ptr[step+1]  ;
  168.  
  169.               ptr2[0]=sort(ary,9);
  170.              
  171.              
  172.              
  173.            
  174.            }    
  175.         }
  176.         cvShowImage( "Image 2", img2 );      
  177.            
  178.         cvWaitKey(0);
  179.         cvDestroyWindow( "Image 2" );
  180.         cvDestroyWindow( "Image 1" );
  181.         cvReleaseImage( &img );
  182.         cvReleaseImage( &img2 );
  183.         return 0;
  184.              
  185.              
  186.  
  187.     }
  188.     printf("Can't opne image file\n");system("PAUSE"); return -1;  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment