Python+OpenCV识别图片中是否有红章

import matplotlib.pyplot as plt
import cv2
import numpy as np

img = cv2.imread("4.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

lower_blue = np.array([100, 30, 100])
upper_blue = np.array([150, 255, 255])

mask = cv2.inRange(img_hsv, lower_blue, upper_blue)

res = cv2.bitwise_and(img, img, mask=mask)
r, g, b = cv2.split(res)
r_num = 0
for i in b:
    for j in i:
        if j > 170:
            r_num += 1
# cv2.imshow('img', img)
# cv2.imshow("mask", mask)
cv2.imshow("res", res)

if(r_num>30):
    print("该图片有红章")
else:
    print("该图片没有红章")
print(r_num)
cv2.waitKey(0)

这里给出几个自己的例子
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,我了解了你的问题。关于利用 Python + OpenCV 识别图片的圆形(霍夫变换),我可以给你一些详细的解释和代码示例。 首先,我们需要导入 OpenCV 库和 NumPy 库。这里需要注意的是,需要安装正确的 OpenCV 版本,否则可能会出现一些问题。 ```python import cv2 import numpy as np ``` 接下来,我们需要读取图片并将其转换为灰度图像。然后,我们可以对灰度图像进行高斯模糊处理,以减少噪声。这里的高斯核大小可以根据实际情况进行调整。 ```python img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) ``` 接着,我们可以使用霍夫变换检测圆形。这里需要注意的是,需要设置最小半径和最大半径的范围,以及检测圆形的参数阈值。这些参数的设置也需要根据实际情况进行调整。 ```python circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=30, minRadius=0, maxRadius=0) ``` 最后,我们可以将检测到的圆形标记出来,并显示图片。 ```python if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(img, (x, y), r, (0, 255, 0), 2) cv2.imshow("Image", img) cv2.waitKey(0) ``` 完整代码如下: ```python import cv2 import numpy as np img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=30, minRadius=0, maxRadius=0) if circles is not None: circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: cv2.circle(img, (x, y), r, (0, 255, 0), 2) cv2.imshow("Image", img) cv2.waitKey(0) ``` 希望这个代码示例能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值