@@ -34,8 +34,8 @@ void apply_gaussian_blur(uchar * input_image, uchar * output_image, int row, int
34
34
35
35
__global__
36
36
void apply_sobel_filter (uchar * input_image, float * magnitude, float * gradient, int row, int col) {
37
- int h_filter[] = {1 , 0 , - 1 , 2 , 0 , - 2 , 1 , 0 , - 1 };
38
- int v_filter[] = {1 , 2 , 1 , 0 , 0 , 0 , - 1 , - 2 , - 1 };
37
+ int h_filter[] = {- 1 , 0 , 1 , - 2 , 0 , 2 , - 1 , 0 , 1 };
38
+ int v_filter[] = {- 1 , - 2 , - 1 , 0 , 0 , 0 , 1 , 2 , 1 };
39
39
int index = threadIdx .x + blockIdx .x * blockDim .x ;
40
40
if (index < row * col) {
41
41
int xind = index / col;
@@ -47,12 +47,12 @@ void apply_sobel_filter(uchar * input_image, float * magnitude, float * gradient
47
47
int x1ind = xind + i;
48
48
int y1ind = yind + j;
49
49
if (x1ind >= 0 && x1ind < row && y1ind >= 0 && y1ind < col) {
50
- dX += h_filter[((i + z)) * 2 + (j + z)] * input_image[x1ind * col + y1ind];
51
- dY += v_filter[((i + z)) * 2 + (j + z)] * input_image[x1ind * col + y1ind];
50
+ dX += h_filter[((i + z)) * 3 + (j + z)] * input_image[x1ind * col + y1ind];
51
+ dY += v_filter[((i + z)) * 3 + (j + z)] * input_image[x1ind * col + y1ind];
52
52
}
53
53
}
54
54
}
55
- magnitude[index ] = sqrt ((float )(dX * dX + dY * dY) );
55
+ magnitude[index ] = hypot ((float )dX, ( float )dY );
56
56
gradient[index ] = atan2 ((float )dY, (float )dX);
57
57
}
58
58
@@ -116,14 +116,23 @@ void detect_edge(uchar * input_image, uchar * output_image, int row, int col, in
116
116
cout << ms << " " << ms2 << endl;
117
117
118
118
float output[row * col];
119
- cudaMemcpy ((void *) output, d_blur , sizeof (float ) * row * col, cudaMemcpyDeviceToHost);
119
+ cudaMemcpy ((void *) output, d_magnitude , sizeof (float ) * row * col, cudaMemcpyDeviceToHost);
120
120
float max2 = 0.0 ;
121
+ clock_t start1, end;
122
+ start1 = clock ();
121
123
for (int i = 0 ; i < row * col; ++i) {
122
124
max2 = max (max2, output[i]);
123
125
}
124
126
for (int i = 0 ; i < row * col; ++i) {
125
127
output_image[i] = 255.0 * (output[i] / max2);
126
128
}
129
+ end = clock ();
130
+ cout << double (end - start1) / double (CLOCKS_PER_SEC) << endl;
131
+ cudaFree (d_in);
132
+ cudaFree (d_blur);
133
+ cudaFree (d_kernel);
134
+ cudaFree (d_magnitude);
135
+ cudaFree (d_gradient);
127
136
}
128
137
129
138
0 commit comments