Skip to content

Commit d3552c9

Browse files
fixed sobel filter
1 parent 8a32d43 commit d3552c9

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Diff for: Canny/Canny_Edge_Detector.cu

+15-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ void apply_gaussian_blur(uchar * input_image, uchar * output_image, int row, int
3434

3535
__global__
3636
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};
3939
int index = threadIdx.x + blockIdx.x * blockDim.x;
4040
if(index < row * col) {
4141
int xind = index / col;
@@ -47,12 +47,12 @@ void apply_sobel_filter(uchar * input_image, float * magnitude, float * gradient
4747
int x1ind = xind + i;
4848
int y1ind = yind + j;
4949
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];
5252
}
5353
}
5454
}
55-
magnitude[index] = sqrt((float)(dX * dX + dY * dY));
55+
magnitude[index] = hypot((float)dX, (float)dY);
5656
gradient[index] = atan2((float)dY, (float)dX);
5757
}
5858

@@ -116,14 +116,23 @@ void detect_edge(uchar * input_image, uchar * output_image, int row, int col, in
116116
cout << ms << " " << ms2 << endl;
117117

118118
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);
120120
float max2 = 0.0;
121+
clock_t start1, end;
122+
start1 = clock();
121123
for(int i = 0; i < row * col; ++i) {
122124
max2 = max(max2, output[i]);
123125
}
124126
for(int i = 0; i < row * col; ++i) {
125127
output_image[i] = 255.0 * (output[i] / max2);
126128
}
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);
127136
}
128137

129138

Diff for: img

88 Bytes
Binary file not shown.

Diff for: test.png

40 KB
Loading

0 commit comments

Comments
 (0)