feat: update with claude code
This commit is contained in:
@@ -7,244 +7,93 @@ import logging
|
||||
from pathlib import Path
|
||||
from dataclasses import dataclass
|
||||
|
||||
def _uniform_car_plate(img: cv.typing.MatLike) -> cv.typing.MatLike:
|
||||
"""
|
||||
Uniform the image size to 512x512 while maintaining aspect ratio, padding with black.
|
||||
|
||||
:param img: The image in BGR format to be uniformed.
|
||||
:return: The uniformed image in BGR format.
|
||||
"""
|
||||
UNI_HW: int = 512
|
||||
|
||||
# Calculate the new width and height for given image
|
||||
h, w = img.shape[:2]
|
||||
scale = min(UNI_HW / w, UNI_HW / h)
|
||||
new_w = int(w * scale)
|
||||
new_h = int(h * scale)
|
||||
|
||||
# Resize the image
|
||||
resized_img = cv.resize(img, (new_w, new_h))
|
||||
|
||||
# Create a black canvas of size UNI_HW x UNI_HW
|
||||
padded_img = np.zeros((UNI_HW, UNI_HW, 3), dtype=np.uint8)
|
||||
|
||||
# Calculate position to paste the resized image (centered)
|
||||
y_offset = (UNI_HW - new_h) // 2
|
||||
x_offset = (UNI_HW - new_w) // 2
|
||||
|
||||
# Paste the resized image onto the canvas
|
||||
padded_img[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized_img
|
||||
|
||||
# Return the padded image
|
||||
return padded_img
|
||||
|
||||
|
||||
def extract_car_plate(img: cv.typing.MatLike) -> typing.Optional[cv.typing.MatLike]:
|
||||
"""Extract the car plate part from given image.
|
||||
|
||||
:param img: The image containing car plate in BGR format.
|
||||
:return: The image of binary car plate in U8 format if succeed, otherwise None.
|
||||
"""
|
||||
# Reference: https://www.cnblogs.com/linuxAndMcu/p/19144795
|
||||
# ── 1. 利用蓝色车牌颜色在 HSV 空间定位车牌 ──────────────────────────
|
||||
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
|
||||
# 中国蓝牌 HSV 范围
|
||||
lower_blue = np.array([100, 80, 60])
|
||||
upper_blue = np.array([130, 255, 255])
|
||||
mask_blue = cv.inRange(hsv, lower_blue, upper_blue)
|
||||
|
||||
# Resize the image to make following step works about finding proper contours.
|
||||
img = _uniform_car_plate(img)
|
||||
|
||||
# Convert to grayscale image
|
||||
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
||||
# 形态学:闭运算填孔 + 开运算去噪
|
||||
kernel_close = cv.getStructuringElement(cv.MORPH_RECT, (25, 10))
|
||||
kernel_open = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
|
||||
mask_blue = cv.morphologyEx(mask_blue, cv.MORPH_CLOSE, kernel_close)
|
||||
mask_blue = cv.morphologyEx(mask_blue, cv.MORPH_OPEN, kernel_open)
|
||||
|
||||
# Histogram balance to increase contrast
|
||||
hist_gray = cv.equalizeHist(gray)
|
||||
|
||||
# Apply Gaussian blur to reduce noise
|
||||
blurred = cv.GaussianBlur(hist_gray, (5, 5), 0)
|
||||
|
||||
# Edge detection using Canny
|
||||
edges = cv.Canny(blurred, 50, 150)
|
||||
# ── 2. 连通域分析,筛选最符合车牌长宽比的区域 ──────────────────────
|
||||
num_labels, labels, stats, _ = cv.connectedComponentsWithStats(mask_blue, connectivity=8)
|
||||
|
||||
# cv.imshow('contours', edges)
|
||||
# k = cv.waitKey(0)
|
||||
# return None
|
||||
|
||||
# Morphological operations to connect broken edges
|
||||
kernel_close = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
|
||||
closed = cv.morphologyEx(edges, cv.MORPH_CLOSE, kernel_close)
|
||||
kernel_open = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
|
||||
opened = cv.morphologyEx(closed, cv.MORPH_OPEN, kernel_open)
|
||||
kernel_dilate = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
|
||||
dilated = cv.dilate(edges, kernel_dilate, iterations=2)
|
||||
best = None
|
||||
best_score = 0
|
||||
h_img, w_img = img.shape[:2]
|
||||
|
||||
cv.imshow('contours', opened)
|
||||
k = cv.waitKey(0)
|
||||
return None
|
||||
|
||||
# Find contours
|
||||
contours, _ = cv.findContours(closed, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
||||
if not contours:
|
||||
logging.error("No contours found")
|
||||
return None
|
||||
# List all contours
|
||||
logging.debug(f'Total {len(contours)} contours.')
|
||||
for i, contour in enumerate(contours):
|
||||
logging.debug(f'Contour[{i}] has {contour.shape[0]} points.')
|
||||
|
||||
cv.drawContours(img, contours, -1, (0, 0, 255), 3)
|
||||
cv.imshow('contours', img)
|
||||
k = cv.waitKey(0)
|
||||
return None
|
||||
|
||||
# Filter contours
|
||||
candidates: list[cv.typing.MatLike] = []
|
||||
MIN_AREA: float = 2000
|
||||
MAX_AREA: float = 100000
|
||||
MIN_ASPECT_RATIO: float = 2.5
|
||||
MAX_ASPECT_RATIO: float = 6.0
|
||||
|
||||
for i, contour in enumerate(contours):
|
||||
# Calculate the area
|
||||
area = cv.contourArea(contour)
|
||||
if area < MIN_AREA or area > MAX_AREA:
|
||||
logging.debug(f'Contour[{i}] failed at area limit. The area of this contour is {area}.')
|
||||
for i in range(1, num_labels):
|
||||
x, y, w, h, area = stats[i]
|
||||
if area < 3000:
|
||||
continue
|
||||
ratio = w / (h + 1e-5)
|
||||
# 标准车牌宽高比约 3:1 ~ 5:1
|
||||
if 2.5 < ratio < 6.0:
|
||||
score = area * (1 - abs(ratio - 3.5) / 3.5)
|
||||
if score > best_score:
|
||||
best_score = score
|
||||
best = (x, y, w, h)
|
||||
|
||||
# Get bounding rectangle
|
||||
bouding_rect = cv.boundingRect(contour)
|
||||
(x, y, w, h) = bouding_rect
|
||||
# Calaulate aspect ratio
|
||||
aspect_ratio = w / h
|
||||
if aspect_ratio < MIN_ASPECT_RATIO or aspect_ratio > MAX_ASPECT_RATIO:
|
||||
logging.debug(f'Contour[{i}] failed at aspect ratio limit. The aspect ratio of this contour is {aspect_ratio}.')
|
||||
continue
|
||||
assert best is not None, "未找到车牌区域"
|
||||
x, y, w, h = best
|
||||
# 稍微扩边
|
||||
pad = 6
|
||||
x1 = max(x - pad, 0)
|
||||
y1 = max(y - pad, 0)
|
||||
x2 = min(x + w + pad, w_img)
|
||||
y2 = min(y + h + pad, h_img)
|
||||
|
||||
# Get the convex hull of contour
|
||||
hull = cv.convexHull(contour)
|
||||
|
||||
# Compute the occupation of contour area in convex hull area
|
||||
hull_area = cv.contourArea(hull)
|
||||
solidity = area / hull_area
|
||||
plate_color = img[y1:y2, x1:x2].copy()
|
||||
print(f"车牌区域: x={x1}, y={y1}, w={x2-x1}, h={y2-y1}")
|
||||
|
||||
# Filter more regular contour
|
||||
if solidity > 0.6:
|
||||
# Extra check for the rectangle fill rate
|
||||
fill_ratio = area / (w * h)
|
||||
if fill_ratio > 0.3:
|
||||
logging.debug(f'Contour[{i}] is perfect.')
|
||||
candidates.append(contour)
|
||||
continue
|
||||
else:
|
||||
logging.debug(f'Contour[{i}] failed at rectangle fill ratio limit. The fill ratio of this contour is {fill_ratio}')
|
||||
else:
|
||||
logging.debug(f'Contour[{i}] failed at solidity limit. The solidity of this contour is {solidity}.')
|
||||
# # 在原图上标记(仅供调试)
|
||||
# debug = img.copy()
|
||||
# cv.rectangle(debug, (x1, y1), (x2, y2), (0, 255, 0), 3)
|
||||
# cv.imwrite('./debug_detected.jpg', debug)
|
||||
|
||||
if len(candidates) == 0:
|
||||
logging.error("No candidate contour")
|
||||
return None
|
||||
|
||||
cv.drawContours(img, contours, -1, (0, 0, 255), 3)
|
||||
cv.imshow('contours', img)
|
||||
k = cv.waitKey(0)
|
||||
return None
|
||||
|
||||
# ── 3. 二值化:文字/边缘 → 黑色,背景 → 白色 ─────────────────────
|
||||
gray = cv.cvtColor(plate_color, cv.COLOR_BGR2GRAY)
|
||||
|
||||
# 高斯模糊降噪
|
||||
blurred = cv.GaussianBlur(gray, (3, 3), 0)
|
||||
|
||||
# Step 6: Find the most likely license plate contour
|
||||
# License plates are typically rectangular with specific aspect ratios
|
||||
max_area = 0
|
||||
best_contour = None
|
||||
|
||||
for contour in contours:
|
||||
area = cv.contourArea(contour)
|
||||
if area < 500: # Filter out small contours
|
||||
continue
|
||||
|
||||
# Approximate the contour
|
||||
peri = cv.arcLength(contour, True)
|
||||
approx = cv.approxPolyDP(contour, 0.02 * peri, True)
|
||||
|
||||
# Look for quadrilateral shapes (4 corners)
|
||||
if len(approx) == 4:
|
||||
x, y, w, h = cv.boundingRect(contour)
|
||||
aspect_ratio = float(w) / h if h > 0 else 0
|
||||
|
||||
# Typical license plate aspect ratio is between 2 and 5
|
||||
if 2 <= aspect_ratio <= 5 and area > max_area:
|
||||
max_area = area
|
||||
best_contour = approx
|
||||
|
||||
if best_contour is None:
|
||||
# If no perfect quadrilateral found, try with largest rectangular contour
|
||||
for contour in contours:
|
||||
area = cv.contourArea(contour)
|
||||
if area < 500:
|
||||
continue
|
||||
|
||||
x, y, w, h = cv.boundingRect(contour)
|
||||
aspect_ratio = float(w) / h if h > 0 else 0
|
||||
|
||||
if 1.5 <= aspect_ratio <= 6 and area > max_area:
|
||||
max_area = area
|
||||
rect = cv.minAreaRect(contour)
|
||||
box = cv.boxPoints(rect)
|
||||
best_contour = np.int0(box)
|
||||
|
||||
if best_contour is None:
|
||||
logging.error("No valid contour found")
|
||||
return None
|
||||
|
||||
# Step 7: Perspective transformation to get front view
|
||||
# Order points: top-left, top-right, bottom-right, bottom-left
|
||||
pts = best_contour.reshape(4, 2)
|
||||
rect = np.zeros((4, 2), dtype="float32")
|
||||
|
||||
# Sum and difference of coordinates to find corners
|
||||
s = pts.sum(axis=1)
|
||||
rect[0] = pts[np.argmin(s)] # Top-left has smallest sum
|
||||
rect[2] = pts[np.argmax(s)] # Bottom-right has largest sum
|
||||
|
||||
diff = np.diff(pts, axis=1)
|
||||
rect[1] = pts[np.argmin(diff)] # Top-right has smallest difference
|
||||
rect[3] = pts[np.argmax(diff)] # Bottom-left has largest difference
|
||||
|
||||
# Calculate width and height of new image
|
||||
width_a = np.linalg.norm(rect[0] - rect[1])
|
||||
width_b = np.linalg.norm(rect[2] - rect[3])
|
||||
max_width = max(int(width_a), int(width_b))
|
||||
|
||||
height_a = np.linalg.norm(rect[0] - rect[3])
|
||||
height_b = np.linalg.norm(rect[1] - rect[2])
|
||||
max_height = max(int(height_a), int(height_b))
|
||||
|
||||
# Destination points for perspective transform
|
||||
dst_pts = np.array([
|
||||
[0, 0],
|
||||
[max_width - 1, 0],
|
||||
[max_width - 1, max_height - 1],
|
||||
[0, max_height - 1]
|
||||
], dtype="float32")
|
||||
|
||||
# Get perspective transform matrix and apply it
|
||||
M = cv.getPerspectiveTransform(rect, dst_pts)
|
||||
warped = cv.warpPerspective(img, M, (max_width, max_height))
|
||||
# Otsu 自动阈值,得到白字黑底,再取反 → 黑字白底
|
||||
_, binary_otsu = cv.threshold(blurred, 0, 255,
|
||||
cv.THRESH_BINARY + cv.THRESH_OTSU)
|
||||
binary = cv.bitwise_not(binary_otsu) # 反转:字符变黑,背景变白
|
||||
|
||||
# 去除小噪点(开运算)
|
||||
kernel_denoise = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
|
||||
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel_denoise)
|
||||
|
||||
return binary
|
||||
# cv.imwrite('./plate_binary.png', binary)
|
||||
# print("二值化结果已保存: plate_binary.png")
|
||||
|
||||
# ── 4. 叠加边框轮廓(细化文字边缘,参考效果图)─────────────────────
|
||||
# Canny 边缘叠加让效果更接近参考图
|
||||
edges = cv.Canny(blurred, 40, 120)
|
||||
edges_inv = cv.bitwise_not(edges) # 边缘→黑色
|
||||
combined = cv.bitwise_and(binary, edges_inv) # 合并
|
||||
# 再做一次轻微腐蚀让字体略粗
|
||||
kernel_dilate = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
|
||||
combined = cv.erode(combined, kernel_dilate, iterations=1)
|
||||
|
||||
return combined
|
||||
# cv.imwrite('./plate_final.png', combined)
|
||||
# print("最终结果已保存: plate_final.png")
|
||||
|
||||
# Step 8: Convert warped image to grayscale
|
||||
warped_gray = cv.cvtColor(warped, cv.COLOR_BGR2GRAY)
|
||||
|
||||
# Step 9: Apply adaptive thresholding for better binarization
|
||||
# First, enhance contrast
|
||||
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
||||
enhanced = clahe.apply(warped_gray)
|
||||
|
||||
# Apply Otsu's thresholding to get binary image
|
||||
_, binary = cv.threshold(enhanced, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
|
||||
|
||||
# Step 10: Clean up the binary image with morphological operations
|
||||
kernel_clean = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
|
||||
cleaned = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel_clean)
|
||||
|
||||
# Ensure the output is in the correct format (U8)
|
||||
result = cleaned.astype(np.uint8)
|
||||
|
||||
return result
|
||||
|
||||
@dataclass
|
||||
class Cli:
|
||||
|
||||
Reference in New Issue
Block a user