-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcudaProcess.cu
More file actions
168 lines (126 loc) · 5.79 KB
/
cudaProcess.cu
File metadata and controls
168 lines (126 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "cudaProcess.h"
__global__
void execCudaGrayscale(unsigned char* image, unsigned char* grayImage, int rows, int cols, int channels, int step) {
int index = threadIdx.x + (blockDim.x * blockIdx.x);
int y = index / cols;
int x = index % cols;
int blue = (int)image[channels*x + step*y];
int green = (int)image[channels*x + step*y + 1];
int red = (int)image[channels*x + step*y + 2];
grayImage[x + cols*y] = (unsigned char)(.3*red) + (.59 * green) + (.11 * blue);
}
__device__
void cudaKernelSum(unsigned char* image, int rows, int cols, int channels, int step, int x, int y, int size, int* sum) {
int numPixels = 0;
for (int i = (x - (size/2)); i < (x + (size/2))+1; i++) {
for (int j = (y - (size/2)); j < (y + (size/2))+1; j++) {
if (i >= 0 && j >= 0 && i < cols && j < rows) {
sum[0] += image[i*channels + j*step];
sum[1] += image[i*channels + j*step + 1];
sum[2] += image[i*channels + j*step + 2];
numPixels++;
}
}
}
sum[0] = sum[0] / numPixels;
sum[1] = sum[1] / numPixels;
sum[2] = sum[2] / numPixels;
}
__global__
void execCudaBlur(unsigned char* image, unsigned char* blurImage, int rows, int cols, int channels, int step, int size) {
int index = threadIdx.x + (blockDim.x * blockIdx.x);
int sum[3] = {0,0,0};
int y = index / cols;
int x = index % cols;
cudaKernelSum(image, rows, cols, channels, step, x, y, size, sum);
blurImage[channels*x + step*y] = sum[0];
blurImage[channels*x + step*y + 1] = sum[1];
blurImage[channels*x + step*y + 2] = sum[2];
}
__device__
void cudaKernelLineDetect(unsigned char* image, int rows, int cols, int x, int y, int* val, int* cudaKernelArray) {
int numPixels = 0;
int kx = 0;
for (int i = (x - 1); i < (x + 2); i++) {
int ky = 0;
for (int j = (y - 1); j < (y + 2); j++) {
if (i >= 0 && j >= 0 && i < cols && j < rows) {
for(int k = 0; k < 4; k ++) {
val[0] += cudaKernelArray[k*9 + kx + ky*3] * image[i + cols*j];
}
numPixels++;
}
ky++;
}
kx++;
}
val[0] = val[0] / (numPixels*4);
}
__global__
void execCudaDetectLine(unsigned char* image, unsigned char* lineImage, int rows, int cols, int channels, int step, int* kernel) {
//Assuming gray image input
int index = threadIdx.x + (blockDim.x * blockIdx.x);
int val[1] = {0};
int y = index / cols;
int x = index % cols;
cudaKernelLineDetect(image, rows, cols, x, y, val, kernel);
lineImage[x + cols*y] = *val;
}
unsigned char* cudaGrayscale(unsigned char* image, int rows, int cols, int channels, int step) {
int threadsPerBlock = 1024;
int numBlocks = ((rows*cols) / 1024) + 1;
unsigned char* cudaImage;
unsigned char* cudaGrayImage;
cudaMallocManaged(&cudaImage, sizeof(unsigned char)*rows*cols*channels);
cudaMallocManaged(&cudaGrayImage, sizeof(unsigned char)*rows*cols);
memcpy(cudaImage, image, sizeof(unsigned char)*rows*cols*channels);
memset(cudaGrayImage, 0, sizeof(unsigned char)*rows*cols);
execCudaGrayscale<<<numBlocks, threadsPerBlock>>>(cudaImage, cudaGrayImage, rows, cols, channels, step);
cudaDeviceSynchronize();
unsigned char* grayImage = (unsigned char*)malloc(sizeof(unsigned char)*rows*cols);
memcpy(grayImage, cudaGrayImage, sizeof(unsigned char)*rows*cols);
cudaFree(cudaImage);
cudaFree(cudaGrayImage);
return grayImage;
}
unsigned char* cudaBlur(unsigned char* image, int rows, int cols, int channels, int step, int size) {
int threadsPerBlock = 1024;
int numBlocks = ((rows*cols) / 1024) + 1;
unsigned char* cudaImage;
unsigned char* cudaBlurImage;
cudaMallocManaged(&cudaImage, sizeof(unsigned char)*rows*cols*channels);
cudaMallocManaged(&cudaBlurImage, sizeof(unsigned char)*rows*cols*channels);
memcpy(cudaImage, image, sizeof(unsigned char)*rows*cols*channels);
memset(cudaBlurImage, 0, sizeof(unsigned char)*rows*cols*channels);
execCudaBlur<<<numBlocks, threadsPerBlock>>>(cudaImage, cudaBlurImage, rows, cols, channels, step, size);
cudaDeviceSynchronize();
unsigned char* blurImage = (unsigned char*)malloc(sizeof(unsigned char)*rows*cols*channels);
memcpy(blurImage, cudaBlurImage, sizeof(unsigned char)*rows*cols*channels);
cudaFree(cudaImage);
cudaFree(cudaBlurImage);
return blurImage;
}
unsigned char* cudaDetectLine(unsigned char* image, int rows, int cols, int channels, int step) {
int threadsPerBlock = 1024;
int numBlocks = ((rows*cols) / 1024) + 1;
unsigned char* grayImage = cudaGrayscale(image, rows, cols, channels, step);
unsigned char* cudaImage;
unsigned char* cudaLineImage;
int* cudaKernelArrayMalloc;
cudaMallocManaged(&cudaImage, sizeof(unsigned char)*rows*cols);
cudaMallocManaged(&cudaLineImage, sizeof(unsigned char)*rows*cols);
cudaMallocManaged(&cudaKernelArrayMalloc, sizeof(int)*36);
int cudaKernelArray[36] = {-1,-1,-1,2,2,2,-1,-1,-1,-1,2,-1,-1,2,-1,-1,2,-1,
-1,-1,2,-1,2,-1,2,-1,-1,2,-1,-1,-1,2,-1,-1,-1,2};
memcpy(cudaKernelArrayMalloc, cudaKernelArray, sizeof(int)*36);
memcpy(cudaImage, grayImage, sizeof(unsigned char)*rows*cols);
memset(cudaLineImage, 0, sizeof(unsigned char)*rows*cols);
execCudaDetectLine<<<numBlocks, threadsPerBlock>>>(cudaImage, cudaLineImage, rows, cols, channels, step, cudaKernelArrayMalloc);
cudaDeviceSynchronize();
unsigned char* lineImage = (unsigned char*)malloc(sizeof(unsigned char)*rows*cols);
memcpy(lineImage, cudaLineImage, sizeof(unsigned char)*rows*cols);
cudaFree(cudaImage);
cudaFree(cudaLineImage);
cudaFree(cudaKernelArrayMalloc);
return lineImage;
}