智识之眼:不看题目就能知道答案的空间推理验证码

部署运行你感兴趣的模型镜像

前言

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

在这里插入图片描述
在这里插入图片描述

这答案选啥?写到脸上了

处理思路

法一:

直接用目标检测,然后求面积最小最小值。

法二:

我们发现背景的物体大多都是彩色的,离不开红橙黄绿蓝靛紫,那么我们我直接定义一个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")

结果

在这里插入图片描述

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

您可能感兴趣的与本文相关的镜像

Yolo-v5

Yolo-v5

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值