Stybyk

main pro kernely cuda

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