wonders (0)

  Since entering the electrical engineering, accidentally learned cuda, in the process of learning to understand some preliminary ideas of parallel computing,

Because of the limited space of my computer, so I am going to record some cuda programming exercises usual, put it on my blog so easy to park

After the study, which also can be grown in the garden blog to make it modest. Next, enter the topic:

  Cuda image gradation conversion achieved:

 1 #include "cuda_runtime.h"
 2 #include "device_launch_parameters.h"
 3 #include <stdio.h>
 4 #include<iostream>
 5 
 6 #include <iostream>  
 7 #include <opencv2/core/core.hpp>  
 8 #include <opencv2/highgui/highgui.hpp> 
 9 
10 #define CHANNELS 3
11 
12 __global__ void colorToGreyScaleConversion(unsigned char *pout, unsigned char *pin, int width, int height) {
13     int Col = blockIdx.x*blockDim.x + threadIdx.x;
14     int Row = blockIdx.y*blockDim.y + threadIdx.y;
15 
16     if (Col < width && Row < height) {
17         int greyoffset = Row * width + Col;
18         int rgbOffset = greyoffset * CHANNELS;
19 
20         unsigned char r = pin[rgbOffset];
21         unsigned char g = pin[rgbOffset + 1];
22         unsigned char b = pin[rgbOffset + 2];
 23 is  
24          Pout [greyoffset] = 0.21f * R & lt + 0.71f * G + 0.07f * B;
 25      }
 26 is  }
 27  
28  the using  namespace CV;
 29  int main ( void ) {
 30  
31 is      // read an image (thumbnail)     
32      Mat imread IMG = ( " E: \\ \\ lena512color.tiff OpenCV " );
 33 is      // Create a "picture" window called     
34 is      namedWindow ( " Lena " );
 35      //Picture displayed in the window    
36      imshow ( " Lena " , IMG);
 37 [      // Wait 6000 ms window automatically closes     
38 is      waitKey ( 6000 );
 39  
40      const  int imgheight = img.rows;
 41 is      const  int imgwidth = img.cols;
 42 is      const  int imgchannel = img.channels ();
 43 is  
44 is      Mat grayImage (imgheight, imgwidth, CV_8UC1, the Scalar ( 0 ));
 45  
46 is      unsigned char * dev_pin;
 47     unsigned char *dev_pout;
48 
49     cudaMalloc((void**)&dev_pin, imgheight*imgwidth*imgchannel* sizeof(unsigned char));
50     cudaMalloc((void**)&dev_pout, imgheight*imgwidth*sizeof(unsigned char));
51 
52     cudaMemcpy(dev_pin, img.data, imgheight*imgwidth*imgchannel * sizeof(unsigned char), cudaMemcpyHostToDevice);
53 
54 
55     dim3 BlockDim(16, 16);
56     dim3 GridDim((imgwidth - 1) / BlockDim.x + 1, (imgheight - 1) / BlockDim.y + 1);
57     colorToGreyScaleConversion << <GridDim, BlockDim >> > (dev_pout, dev_pin, imgwidth, imgheight);
58 
59     cudaMemcpy(grayImage.data, dev_pout, imgheight*imgwidth*sizeof(unsigned char), cudaMemcpyDeviceToHost);
60 
61     cudaFree(dev_pin);
62     cudaFree(dev_pout);
63     imshow("grayImage", grayImage);
64     waitKey(3000);
65     return 0;
66 }
greyConvert

  It is worth noting: I vs2017 is implemented on the platform, when programming call openCV library functions, the reader need to configure their own environment, in order to prevent

We spend too much time in the configuration environment, I am attaching a link on the configuration tutorial CSDN a big God: https://blog.csdn.net/qq_41175905/article/details/80560429

Before conversion as shown below:

  

FIG converted as follows:

Further readers available Matlab, openCV other library routines, found comparison, CUDA parallel computation time faster.

Guess you like

Origin www.cnblogs.com/xuelanga000/p/12381419.html