首页 > 解决方案 > 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()

再次感谢您的帮助。

标签: pythonopencvimage-processinghough-transform

解决方案


好的,我已经想通了。我不得不减少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()

推荐阅读