Compare commits
2 Commits
5392084f0f
...
75f1d58161
| Author | SHA1 | Date | |
|---|---|---|---|
| 75f1d58161 | |||
| 1cf5afcd0f |
1
mv-and-ip/.gitignore
vendored
1
mv-and-ip/.gitignore
vendored
@@ -4,6 +4,7 @@
|
||||
|
||||
# All image files
|
||||
*.jpg
|
||||
*.jpeg
|
||||
*.png
|
||||
*.webp
|
||||
|
||||
|
||||
@@ -49,21 +49,29 @@ 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 +84,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 +93,8 @@ class CarPlateRegion:
|
||||
y: int
|
||||
w: int
|
||||
h: int
|
||||
need_revert: bool
|
||||
"""是否对颜色取反,与CarPlateHsvBoundary中的同名字段含义一致"""
|
||||
|
||||
|
||||
MIN_AREA: float = 3000
|
||||
@@ -94,10 +104,10 @@ 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,7 +123,7 @@ 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
|
||||
|
||||
@@ -136,10 +146,11 @@ def extract_car_plate(img: cv.typing.MatLike) -> typing.Optional[cv.typing.MatLi
|
||||
# 连通域分析,筛选最符合车牌长宽比的区域作为车牌
|
||||
candidate = _analyse_car_plate_connection(mask)
|
||||
# 找到任意一个就退出
|
||||
if candidate is not None: break
|
||||
if candidate is not None:
|
||||
break
|
||||
|
||||
if candidate is None:
|
||||
logging.error('Can not find any car plate.')
|
||||
logging.error("Can not find any car plate.")
|
||||
return None
|
||||
|
||||
# 稍微扩边获取最终车牌区域
|
||||
@@ -149,7 +160,7 @@ def extract_car_plate(img: cv.typing.MatLike) -> typing.Optional[cv.typing.MatLi
|
||||
y1 = max(candidate.y - pad, 0)
|
||||
x2 = min(candidate.x + candidate.w + pad, w_img)
|
||||
y2 = min(candidate.y + candidate.h + pad, h_img)
|
||||
logging.info(f'车牌区域: x={x1}, y={y1}, w={x2 - x1}, h={y2 - y1}')
|
||||
logging.info(f"车牌区域: x={x1}, y={y1}, w={x2 - x1}, h={y2 - y1}")
|
||||
|
||||
# # 在原图上标记(仅供调试)
|
||||
# debug = img.copy()
|
||||
@@ -165,17 +176,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)
|
||||
|
||||
#return binary
|
||||
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) # 边缘→黑色
|
||||
|
||||
Reference in New Issue
Block a user