| 17 | using namespace dlib; |
| 18 | |
| 19 | int main() |
| 20 | { |
| 21 | // First let's make a 400x400 image. This will form the input to the Hough transform. |
| 22 | array2d<unsigned char> img(400,400); |
| 23 | // Now we make a hough_transform object. The 300 here means that the Hough transform |
| 24 | // will operate on a 300x300 subwindow of its input image. |
| 25 | hough_transform ht(300); |
| 26 | |
| 27 | image_window win, win2; |
| 28 | double angle1 = 0; |
| 29 | double angle2 = 0; |
| 30 | while(true) |
| 31 | { |
| 32 | // Generate a line segment that is rotating around inside the image. The line is |
| 33 | // generated based on the values in angle1 and angle2. So each iteration creates a |
| 34 | // slightly different line. |
| 35 | angle1 += pi/130; |
| 36 | angle2 += pi/400; |
| 37 | const point cent = center(get_rect(img)); |
| 38 | // A point 90 pixels away from the center of the image but rotated by angle1. |
| 39 | const point arc = rotate_point(cent, cent + point(90,0), angle1); |
| 40 | // Now make a line that goes though arc but rotate it by angle2. |
| 41 | const point l = rotate_point(arc, arc + point(500,0), angle2); |
| 42 | const point r = rotate_point(arc, arc - point(500,0), angle2); |
| 43 | |
| 44 | |
| 45 | // Next, blank out the input image and then draw our line on it. |
| 46 | assign_all_pixels(img, 0); |
| 47 | draw_line(img, l, r, 255); |
| 48 | |
| 49 | |
| 50 | const point offset(50,50); |
| 51 | array2d<int> himg; |
| 52 | // pick the window inside img on which we will run the Hough transform. |
| 53 | const rectangle box = translate_rect(get_rect(ht),offset); |
| 54 | // Now let's compute the hough transform for a subwindow in the image. In |
| 55 | // particular, we run it on the 300x300 subwindow with an upper left corner at the |
| 56 | // pixel point(50,50). The output is stored in himg. |
| 57 | ht(img, box, himg); |
| 58 | // Now that we have the transformed image, the Hough image pixel with the largest |
| 59 | // value should indicate where the line is. So we find the coordinates of the |
| 60 | // largest pixel: |
| 61 | point p = max_point(mat(himg)); |
| 62 | // And then ask the ht object for the line segment in the original image that |
| 63 | // corresponds to this point in Hough transform space. |
| 64 | std::pair<point,point> line = ht.get_line(p); |
| 65 | |
| 66 | // Finally, let's display all these things on the screen. We copy the original |
| 67 | // input image into a color image and then draw the detected line on top in red. |
| 68 | array2d<rgb_pixel> temp; |
| 69 | assign_image(temp, img); |
| 70 | // Note that we must offset the output line to account for our offset subwindow. |
| 71 | // We do this by just adding in the offset to the line endpoints. |
| 72 | draw_line(temp, line.first+offset, line.second+offset, rgb_pixel(255,0,0)); |
| 73 | win.clear_overlay(); |
| 74 | win.set_image(temp); |
| 75 | // Also show the subwindow we ran the Hough transform on as a green box. You will |
| 76 | // see that the detected line is exactly contained within this box and also |
nothing calls this directly
no test coverage detected