python - Python OPenCV Hough Circle 需要很长时间才能从网络摄像头加载图像
问题描述
我正在尝试从网络摄像头获取视频流并使用HoughCircles()
. 但是,当我尝试运行代码时,视频会花费时间来加载图像,或者根本不会加载图像。任何有关如何让代码进行圆检测的帮助将不胜感激。
注意:我没有尝试实时做任何事情。我只想获得一些基本的圆圈检测来处理来自我的网络摄像头的视频流。
这是代码:
import cv2
import numpy as np
import sys
cap = cv2.VideoCapture(0)
width = 320
height = 240
dim = (width, height)
while(True):
gray = cv2.medianBlur(cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY),5)
resized = cv2.resize(gray,dim,interpolation = cv2.INTER_AREA)
edges = cv2.Canny(gray,100,200)
circ = cv2.HoughCircles(resized,cv2.HOUGH_GRADIENT,1,30,param1=50,param2=75,
minRadius=0,maxRadius=0)
cv2.imshow('video',resized)
if circ is not None:
circ = np.uint16(np.around(circ))[0,:]
print(circ)
for j in circ:
cv2.circle(resized, (j[0], j[1]), j[2], (0, 255, 0), 2)
cv2.imshow('video',resized)
if cv2.waitKey(1)==27:# esc Key
break
cap.release()
cv2.destroyAllWindows()
再次感谢您的帮助。
解决方案
好的,我已经想通了。我不得不减少HoughCircles
. 我已更改circ = cv2.HoughCircles(resized,cv2.HOUGH_GRADIENT,1,30,param1=50,param2=75, minRadius=0,maxRadius=0)
为cv2.HoughCircles(resized,cv2.HOUGH_GRADIENT,1,50,param1=50,param2=35, minRadius=0,maxRadius=0)
允许代码显示视频流,同时以合理的帧速率检测圆圈。还要感谢@ZdaR 的帮助。
这是有效的代码
import cv2
import numpy as np
import serial
cap = cv2.VideoCapture(1)
width = 320
height = 240
dim = (width, height)
while(True):
gray = cv2.medianBlur(cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY),5)
resized = cv2.resize(gray,dim,interpolation = cv2.INTER_AREA)
circ = cv2.HoughCircles(resized,cv2.HOUGH_GRADIENT,1,50,param1=50,param2=35,
minRadius=0,maxRadius=0)
cv2.imshow('video',resized)
if circ is not None:
circ = np.uint16(np.around(circ))[0,:]
print(circ)
for j in circ:
cv2.circle(resized, (j[0], j[1]), j[2], (0, 255, 0), 2)
cv2.imshow('video',resized)
if cv2.waitKey(1)==27:# esc Key
break
cap.release()
cv2.destroyAllWindows()
推荐阅读
- ruby-on-rails - 评论部分将最新的评论排序在顶部而不是底部
- vuex - 从另一个文件 nuxt 提交突变
- intellij-idea - Intellij Idea 中的“使用 dartfmt 重新格式化代码”操作 - 收到“无法重新格式化代码”消息
- java - Spring Boot 应用程序 - Tomcat 部署 - 无法确定合适的驱动程序类
- java - 将一个类对象转换为另一种类型时编译时 ClassCastException 和错误之间的区别
- swift - 将 CIImage 乘以标量
- watson-assistant - Watson Assistant 无法显示我电脑中的图像
- javascript - 制表符:groupToggleElement 打破布局
- graphql - Apollo Client 从响应对象中去除额外的结果
- excel - 如何使用或过滤器从 Excel 中的 OLAP 多维数据集数据透视表中获取数据