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

{
22
unsigned int x = blockIdx.x*blockDim.x+threadIdx.x;
23
unsigned int y = blockIdx.y*blockDim.y+threadIdx.y;
24
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;
27
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;
29
}
30
}
31
32
int main()
33

{
34
IplImage* src = cvLoadImage("IMG_03.JPG");
35
36
int widthStep = src->widthStep;
37
int width = src->width;
38
int height = src->height;
39
40
printf("before widthStep = %d\n", widthStep);
41
if( widthStep%4 != 0)
42
{
43
widthStep = (1+widthStep/4)*4;
44
}
45
printf("after widthStep = %d\n", widthStep);
46
47
unsigned char* d_img_data;
48
CUDA_SAFE_CALL(cudaMalloc((void**)&d_img_data, widthStep*height));
49
CUDA_SAFE_CALL(cudaMemcpy(d_img_data, src->imageData, widthStep*height, cudaMemcpyHostToDevice));
50
51
dim3 dimBlock(16, 16, 1);
52
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);
54
CUDA_SAFE_CALL(cudaThreadSynchronize());
55
56
CUDA_SAFE_CALL( cudaMemcpy( src->imageData, d_img_data, widthStep*height, cudaMemcpyDeviceToHost) );
57
58
cvNamedWindow("test",CV_WINDOW_AUTOSIZE);
59
cvShowImage("test",src);
60
cvWaitKey(0);
61
cvDestroyAllWindows();
62
63
cvReleaseImage(&src);
64
CUDA_SAFE_CALL(cudaFree(d_img_data));
65
return 0;
66
}
#include <cutil_inline.h>2
#include <cv.h>3
#include <cstdio>4
#include <iostream>5
#include <cutil.h>6
#include <ctime>7
#include <cstdlib>8
#include <highgui.h>9
#include <windows.h>10

11
#pragma comment(lib, "cuda.lib")12
#pragma comment(lib, "cudart.lib")13
#pragma comment(lib, "cutil32.lib")14
#pragma comment(lib, "cv.lib")15
#pragma comment(lib, "cxcore.lib")16
#pragma comment(lib, "highgui.lib")17

18
using namespace std;19

20
__global__ void mainKernel(unsigned char *d_data, int widthStep, int width, int height)21


{22
unsigned int x = blockIdx.x*blockDim.x+threadIdx.x;23
unsigned int y = blockIdx.y*blockDim.y+threadIdx.y;24
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;27
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;29
}30
}31

32
int main()33


{34
IplImage* src = cvLoadImage("IMG_03.JPG");35

36
int widthStep = src->widthStep;37
int width = src->width;38
int height = src->height;39

40
printf("before widthStep = %d\n", widthStep);41
if( widthStep%4 != 0)42

{43
widthStep = (1+widthStep/4)*4;44
}45
printf("after widthStep = %d\n", widthStep);46

47
unsigned char* d_img_data;48
CUDA_SAFE_CALL(cudaMalloc((void**)&d_img_data, widthStep*height));49
CUDA_SAFE_CALL(cudaMemcpy(d_img_data, src->imageData, widthStep*height, cudaMemcpyHostToDevice));50

51
dim3 dimBlock(16, 16, 1);52
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);54
CUDA_SAFE_CALL(cudaThreadSynchronize());55

56
CUDA_SAFE_CALL( cudaMemcpy( src->imageData, d_img_data, widthStep*height, cudaMemcpyDeviceToHost) );57
58
cvNamedWindow("test",CV_WINDOW_AUTOSIZE);59
cvShowImage("test",src);60
cvWaitKey(0);61
cvDestroyAllWindows();62

63
cvReleaseImage(&src);64
CUDA_SAFE_CALL(cudaFree(d_img_data));65
return 0;66
}

