1
0

revoke: revoke persepctive correction code and fix yellow car plate issue

This commit is contained in:
2026-04-09 07:47:20 +08:00
parent 1cf5afcd0f
commit eb43b3df31
2 changed files with 28 additions and 99 deletions

View File

@@ -4,6 +4,7 @@
# All image files
*.jpg
*.jpeg
*.png
*.webp

View File

@@ -49,21 +49,30 @@ def _uniform_car_plate(img: cv.typing.MatLike) -> cv.typing.MatLike:
class CarPlateHsvBoundary:
lower_bound: cv.typing.MatLike
upper_bound: cv.typing.MatLike
need_revert: bool
"""是否取反黑白颜色,因为蓝牌和黄牌的操作正好是反的"""
CAR_PLATE_HSV_BOUNDARIES: tuple[CarPlateHsvBoundary, ...] = (
# 中国蓝牌 HSV 范围
CarPlateHsvBoundary(np.array([100, 80, 60]), np.array([130, 255, 255])),
CarPlateHsvBoundary(np.array([100, 80, 60]), np.array([130, 255, 255]), True),
# 中国绿牌 HSV 范围
CarPlateHsvBoundary(np.array([35, 43, 46]), np.array([99, 255, 255])),
CarPlateHsvBoundary(np.array([35, 43, 46]), np.array([99, 255, 255]), False),
# 中国黄牌 HSV 范围
CarPlateHsvBoundary(np.array([32, 43, 46]), np.array([68, 255, 255])),
CarPlateHsvBoundary(np.array([16, 43, 46]), np.array([34, 255, 255]), False),
)
@dataclass
class CarPlateMask:
mask: cv.typing.MatLike
need_revert: bool
"""是否对颜色取反与CarPlateHsvBoundary中的同名字段含义一致"""
def _batchly_mask_car_plate(
hsv: cv.typing.MatLike,
) -> typing.Iterator[cv.typing.MatLike]:
) -> typing.Iterator[CarPlateMask]:
""" """
for boundary in CAR_PLATE_HSV_BOUNDARIES:
# 以给定HSV范围检测符合该颜色的位置
@@ -76,7 +85,7 @@ def _batchly_mask_car_plate(
mask = cv.morphologyEx(mask, cv.MORPH_OPEN, kernel_open)
# Return value
yield mask
yield CarPlateMask(mask, boundary.need_revert)
@dataclass
@@ -85,6 +94,8 @@ class CarPlateRegion:
y: int
w: int
h: int
need_revert: bool
"""是否对颜色取反与CarPlateHsvBoundary中的同名字段含义一致"""
MIN_AREA: float = 3000
@@ -94,10 +105,12 @@ BEST_ASPECT_RATIO: float = 3.5
def _analyse_car_plate_connection(
mask: cv.typing.MatLike,
mask: CarPlateMask,
) -> typing.Optional[CarPlateRegion]:
# 连通域分析,筛选最符合车牌长宽比的区域
num_labels, labels, stats, _ = cv.connectedComponentsWithStats(mask, connectivity=8)
num_labels, labels, stats, _ = cv.connectedComponentsWithStats(
mask.mask, connectivity=8
)
best: typing.Optional[CarPlateRegion] = None
best_score = 0
@@ -113,77 +126,11 @@ def _analyse_car_plate_connection(
score = area * (1 - abs(ratio - BEST_ASPECT_RATIO) / BEST_ASPECT_RATIO)
if score > best_score:
best_score = score
best = CarPlateRegion(x, y, w, h)
best = CarPlateRegion(x, y, w, h, mask.need_revert)
return best
@dataclass
class PerspectiveData:
top_left: tuple[int, int]
top_right: tuple[int, int]
bottom_left: tuple[int, int]
bottom_right: tuple[int, int]
new_width: int
new_height: int
def _extract_perspective_data(
gray: cv.typing.MatLike,
) -> typing.Optional[PerspectiveData]:
""" """
# 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)
# Find contours
contours, _ = cv.findContours(edges, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
if not contours:
return None
# Find the largest one because all image is car plate
max_area_contour = max(contours, key=lambda contour: cv.contourArea(contour))
# Approximate the contour
peri = cv.arcLength(max_area_contour, True)
approx = cv.approxPolyDP(max_area_contour, 0.02 * peri, True)
if len(approx) != 4:
return None
# Perspective transformation to get front view
# Order points: top-left, top-right, bottom-right, bottom-left
pts = approx.reshape(4, 2)
rect = np.zeros((4, 2), dtype="float32")
# Sum and difference of coordinates to find corners
s = pts.sum(axis=1)
top_left = pts[np.argmin(s)] # Top-left has smallest sum
bottom_right = pts[np.argmax(s)] # Bottom-right has largest sum
diff = np.diff(pts, axis=1)
top_right = pts[np.argmin(diff)] # Top-right has smallest difference
bottom_left = 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))
# Return value
return PerspectiveData(
top_left, top_right, bottom_left, bottom_right, max_width, max_height
)
def extract_car_plate(img: cv.typing.MatLike) -> typing.Optional[cv.typing.MatLike]:
"""
Extract the car plate part from given image.
@@ -232,39 +179,20 @@ def extract_car_plate(img: cv.typing.MatLike) -> typing.Optional[cv.typing.MatLi
# Otsu 自动阈值,得到白字黑底,再取反 → 黑字白底
_, binary_otsu = cv.threshold(blurred, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# 反转:字符变黑,背景变白
binary = cv.bitwise_not(binary_otsu)
if candidate.need_revert:
binary = cv.bitwise_not(binary_otsu)
else:
binary = binary_otsu
# 去除小噪点(开运算)
kernel_denoise = cv.getStructuringElement(cv.MORPH_RECT, (2, 2))
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel_denoise)
# 尝试获取视角矫正数据
perspective_data = _extract_perspective_data(gray)
if perspective_data is None:
logging.warning(f'Can not fetch perspective data. The output image has no perspective correction.')
return binary
# 执行视角矫正
perspective_src = np.array([
list(perspective_data.top_left),
list(perspective_data.top_right),
list(perspective_data.bottom_right),
list(perspective_data.bottom_left)
], dtype="float32")
perspective_dst = np.array([
[0, 0],
[perspective_data.new_width - 1, 0],
[perspective_data.new_width - 1, perspective_data.new_height - 1],
[0, perspective_data.new_height - 1]
], dtype="float32")
M = cv.getPerspectiveTransform(perspective_src, perspective_dst)
warped = cv.warpPerspective(binary, M, (perspective_data.new_width, perspective_data.new_height))
return warped
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) # 边缘→黑色