Skip to content

Uses Edge Detection algorithm and Canny filter to identify potholes and provide warnings to the driver

Notifications You must be signed in to change notification settings

letranduytan/PotholeDetection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 

Repository files navigation

Pothole Detection using OpenCV-Python

Overview

Description: This project uses classic image processing techniques including edge detection and the Canny filter to detect potholes in road images and generate alerts for drivers.

Pothole Detection


1. Block Diagram

flowchart TD
    A[Start] --> B[Read Image]
    B --> C[Convert to Grayscale]
    C --> D[Apply Gaussian Blur]
    D --> E[Apply Canny Edge Detection]
    E --> F[Find Contours]
    F --> G[Filter Out Small Contours]
    G --> H[Detect Potential Pothole Regions]
    H --> I[Draw Bounding Boxes and Labels]
    I --> J[Display Result]
Loading

2. Edge Detection Overview

Edge detection highlights boundaries in images where there are significant changes in color intensity or brightness. It plays a crucial role in:

  • Object Detection
  • Shape Analysis
  • Object Tracking
  • Image Segmentation

Common Edge Detection Techniques

1. Canny Filter
Powerful algorithm involving smoothing, gradient calculation, thinning, and double thresholding.

2. Gradient Operators (Sobel, Prewitt, Roberts)
Detect intensity changes in vertical/horizontal or diagonal directions.

3. Laplacian / Laplacian of Gaussian (LoG)
Second-order derivative methods useful for detecting rapid intensity changes.

4. Morphological Operations
Improve clarity of shapes using dilation, erosion, opening, and closing.

5. Hough Transform
Useful for detecting lines or curves using a voting system.

6. Watershed Algorithm
Segment overlapping or adjacent objects by treating image as topography.


3. Canny Edge Detection

Step 1: Gaussian Blur

Reduces image noise with a Gaussian kernel:

G ( x , y ) = 1 2 π σ 2 e x 2 + y 2 2 σ 2

Step 2: Compute Gradient

G = G x 2 + G y 2 , θ = arctan ( G y G x )

Step 3: Non-Maximum Suppression

Thin edges by removing non-maximum gradient values.

Step 4: Double Thresholding

Separate strong and weak edges using high/low threshold values.

Step 5: Edge Tracking by Hysteresis

Link weak edges to strong edges to maintain edge continuity.


4. Sample Code

Convert to Grayscale

im = cv2.imread('611.jpg')
gray1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imwrite('graypothholeresult.jpg', gray1)

Contour Detection

imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours2, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
out = cv2.drawContours(im.copy(), contours2, -1, (250, 250, 250), 1)

Draw Bounding Boxes

for c in contours:
    rect = cv2.boundingRect(c)
    if rect[2] < 100 or rect[3] < 100: continue
    x, y, w, h = rect
    cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 8)
    cv2.putText(im, 'Pothole Detected', (x + w + 40, y + h), 0, 2.0, (0, 255, 0))

Check Convexity

k = cv2.isContourConvex(cnt)
print(k)

Blurring Methods

blur = cv2.blur(im, (5, 5))
gblur = cv2.GaussianBlur(im, (5, 5), 0)
median = cv2.medianBlur(im, 5)

Morphological Operations

kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(median, kernel, iterations=1)
dilation = cv2.dilate(erosion, kernel, iterations=5)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
edges = cv2.Canny(dilation, 9, 220)

Display Results with Matplotlib

plt.subplot(332), plt.imshow(blur), plt.title('Blurred')
plt.subplot(333), plt.imshow(gblur), plt.title('Gaussian Blur')
plt.subplot(334), plt.imshow(median), plt.title('Median Blur')
plt.subplot(337), plt.imshow(im, cmap='gray'), plt.title('Dilated Image')
plt.subplot(338), plt.imshow(edges, cmap='gray'), plt.title('Edge Image')
plt.subplot(335), plt.imshow(erosion), plt.title('Erosion')
plt.subplot(336), plt.imshow(closing), plt.title('Closing')
plt.show()

5. Resources

OpenCV Installation

Install with pip:

pip install opencv-python

References

  • CSIR (2010). Potholes: Technical Guide to Their Causes, Identification and Repair.
    View PDF

  • OpenCV (2014). The OpenCV Reference Manual Release 2.4.9.0.
    View PDF

About

Uses Edge Detection algorithm and Canny filter to identify potholes and provide warnings to the driver

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages