为什么python OpenCV GUI在linux上的简单视频圈检测程序中崩溃?
我一直在尝试检测视频中的圆圈。我检查了许多教程和stackoverflow问题,我的代码似乎是正确的。它甚至可以正确编译。然而,打开GUI窗口需要时间,一旦打开,它就会崩溃。这是我的代码有什么问题吗 ` `为什么python OpenCV GUI在linux上的简单视频圈检测程序中崩溃?,python,opencv,computer-vision,geometry,hough-transform,Python,Opencv,Computer Vision,Geometry,Hough Transform,我一直在尝试检测视频中的圆圈。我检查了许多教程和stackoverflow问题,我的代码似乎是正确的。它甚至可以正确编译。然而,打开GUI窗口需要时间,一旦打开,它就会崩溃。这是我的代码有什么问题吗 ` ` 更新:我听从了@Mika的建议,窗口出现了。然而,它花了很长时间才打开,并且没有超出视频的第一帧下面是一段用Python 2.7.12和OpenCV3.2.0测试过的可行代码 import numpy as np import cv2 capture = cv2.VideoCapture(
更新:我听从了@Mika的建议,窗口出现了。然而,它花了很长时间才打开,并且没有超出视频的第一帧下面是一段用
Python 2.7.12
和OpenCV3.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()
主要的变化是圆中(x,y,r)的
循环:
获取并绘制圆。一旦添加cv2.medianBlur()
,视频播放会有点慢。通过cv2.HoughCircles()
检测,它甚至进一步减慢了速度
下面是视频播放的屏幕截图。假设您可能需要修改cv2.HoughCircles()
函数参数和圆检索以满足您的要求
希望能得到帮助。下面是一段经过
Python 2.7.12
和OpenCV3.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()
主要的变化是圆中(x,y,r)的
循环:
获取并绘制圆。一旦添加cv2.medianBlur()
,视频播放会有点慢。通过cv2.HoughCircles()
检测,它甚至进一步减慢了速度
下面是视频播放的屏幕截图。假设您可能需要修改cv2.HoughCircles()
函数参数和圆检索以满足您的要求
希望对您有所帮助。欢迎使用SO!。请阅读此文以完善您的问题,如有回溯,请显示回溯。是否有任何错误消息?捕获后:继续之前检查img是否为空。找出它在哪一行崩溃(例如,在每一行之前打印一些东西)。@Mika我将通过编辑问题来发布一张它所做事情的图片。它只说init完成了,之后什么也不做。我绕着飞机转了一圈GUI@thewaywewere谢谢你的意见。我已经编辑了我的问题什么样的gui?你是说opencv窗口吗?在imshow之后将
cv2.waitKey(1)
放入循环中,否则将完全不显示任何内容(opencv将在waitKey时间内呈现)。欢迎使用SO!。请阅读此文以完善您的问题,如有回溯,请显示回溯。是否有任何错误消息?捕获后:继续之前检查img是否为空。找出它在哪一行崩溃(例如,在每一行之前打印一些东西)。@Mika我将通过编辑问题来发布一张它所做事情的图片。它只说init完成了,之后什么也不做。我绕着飞机转了一圈GUI@thewaywewere谢谢你的意见。我已经编辑了我的问题什么样的gui?你是说opencv窗口吗?在imshow之后将cv2.waitKey(1)
放在循环中,否则什么都不会显示(opencv将在waitKey时间内渲染)。这可能是因为我使用的是虚拟机,速度非常慢吗?because@thewaywewere我尝试将我的代码修改为您的代码,但它仍然非常缓慢地打开,并且在显示第一个代码后崩溃frame@Hamza使用虚拟机的速度肯定会减慢。然而,问题似乎出现在原始代码上。它没有在我的Win10 PC上运行并完成,我需要终止会话。如前所述,您可能需要调整cv2.HoughCircles()
参数,如“minRadius”和“maxRadius”。@Hamza顺便说一句,请阅读此文。可能是因为我使用的是虚拟机,速度非常慢吗?because@thewaywewere我尝试将我的代码修改为您的代码,但它仍然非常缓慢地打开,并且在显示第一个代码后崩溃frame@Hamza使用虚拟机的速度肯定会减慢。然而,问题似乎出现在原始代码上。它没有在我的Win10 PC上运行并完成,我需要终止会话。如前所述,您可能需要调整cv2.HoughCircles()
参数,如“minRadius”和“maxRadius”。@Hamza顺便说一下,请阅读此内容。