https://www.youtube.com/watch?v=3O4J4DH4tyo&list=PLaFjr-j4HdiukF6f2LW-PGsXs1vy4W8VR&index=1
https://www.youtube.com/watch?v=-4788Tmz9Zo&list=PLaFjr-j4HdisLdfbe3SuTwq6kiBdgr50z
https://www.youtube.com/watch?v=bYZ2ftVIr0g&list=PLaFjr-j4HdivG5mMhaAI5SrrpbR_a0viv
https://www.youtube.com/watch?v=5xdKykxgfPA&list=PLaFjr-j4HdiskLlqPHt8fHwP-r8CtZvMI
def auto_canny(image, sigma=0.33):
# compute the median of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)
# return the edged image
return edged
import cv2
import numpy as np
# Step 1: Read the image
image = cv2.imread('vehicle_image.jpg')
# Step 2: Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Step 3: Apply Gaussian Blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Step 4: Use Canny edge detection
edges = cv2.Canny(blurred, 50, 150)
# Step 5: Find contours in the edge-detected image
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Step 6: Loop through contours and filter by size and aspect ratio
for contour in contours:
# Approximate the contour to a polygon
epsilon = 0.04 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# Check if the contour is a quadrilateral (4 sides)
if len(approx) == 4:
# Get the bounding box of the contour
x, y, w, h = cv2.boundingRect(approx)
# Filter based on aspect ratio (usually, plates are rectangular)
aspect_ratio = w / float(h)
if 2 < aspect_ratio < 5: # Typical license plate aspect ratio
# Draw the contour (for debugging purposes)
cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
# Crop the region (license plate) from the original image
plate = image[y:y+h, x:x+w]
cv2.imshow("License Plate", plate)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Step 7: Display the image with detected contours
cv2.imshow('Detected License Plate', image)
cv2.waitKey(0)
cv2.destroyAllWindows()