【python】15行代码实现猫脸检测(opencv)

在这里插入图片描述


1. 项目简介

利用opecvpython库及训练好的级联分类器实现猫脸检测。

2. 项目地址

https://github.com/XavierJiezou/opecv-face-detect

3. 依赖模块

pip install opencv-python

4. 完整代码

import cv2

def face_detect(file_name, cascade_name):
    img = cv2.imread(file_name)  # 读取图片
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 图片灰度化
    img_gray = cv2.equalizeHist(img_gray)  # 直方图均衡化
    face_cascade = cv2.CascadeClassifier(cascade_name)  # 加载级联分类器
    faces = face_cascade.detectMultiScale(img_gray)  # 多尺度检测
    for (x, y, w, h) in faces:  # 遍历所有检测到的猫脸
        img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 255), 5)  # 绘制矩形框
    cv2.imshow('Face detection', img)  # 检测效果预览
    cv2.waitKey(0)  # 保持窗口显示

if __name__ == "__main__":
    face_detect('test.jpg', 'haarcascade_frontalcatface.xml')

5. 必要组件

opencv官方提供了3个已经训练好的猫脸级联分类文件:

5.1. haar级联特征分类器(精度高)

5.2. lbp级联特征分类器(速度快)


frontalface对正脸检测效果好。一般来说,haar特征检测精度更高,而lbp特征检测用时更短。

6. 成果展示

6.1. 测试样例1

  • haar

在这里插入图片描述

  • lbp

在这里插入图片描述

6.2. 测试样例2

  • haar

在这里插入图片描述

  • lbp(没检测到/(ㄒoㄒ)/~~)

在这里插入图片描述

6.3. 测试样例3

  • haar

在这里插入图片描述

  • lbp(没检测到(;′⌒`))

在这里插入图片描述

7. 对比分析

从测试结果来看,haar级联特征的猫脸检测的精度还可以,但lbp就不行,有的图片根本检测不到猫。

8. 引用参考

https://docs.opencv.org/master/db/d28/tutorial_cascade_classifier.html
https://docs.opencv.org/master/d2/d99/tutorial_js_face_detection.html

9. 相关推荐

【python】15行代码实现动漫人脸检测(opencv)

【python】15行代码实现人脸检测(opencv)

【python】30行代码实现视频中的动漫人脸检测(opencv)

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
霍夫直线检测(Hough Line Transform)是一种经典的图像处理算法,用于检测图像中的直线。下面是一个使用 Python 实现的简单示例代码,不调用 OpenCV 库: ```python import numpy as np import math def hough_lines(image): # 定义参数 theta_res = 1 # 角度步长 rho_res = 1 # 距离步长 threshold = 50 # 阈值 theta_num = int(180 / theta_res) rows, cols = image.shape # 计算图像的最大距离 max_distance = int(math.sqrt(rows**2 + cols**2)) # 构造累加器矩阵 accumulator = np.zeros((int(max_distance / rho_res), theta_num), dtype=np.uint8) # 遍历图像中所有非零像素点 for r in range(rows): for c in range(cols): if image[r][c] > 0: for t in range(theta_num): theta = t * theta_res * math.pi / 180.0 rho = int((c * math.cos(theta) + r * math.sin(theta)) / rho_res + 0.5) # 累加器加一 accumulator[rho][t] += 1 # 获取直线的参数 lines = [] for r in range(int(max_distance / rho_res)): for t in range(theta_num): if accumulator[r][t] > threshold: a = math.cos(t * theta_res * math.pi / 180.0) b = math.sin(t * theta_res * math.pi / 180.0) x0 = a * r * rho_res y0 = b * r * rho_res x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) lines.append((x1, y1, x2, y2)) return lines ``` 该代码实现了一个简单的霍夫直线检测算法,输入参数为二值化图像,返回值为检测到的直线参数。注意,该代码实现的霍夫直线检测算法可能存在一定的性能问题,仅用于学习和实验之用。如果需要高性能和高精度的霍夫直线检测算法,建议使用 OpenCV 库提供的相关函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xavier Jiezou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值