1 #include <cutil_inline.h>
#include <cutil_inline.h>
 2 #include <cv.h>
#include <cv.h>
 3 #include <cstdio>
#include <cstdio>
 4 #include <iostream>
#include <iostream>
 5 #include <cutil.h>
#include <cutil.h>
 6 #include <ctime>
#include <ctime>
 7 #include <cstdlib>
#include <cstdlib>
 8 #include <highgui.h>
#include <highgui.h>
 9 #include <windows.h>
#include <windows.h>
10
11 #pragma comment(lib, "cuda.lib")
#pragma comment(lib, "cuda.lib")
12 #pragma comment(lib, "cudart.lib")
#pragma comment(lib, "cudart.lib")
13 #pragma comment(lib, "cutil32.lib")
#pragma comment(lib, "cutil32.lib")
14 #pragma comment(lib, "cv.lib")
#pragma comment(lib, "cv.lib")
15 #pragma comment(lib, "cxcore.lib")
#pragma comment(lib, "cxcore.lib")
16 #pragma comment(lib, "highgui.lib")
#pragma comment(lib, "highgui.lib")
17
18 using namespace std;
using namespace std;
19
20 __global__ void mainKernel(unsigned char *d_data, int widthStep, int width, int height)
__global__ void mainKernel(unsigned char *d_data, int widthStep, int width, int height)
21

 {
{
22 unsigned int x = blockIdx.x*blockDim.x+threadIdx.x;
    unsigned int x = blockIdx.x*blockDim.x+threadIdx.x;
23 unsigned int y = blockIdx.y*blockDim.y+threadIdx.y;
    unsigned int y = blockIdx.y*blockDim.y+threadIdx.y;
24 if( x>0 && x < width && y>0 && y < height )
    if( x>0 && x < width && y>0 && y < height )
25
 
     {
{                  
26 d_data[y*widthStep+x*3+0] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
        d_data[y*widthStep+x*3+0] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
27 d_data[y*widthStep+x*3+1] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
        d_data[y*widthStep+x*3+1] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
28 d_data[y*widthStep+x*3+2] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
        d_data[y*widthStep+x*3+2] ^= ( ((x&0x0F)==0) ^ ((y&0x0F)==0)  ) *255;
29 }
    }
30 }
}
31
32 int main()
int main()
33

 {
{
34 IplImage* src = cvLoadImage("IMG_03.JPG");
    IplImage* src = cvLoadImage("IMG_03.JPG");
35
36 int widthStep = src->widthStep;
    int widthStep = src->widthStep;
37 int width   = src->width;
    int width   = src->width;
38 int height  = src->height;
    int height  = src->height;
39
40 printf("before widthStep = %d\n", widthStep);
    printf("before widthStep = %d\n", widthStep);
41 if( widthStep%4 != 0)
    if( widthStep%4 != 0)
42
 
     {
{
43 widthStep = (1+widthStep/4)*4;
        widthStep = (1+widthStep/4)*4;
44 }
    }
45 printf("after widthStep = %d\n", widthStep);
    printf("after widthStep = %d\n", widthStep);
46
47 unsigned char* d_img_data;
    unsigned char* d_img_data;
48 CUDA_SAFE_CALL(cudaMalloc((void**)&d_img_data, widthStep*height));
    CUDA_SAFE_CALL(cudaMalloc((void**)&d_img_data, widthStep*height));
49 CUDA_SAFE_CALL(cudaMemcpy(d_img_data, src->imageData, widthStep*height, cudaMemcpyHostToDevice));
    CUDA_SAFE_CALL(cudaMemcpy(d_img_data, src->imageData, widthStep*height, cudaMemcpyHostToDevice));
50
51 dim3 dimBlock(16, 16, 1);
    dim3 dimBlock(16, 16, 1);
52 dim3 dimGrid( (width+dimBlock.x-1)/dimBlock.x, (height+dimBlock.y-1)/dimBlock.y );
    dim3 dimGrid( (width+dimBlock.x-1)/dimBlock.x, (height+dimBlock.y-1)/dimBlock.y );
53 mainKernel<<<dimGrid, dimBlock, 0>>>(d_img_data, widthStep, width, height);
    mainKernel<<<dimGrid, dimBlock, 0>>>(d_img_data, widthStep, width, height);
54 CUDA_SAFE_CALL(cudaThreadSynchronize());
    CUDA_SAFE_CALL(cudaThreadSynchronize());
55
56 CUDA_SAFE_CALL( cudaMemcpy( src->imageData, d_img_data, widthStep*height, cudaMemcpyDeviceToHost) );
    CUDA_SAFE_CALL( cudaMemcpy( src->imageData, d_img_data, widthStep*height, cudaMemcpyDeviceToHost) );
57 
    
58 cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
    cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
59 cvShowImage("test",src);
    cvShowImage("test",src);
60 cvWaitKey(0);
    cvWaitKey(0);
61 cvDestroyAllWindows();
    cvDestroyAllWindows();
62
63 cvReleaseImage(&src);
    cvReleaseImage(&src);
64 CUDA_SAFE_CALL(cudaFree(d_img_data));
    CUDA_SAFE_CALL(cudaFree(d_img_data));
65 return 0;
    return 0;
66 }
} 
 
	posted on 2009-12-28 10:58 
noBugnoGain 阅读(1822) 
评论(1)  编辑 收藏 引用