RenHao

multumedia_hk1

Apr 8th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "opencv/cv.h"
  5. #include "opencv/highgui.h"
  6. #include "opencv/cxcore.h"
  7.  
  8. #define scale 1
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12.     int w, h, nc;  int step;
  13.     int x, y, i;
  14.    
  15.     IplImage* img = cvLoadImage("/Users/ranhao/Desktop/introduce to multimedia/hk1/hk1/hk1/7213_Kobe.jpg", CV_LOAD_IMAGE_UNCHANGED );
  16.     //IplImage* img2 = cvLoadImage("/Users/ranhao/Desktop/introduce to multimedia/hk1/kb.jpg", CV_LOAD_IMAGE_UNCHANGED );
  17.     /*
  18.      #define CV_LOAD_IMAGE_UNCHANGED -1         原圖影像
  19.      #define CV_LOAD_IMAGE_GRAYSCALE 0             灰階
  20.      #define CV_LOAD_IMAGE_COLOR 1                 彩色
  21.      #define CV_LOAD_IMAGE_ANYDEPTH 2           任何彩度
  22.      #define CV_LOAD_IMAGE_ANYCOLOR 4           任何彩色
  23.     */
  24.     IplImage* dst;
  25.     IplImage* img3;
  26.     int Y, R, G, B;
  27.     int *ary[9]={0};
  28.    
  29.     double alpha ;
  30.     double beta ;
  31.    
  32.    
  33.     //if( argc == 2 && (img = cvLoadImage(argv[1], -1)) != 0 )
  34.     if( img != 0 ) //
  35.     {
  36.         printf("Channels %d depth %d widthStep %d \n",
  37.                img->nChannels, img->depth, img->widthStep);
  38.         printf("The image Width by Height is %d * %d \n", img->width,img->height);
  39.         printf("The image origin is  %d\n", img->origin);
  40.        
  41.         step =  img->widthStep;
  42.         w = img->width;
  43.         h = img->height;
  44.         nc = img->nChannels;
  45.        
  46.         dst = cvCreateImage(
  47.                              cvSize(w, h), //CvSize size,
  48.                              IPL_DEPTH_8U , //int depth,
  49.                              nc  //int channels
  50.                              );
  51.         img3 = cvCreateImage(
  52.                             cvSize(w, h), //CvSize size,
  53.                             IPL_DEPTH_8U , //int depth,
  54.                             nc  //int channels
  55.                             );
  56.        
  57.         //Open first image
  58.         cvNamedWindow( "Image 1", 1 );
  59.         cvMoveWindow("Image 1", 0,  0);
  60.         cvShowImage( "Image 1", img );
  61.         cvWaitKey(0); //0:wait for entering any keys ; number:unit is ms;
  62.        
  63.         //Open second image
  64.         /*
  65.          cvNamedWindow( "Image 2", 1 );
  66.         cvMoveWindow("Image 2", 630,  0); //Move to other point on screen
  67.         cvShowImage( "Image 2", img2 );
  68.         cvWaitKey(0);
  69.          */
  70.        
  71.         uchar* ptr;
  72.         uchar* ptr2;
  73.        
  74.         int temp;
  75.        
  76.        
  77.        
  78.         for (y = 1; y < h-1; y++)  // using macro
  79.         {
  80.             for (x = 1; x < w-1; x++)
  81.             {
  82.                 ptr = &CV_IMAGE_ELEM(img, uchar, y, x *3);
  83.                 ptr2 = &CV_IMAGE_ELEM(img3, uchar, y, x *3);
  84.                 //CV_IMAGE_ELEM( image, elemtype, row, col )
  85.                 //Iplimage  BGR
  86.                 temp =
  87.                 0*ptr[-2-2*step] +  ptr[-1-2*step]  + 2*ptr[-2*step] + ptr[1-2*step]  + 0*ptr[2-2*step] +
  88.                 ptr[-2-step]    + 3*ptr[-1-step]   + 5*ptr[0-step]  + 3*ptr[1-step]  +   ptr[2-step]   +
  89.                 2*ptr[-2]        + 5*ptr[-1]        + 9*ptr[0]       + 5*ptr[1]       +  2*ptr[2]       +
  90.                 ptr[step-2]     + 3*ptr[step-1]    + 5*ptr[step+0]  + 3*ptr[step+1]  +   ptr[step+2]   +
  91.                 0*ptr[2*step-2]  +  ptr[-1+2*step]  + 2*ptr[2*step]  + ptr[1+2*step]  + 0*ptr[2+2*step]  ;
  92.                 //  5*5 Filter
  93.                 /*
  94.                  0 1 2 1 0
  95.                  1 3 5 3 1
  96.                  2 5 9 5 2
  97.                  1 3 5 3 1
  98.                  0 1 2 1 0
  99.                  */
  100.                 ptr2[0] = temp/25;
  101.                
  102.             }
  103.         }
  104.  
  105.        
  106.         cvNamedWindow("Image 3", 1);
  107.         cvMoveWindow("Image 3", 630,  0);
  108.         for (alpha=0.0; alpha<=1.0; alpha = alpha + 0.1)
  109.         {
  110.             beta = 1 - alpha;
  111.            
  112.             cvAddWeighted(img, beta, img3, alpha, 0.0, dst);
  113.            
  114.             cvShowImage("Image 3", dst);
  115.             cvWaitKey(150);
  116.         }
  117.        
  118.         cvWaitKey(0);
  119.        
  120.        
  121.         //Destroy and release
  122.         cvDestroyWindow( "Image 2" );
  123.         cvDestroyWindow( "Image 1" );
  124.         //cvDestroyWindow( "Image 4" );
  125.         cvDestroyWindow( "Image 3" );
  126.         cvReleaseImage( &dst );
  127.         cvReleaseImage( &img3 );
  128.         cvReleaseImage( &img );
  129.         //cvReleaseImage( &img2 );
  130.         return 0;
  131.     }    
  132.    
  133.    
  134.     printf("Can't open image file\n");
  135.     //system("PAUSE");
  136.     return -1;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment