前言
数美推理验证码还能这么玩,看了很多题目都是选最小的××颜色××物体


这答案选啥?写到脸上了
处理思路
法一:
直接用目标检测,然后求面积最小最小值。
法二:
我们发现背景的物体大多都是彩色的,离不开红橙黄绿蓝靛紫,那么我们我直接定义一个HSV 色彩空间,将背景中的这些彩色物体给他干出来就行了。
- 定义彩色空间(注:opencv的HSV与普通的HSV不同,需要换算得到):
color_limits = [
(np.array([0, 100, 100]), np.array([10, 255, 255])),
(np.array([160, 100, 100]), np.array([180, 255, 255])),
(np.array([11, 100, 100]), np.array([25, 255, 255])),
(np.array([26, 100, 100]), np.array([35, 255, 255])),
(np.array([36, 100, 100]), np.array([85, 255, 255])),
(np.array([86, 100, 100]), np.array([100, 255, 255])),
(np.array([101, 100, 100]), np.array([130, 255, 255])),
(np.array([131, 100, 100]), np.array([160, 255, 255]))
]
-
整张和原图一模一样的空白掩码,然后将符合HSV的给他干上
mask_total = np.zeros(hsv_img.shape[:2], dtype=np.uint8) for low, high in color_limits: mask_total = mask_total | cv2.inRange(hsv_img, low, high)

- cv2提轮廓
kernel = np.ones((5, 5), np.uint8)
clean_mask = cv2.morphologyEx(mask_total, cv2.MORPH_OPEN, kernel)
contours_found, _ = cv2.findContours(clean_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
out_img = img.copy()
sm_area = None
ct = None
smallest_center = None
for iii, cnt in enumerate(contours_found):
a = cv2.contourArea(cnt)
if a < 100:
continue
if sm_area is None or a < sm_area:
sm_area = a
ct = cnt
M = cv2.moments(cnt)
if M["m00"] != 0:
smallest_center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
else:
smallest_center = None
cv2.drawContours(out_img, [cnt], -1, (0, 0, 255), 2)
M = cv2.moments(cnt)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
cv2.putText(out_img, str(iii + 1), (cx - 10, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2)
if ct is not None:
cv2.drawContours(out_img, [ct], -1, (0, 255, 0), 3)
if smallest_center:
cv2.putText(out_img, "MIN", (smallest_center[0] - 20, smallest_center[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (100, 100, 0), 2)
print(f"最小面积是: {sm_area:.2f} 像素")
print(f"最小面积的中心点: {smallest_center}")
else:
print("erro")
结果

付费的训练框架,包括但不限于验证码,轨迹,预测。
863

被折叠的 条评论
为什么被折叠?



