Stybyk

main pro kernely cuda

Dec 2nd, 2014
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.63 KB | None | 0 0
  1. // ***********************************************************************
  2. //
  3. // Demo program pro vyuku predmetu APPS (10/2012)
  4. // Petr Olivka, katedra informatiky, FEI, VSB-TU Ostrava
  5. // email:petr.olivka@vsb.cz
  6. //
  7. // Priklad pouziti CUDA technologie.
  8. // Prevedeni barevneho obrazku na odstiny sede.
  9. //
  10. // Pro manimulaci s obrazkem je pouzita knihovna OpenCV.
  11. //
  12. // ***********************************************************************
  13.  
  14. #include <stdio.h>
  15. #include <cuda_runtime.h>
  16. #include "..\include\opencv\highgui.h"
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. void joinImage( uchar4 *first, uchar4 *second,uchar4 *final, int width, int height );
  27.  
  28. void run_grayscale( uchar4 *color_pic, uchar4 *bw_pic, uchar4 *MyO ,int sizex, int sizey );
  29. void run_rotace(uchar4 *input ,uchar4* output ,int sizex ,int sizey);
  30. void run_resize_low(uchar4 *input ,uchar4* output ,int sizex ,int sizey);
  31. void run_resize_big(uchar4 *input ,uchar4* output ,int sizex ,int sizey);
  32. int main( int numarg, char **arg )
  33. {
  34.     if ( numarg < 2 )
  35.     {
  36.         printf( "Enter picture filename!\n" );
  37.         return 1;
  38.     }
  39.  
  40.     /////
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.         // nacteni obrazku
  48.     IplImage *first =  cvLoadImage( arg[ 1 ] );
  49.     IplImage *second = cvLoadImage( arg[ 2] );
  50.    
  51.     int height = first->height;
  52.     int width = first->width;
  53.  
  54.     uchar4 *firstImg = new uchar4[ width * height ];
  55.     uchar4 *secondImg = new uchar4[ width * height ];
  56.     uchar4 *finalImg = new uchar4[ width * height ];
  57.    
  58.     // vyplneni obrazku barevnym gradientem modra-zelena-cervena
  59.     for ( int y = 0; y < height; y++ )
  60.         for ( int x  = 0; x < width; x++ )
  61.         {  
  62.             CvScalar s = cvGet2D( first, y, x );
  63.             uchar4 bgr = { (char) s.val[ 0 ], (char) s.val[ 1 ], (char) s.val[ 2 ] };
  64.             firstImg[ y * width + x ] = bgr;
  65.         }
  66.     for ( int y = 0; y < height; y++ )
  67.         for ( int x  = 0; x < width; x++ )
  68.         {  
  69.             CvScalar s = cvGet2D( second, y, x );
  70.             uchar4 bgr = { (char) s.val[ 0 ], (char) s.val[ 1 ], (char) s.val[ 2 ] };
  71.             secondImg[ y * width + x ] = bgr;
  72.         }
  73.  
  74.  
  75.        
  76.  
  77.  
  78.  
  79.  
  80.  
  81.     // prenos vypoctu do souboru .cu
  82.     joinImage(firstImg,secondImg,finalImg,width,height);
  83.     cvShowImage("Prvni",first);
  84.     cvShowImage("Druhy",second);
  85.     IplImage *joinedimg = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 3 );
  86.  
  87.     // prevedeni ziskanych dat do obrazku
  88.     for ( int y = 0; y < height; y++ )
  89.         for ( int x  = 0; x < width; x++ )
  90.         {
  91.             uchar4 bgr = finalImg[ y * width + x ];
  92.             CvScalar s = { bgr.x, bgr.y, bgr.z };
  93.             cvSet2D( joinedimg, y, x, s );
  94.         }
  95.  
  96.     // zobrazeni vysledku
  97.     cvShowImage( "joinded image", joinedimg );
  98.     cvWaitKey( 0 );
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.     //
  122.     // nacteni obrazku
  123.     IplImage *bgr_img = cvLoadImage( arg[ 1 ] );
  124.     int sizex = bgr_img->width;
  125.     int sizey = bgr_img->height;
  126.  
  127.  
  128.     int lowsizex = sizex /2;
  129.  
  130.     int hightsizex = sizex *2;
  131.     int lowsizey = sizey /2;
  132.     int hightsizey = sizex *2;
  133.  
  134.  
  135.  
  136.     // alokace poli uchar4 pro body obrazku a jeho naplneni
  137.     uchar4 *bgr_pole = new uchar4[ sizex * sizey ];
  138.     uchar4 *bw_pole = new uchar4[ sizex * sizey ];
  139.     uchar4 *MyO_pole = new uchar4[ sizex * sizey ];
  140.  
  141.     uchar4* Rotate_pole = new uchar4[sizex * sizey];
  142.  
  143.     uchar4 *lowpole = new uchar4[lowsizex * lowsizey];
  144.  
  145.     uchar4 *bigpole = new uchar4[hightsizex * hightsizey];
  146.  
  147.     /// input obrazku do pole bgr
  148.     for ( int y = 0; y < sizey; y++ )
  149.         for ( int x  = 0; x < sizex; x++ )
  150.         {
  151.             CvScalar s = cvGet2D( bgr_img, y, x );
  152.             uchar4 bgr = { (char) s.val[ 0 ], (char) s.val[ 1 ], (char) s.val[ 2 ] };
  153.             bgr_pole[ y * sizex + x ] = bgr;
  154.  
  155.         }
  156.  
  157.     // volani funkce ze souboru .cu
  158.     run_grayscale( bgr_pole, bw_pole, MyO_pole ,sizex, sizey );
  159.  
  160.  
  161.     run_rotace(bgr_pole,Rotate_pole,sizex,sizey);
  162.  
  163.     run_resize_low(bgr_pole,lowpole,sizex,sizey);
  164.     run_resize_big(bgr_pole,bigpole,sizex,sizey);
  165.  
  166.     IplImage *bw_img = cvCreateImage( cvSize( sizex, sizey ), IPL_DEPTH_8U, 3 );
  167.     IplImage *MyO = cvCreateImage( cvSize( sizex, sizey ), IPL_DEPTH_8U, 3 );
  168.  
  169.     IplImage *RotateImage = cvCreateImage( cvSize( sizey, sizex ), IPL_DEPTH_8U, 3 );
  170.  
  171.     IplImage *LowImage = cvCreateImage( cvSize( lowsizex, lowsizey ), IPL_DEPTH_8U, 3 );
  172.     IplImage *BigImage = cvCreateImage( cvSize( hightsizex, hightsizey ), IPL_DEPTH_8U, 3 );
  173.  
  174.     // maly obrazek
  175.     for ( int y = 0; y < lowsizey; y++ )
  176.         for ( int x  = 0; x < lowsizex; x++ )
  177.         {
  178.             uchar4 bgr = lowpole[ y * lowsizex + x ];
  179.             CvScalar s = { bgr.x, bgr.y, bgr.z };
  180.             cvSet2D( LowImage, y, x, s );
  181.         }
  182.  
  183.  
  184.         // velky obrazek
  185.         for ( int y = 0; y < hightsizey; y++ )
  186.         for ( int x  = 0; x <hightsizex; x++ )
  187.         {
  188.             uchar4 bgr = bigpole[ y * hightsizex + x ];
  189.             CvScalar s = { bgr.x, bgr.y, bgr.z };
  190.             cvSet2D( BigImage, y, x, s );
  191.         }
  192.  
  193.  
  194.  
  195.  
  196.     // ziskana data ulozime do noveho obrazku
  197.     for ( int y = 0; y < sizey; y++ )
  198.         for ( int x  = 0; x < sizex; x++ )
  199.         {
  200.             uchar4 bgr = bw_pole[ y * sizex + x ];
  201.             CvScalar s = { bgr.x, bgr.y, bgr.z };
  202.             cvSet2D( bw_img, y, x, s );
  203.         }
  204.  
  205.  
  206.         // ziskana data ulozime do noveho obrazku
  207.     for ( int y = 0; y < sizey; y++ )
  208.         for ( int x  = 0; x < sizex; x++ )
  209.         {
  210.             uchar4 bgr1 = MyO_pole[ y * sizex + x ];
  211.             CvScalar s = { bgr1.x, bgr1.y, bgr1.z };
  212.             cvSet2D( MyO, y, x, s );
  213.         }
  214.  
  215.  
  216.    
  217.         /// zobrayeni rotovaneho obrazku
  218.         for ( int y = 0; y < sizex; y++ )
  219.         for ( int x  = 0; x < sizey; x++ )
  220.         {
  221.             uchar4 bgr = Rotate_pole[ y * sizey + x ];
  222.             CvScalar s = { bgr.x, bgr.y, bgr.z };
  223.             cvSet2D (RotateImage, y, x, s );
  224.         }
  225.  
  226.     // zobrazeni puvodniho obrazku a vysledku
  227.     cvShowImage( "Color", bgr_img );
  228.     cvShowImage( "GrayScale", bw_img );
  229.     cvShowImage( "MojeUprava",MyO);
  230.     cvShowImage( "rotace",RotateImage);
  231.     cvShowImage( "zmenseni",LowImage );
  232.     cvShowImage( "zvetseni",BigImage );
  233.     cvWaitKey( 0 );
  234. }
Add Comment
Please, Sign In to add comment