【问题标题】:Python OPenCV Hough Circle taking a long time to load an image from webcameraPython OPenCV Hough Circle 需要很长时间才能从网络摄像头加载图像
【发布时间】:2018-12-14 00:48:21
【问题描述】:

我正在尝试从网络摄像头获取视频流,并使用 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 中弄乱了一些参数,它起作用了。将其从 640X480 调整为 320X240 显着提高了帧速率。

标签: python opencv image-processing hough-transform


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多