【问题标题】:Why does python OpenCV GUI crashes in simple video circle detection program on linux?为什么 python OpenCV GUI 在 linux 上的简单视频圆圈检测程序中崩溃?
【发布时间】:2017-05-05 19:17:46
【问题描述】:

Snapshot of the issue我一直在尝试检测视频中的圆圈。我检查了许多教程和 stackoverflow 问题,我的代码似乎是正确的。它甚至可以正确编译。然而,打开 GUI 窗口需要时间,而且一旦打开,它就会崩溃。这是我的代码有什么问题吗?

`

import cv2
import numpy as np

cap  = cv2.VideoCapture('Red Motion Spin Looping Motion Background.mp4')
while (cap.isOpened()):
    ret,img = cap.read()
    img = cv2.medianBlur(img,5) 
    cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    circles = cv2.HoughCircles(cimg,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
            # draw the outer circle
            cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
            # draw the center of the circle
            cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

    cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

` 更新:我听从了@Micka 的建议,窗口出现了。但是它花了很长时间才打开,并且没有超出视频的第一帧screenshot of the current situation

【问题讨论】:

  • 欢迎来到 SO!。请阅读此how-to-ask 以完善您的问题,例如显示回溯(如果有)。
  • 有错误信息吗?捕获后:在继续之前检查 img 是否为空。找出它在哪一行崩溃(例如,在每行之前打印一些内容)。
  • @Micka 我将通过编辑问题发布一张图片说明它的作用。它只说初始化完成,之后什么也不做。我圈出了 GUI
  • @thewaywewere 感谢您的意见。我已经编辑了我的问题
  • 什么样的gui?你的意思是opencv窗口吗?将cv2.waitKey(1)放在imshow之后的循环中,否则什么都不会显示(opencv会在waitkey时间内渲染)。

标签: python opencv computer-vision geometry hough-transform


【解决方案1】:

下面是用Python 2.7.12OpenCV3.2.0 测试的可行代码。

import numpy as np
import cv2

capture = cv2.VideoCapture("001.mp4")
#capture = cv2.VideoCapture(0)

while capture.isOpened():
    # grab the current frame and initialize the status text
    grabbed, frame = capture.read()

    if frame is not None:
        # convert the frame to grayscale, blur it, and detect circles
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        blur = cv2.medianBlur(gray,5) 
        circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,1,20, \
                                   param1=50,param2=30,minRadius=0,maxRadius=0)

        if circles is not None:
            # convert the (x, y) coordinates and radius of the circles to integers
            circles = np.round(circles[0, :]).astype("int")
            #circles = np.uint16(np.around(circles[0,:]))

            # loop over the (x, y) coordinates and radius of the circles
            for (x, y, r) in circles:
                # draw the circle in the output image, then draw a rectangle
                # corresponding to the center of the circle
                cv2.circle(frame, (x, y), r, (255, 0, 255), 2)

            # show the frame and record if a key is pressed
            cv2.imshow("Frame", frame)
            # if the 'q' key is pressed, stop the loop
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

capture.release()
cv2.destroyAllWindows()


主要变化在于for (x, y, r) in circles: 循环以获取和绘制圆圈。添加cv2.medianBlur()后,视频播放有点慢。通过cv2.HoughCircles() 检测,它甚至进一步减慢了速度。

这是带有圆圈的视频播放屏幕截图。假设您可能需要修改cv2.HoughCircles()函数参数和圈子检索以满足您的要求。

希望对您有所帮助。

【讨论】:

  • 会不会是因为我用的是虚拟机所以很慢?因为@thewaywere 我尝试将我的代码修改为您的代码,但在显示第一帧后打开和崩溃仍然很慢
  • @Hamza 使用 VM 肯定会减慢速度。但是,问题似乎在原始代码上。它没有在我的 Win10 PC 上运行和完成,我需要终止会话。如前所述,您可能需要调整 cv2.HoughCircles() 参数,例如“minRadius”和“maxRadius”。
  • @Hamza 顺便说一下,请阅读这个"What should I do when someone answers my question?"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多