|
3 | 3 |
|
4 | 4 | void hysteresis_filter(hls::FIFO<unsigned char> &input_fifo,
|
5 | 5 | hls::FIFO<unsigned char> &output_fifo) {
|
6 |
| - #pragma HLS function pipeline |
7 | 6 |
|
8 |
| - if (input_fifo.empty()) |
9 |
| - return; |
10 |
| - |
11 |
| - unsigned char input_pixel = input_fifo.read(); |
12 |
| - |
13 |
| - static hls::LineBuffer<unsigned char, WIDTH, HF_KERNEL_SIZE> line_buffer; |
14 |
| - |
15 |
| - line_buffer.ShiftInPixel(input_pixel); |
16 |
| - |
17 |
| - // keep track of how many pixels we have shifted into the line_buffer to |
18 |
| - // tell when it is filled |
19 |
| - static int count = 0; |
20 |
| - if (!is_filled(HF_KERNEL_SIZE, count)) { |
21 |
| - count++; |
22 |
| - return; |
23 |
| - } |
24 |
| - |
25 |
| - // i, j to track the position we are at while processing each input frame |
26 |
| - static unsigned int i = 0, j = 0; |
| 7 | + hls::LineBuffer<unsigned char, WIDTH, HF_KERNEL_SIZE> line_buffer; |
| 8 | + const unsigned center = HF_KERNEL_SIZE / 2; |
| 9 | + const unsigned LineBufferFillCount = center * WIDTH + center; |
| 10 | + // track the position we are at while processing each input frame |
| 11 | + unsigned int x = 0, y = 0; |
| 12 | + |
| 13 | +#pragma HLS loop pipeline |
| 14 | + for (unsigned int i = 0; i < (HEIGHT * WIDTH + LineBufferFillCount); i++) { |
| 15 | + unsigned char input_pixel = 0; |
| 16 | + if (i < HEIGHT * WIDTH) |
| 17 | + input_pixel = input_fifo.read(); |
| 18 | + line_buffer.ShiftInPixel(input_pixel); |
| 19 | + |
| 20 | + // keep track of how many pixels we have shifted into the line_buffer to |
| 21 | + // tell when it is filled |
| 22 | + if (!is_filled(HF_KERNEL_SIZE, i)) { |
| 23 | + continue; |
| 24 | + } |
27 | 25 |
|
28 |
| - // calculate if the kernel is currently out of bounds, i.e. the kernel |
29 |
| - // overlaps with pixels outside of the current input frame |
30 |
| - bool outofbounds = is_out_of_bounds(HF_KERNEL_SIZE, i, j); |
| 26 | + // calculate if the kernel is currently out of bounds, i.e. the kernel |
| 27 | + // overlaps with pixels outside of the current input frame |
| 28 | + bool out_of_bounds = |
| 29 | + is_out_of_bounds(HF_KERNEL_SIZE, y, x); |
31 | 30 |
|
32 |
| - // update i, j for next iteration |
33 |
| - update_image_position(i, j); |
| 31 | + // update x, y for next iteration |
| 32 | + update_image_position(y, x); |
34 | 33 |
|
35 |
| - if (outofbounds) { |
36 |
| - output_fifo.write(0); |
37 |
| - return; |
38 |
| - } |
| 34 | + if (out_of_bounds) { |
| 35 | + output_fifo.write(0); |
| 36 | + continue; |
| 37 | + } |
39 | 38 |
|
40 |
| - // check stencil for strong edges where the two most |
41 |
| - // significant bits are set to one (& 0xC0) |
42 |
| - int threshold = 0; |
43 |
| - for (unsigned int m = 0; m < HF_KERNEL_SIZE; m++) { |
44 |
| - for (unsigned int n = 0; n < HF_KERNEL_SIZE; n++) { |
45 |
| - threshold = (line_buffer.window[m][n] & 0xC0) ? 1 : threshold; |
| 39 | + // check stencil for strong edges where the two most |
| 40 | + // significant bits are set to one (& 0xC0) |
| 41 | + int threshold = 0; |
| 42 | + for (unsigned int m = 0; m < HF_KERNEL_SIZE; m++) { |
| 43 | + for (unsigned int n = 0; n < HF_KERNEL_SIZE; n++) { |
| 44 | + threshold = (line_buffer.window[m][n] & 0xC0) ? 1 : threshold; |
| 45 | + } |
46 | 46 | }
|
47 |
| - } |
48 | 47 |
|
49 |
| - // strengthen edge by a factor of 2 |
50 |
| - unsigned int center = HF_KERNEL_SIZE / 2; |
51 |
| - int output = line_buffer.window[center][center] * 2; |
52 |
| - output = (output > 255) ? 255 : output; |
| 48 | + // strengthen edge by a factor of 2 |
| 49 | + unsigned int center = HF_KERNEL_SIZE / 2; |
| 50 | + int output = line_buffer.window[center][center] * 2; |
| 51 | + output = (output > 255) ? 255 : output; |
53 | 52 |
|
54 |
| - // only keep edge if it neighbours on a strong edge |
55 |
| - output = threshold ? output : 0; |
| 53 | + // only keep edge if it neighbours on a strong edge |
| 54 | + output = threshold ? output : 0; |
56 | 55 |
|
57 |
| - output_fifo.write(output); |
| 56 | + output_fifo.write(output); |
| 57 | + } |
58 | 58 | }
|
59 |
| - |
0 commit comments