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.
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]
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
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.
Reduces image noise with a Gaussian kernel:
Thin edges by removing non-maximum gradient values.
Separate strong and weak edges using high/low threshold values.
Link weak edges to strong edges to maintain edge continuity.
im = cv2.imread('611.jpg')
gray1 = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imwrite('graypothholeresult.jpg', gray1)
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)
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))
k = cv2.isContourConvex(cnt)
print(k)
blur = cv2.blur(im, (5, 5))
gblur = cv2.GaussianBlur(im, (5, 5), 0)
median = cv2.medianBlur(im, 5)
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)
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()
Install with pip:
pip install opencv-python