Search code examples
pythonopencvimage-processinggaussiancanny-operator

Finding waters edge using OpenCV and Python accurately


I have been working on trying to detect the edge of the water using OpenCV/Python, and the results I am getting are fairly inaccurate and there is no robustness. This is what I have achieved so far: Original Image, output image

Canny Edge detection

What I am currently doing is setting some variables (the level of Gaussian blur, the sigma used for the Canny edge detection, and the maximum deviation which the level measured can change between each point), performing an 'automatic' Canny edge detection (where the median pixel intensity is measured and used to form the lower and upper boundaries), then moving from the bottom left hand corner upwards to find the first 'white' pixel. This is done in x intervals of five the entire length of the frame.

The average y value of the points is the calculated. Each point is then tested to see if it deviates too far from the average pixel, with the deviation limit being set earlier. The remaining points are then drawn on the image as the blue line. The average value of the drawn pixels is recorded at each frame.

After 30 frames, the average of the averages is calculated and drawn as the red line, which is then assumed to be the 'real' water height.

Has anyone have any ideas on a better way to do this? What would make the edge of the water stand out more? This method works on most footage I have recorded, but with poor results.

Thanks in advance.


Solution

  • I have worked on a similar problem and I hope these advices can help you in some way:

    • Try to restrict your search area: can you make assumptions on where the water level should be? Consider also to have correctly detected the water level. Is it safe to assume that in the next frames the water level will decrease/increase constantly? Will it change slowly? Crop your image in order to take into consideration only the area where it is safe to assume that the water level is present.
    • Change color space: you can try to work in other color spaces like HSV in order to have the brightness separated from the chromaticity
    • Hough Transform line detection: try to use this algorithm to search for specific horizontal lines in the image, or other shapes.
    • Image undistortion: if necessary try to correct the image in order to rectify the curved lines, or cancel the perspective with an Inverse Perspective Mapping (IPM).

    You can also consider to change edge detection algorithm.