在OpenCV python上执行Hough圆变换时出错

在OpenCV python上执行Hough圆变换时出错,python,opencv,hough-transform,Python,Opencv,Hough Transform,下面的代码旨在对视频执行Hough圆变换。当从视频中提取帧并执行Hough圆变换时,代码按预期工作。但是,当我尝试复制类似的方法来生成视频时,我得到了ufunc的这个错误循环不支持类型为NoneType的参数0,该参数没有可调用的rint方法。此错误的原因似乎在while循环下面。这个问题的原因可能是什么 非常感谢 更新:我已将模糊=cv2.medianBlurdframe,25更改为模糊=cv2.GaussianBlurdframe,11,11,0。这似乎有一段时间是可行的,但随后代码崩溃,给

下面的代码旨在对视频执行Hough圆变换。当从视频中提取帧并执行Hough圆变换时,代码按预期工作。但是,当我尝试复制类似的方法来生成视频时,我得到了ufunc的这个错误循环不支持类型为NoneType的参数0,该参数没有可调用的rint方法。此错误的原因似乎在while循环下面。这个问题的原因可能是什么

非常感谢

更新:我已将模糊=cv2.medianBlurdframe,25更改为模糊=cv2.GaussianBlurdframe,11,11,0。这似乎有一段时间是可行的,但随后代码崩溃,给了我同样的错误

import numpy as np 
import cv2 
import matplotlib.pyplot as plt 

np.random.seed(42)

def fixColor(image):
    return(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
    
video = cv2.VideoCapture("video.wmv")

#randomly select frames in an array 
frameIds = video.get(cv2.CAP_PROP_FRAME_COUNT) *np.random.uniform(size = 55)

#store selected frames in an array 
frames = []
for fid in frameIds:
    video.set(cv2.CAP_PROP_FRAME_COUNT,fid)
    ret, frame = video.read()
    frames.append(frame)
    
video.release()

#calculate the median frame
medianFrame = np.median(frames,axis=0).astype(dtype=np.uint8)

#random sample 
sample_frame = frames[0]

#convert to grayscale
grayMedianFrame = cv2.cvtColor(medianFrame, cv2.COLOR_BGR2GRAY)
graySample = cv2.cvtColor(sample_frame, cv2.COLOR_BGR2GRAY)

#subtract grayscale frames between sample and median
dframe = cv2.absdiff(graySample, grayMedianFrame)

blurred = cv2.medianBlur(dframe, 25)
#plt.imshow(fixColor(blurred))

#Hough circle 
circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1,120,param1= 50, param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))

for i in circles[0, :]:   
    # draw outer circle
    cv2.circle(sample_frame, (i[0], i[1]), i[2], (0, 255, 0), 2)
    # draw center of circle
    cv2.circle(sample_frame, (i[0], i[1]), 2, (0, 255, 0), 9)

plt.imshow(sample_frame,cmap="gray") #outputs image from sample frame with Hough circle

#write in new video
writer = cv2.VideoWriter("output_hough.mp4",cv2.VideoWriter_fourcc(*"MP4V"),30,(512,512))

video = cv2.VideoCapture("video.wmv")
total_frames = video.get(cv2.CAP_PROP_FRAME_COUNT)

frameCnt = 0
 
########## CODE WORKS FINE UNTIL THIS POINT ##############

while(frameCnt < total_frames-1):
    frameCnt +=1  
    ret, frame = video.read()  #read frame 1 by 1 
    
    gframe = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #grayscale frame
    dframe = cv2.absdiff(gframe, grayMedianFrame)  #remove background
    blurred = cv2.medianBlur(dframe, 25)  #blur
  
    #Hough transformation
    circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1,120,param1= 50, param2=30,minRadius=0,maxRadius=0)
    circles = np.uint16(np.around(circles))
    
    for i in circles[0, :]:   
        # draw outer circle
        cv2.circle(frame, (i[0], i[1]), i[2], (0, 255, 0), 2)
        # draw center of circle
        cv2.circle(frame, (i[0], i[1]), 2, (0, 255, 0), 9)

    writer.write(cv2.resize(frame,(512,512))) #write frame into output vid
    
video.release()
writer.release()

您没有显示完整堆栈和引发错误的确切行。 但是,看看你的代码,我想问题在于:

圆圈=np.uint16np.环绕圆圈

发生的情况是cv2.HoughCircles可以返回None,如果它找不到圆,那么np.around不能使用None


如果圆不是空的,您应该首先检查您是否已获得圆:

您是否已经进行了一些调试?如果是这样,请在你做HoughCircles之前告诉我们模糊是否正确。我相信你尝试使用的图像不存在。使用imshow或.shape检查图像,确保它在处理的每个阶段都存在。@not\u bot\u no\u really\u 82353是的,代码的第一部分显示“模糊”效果良好。@fmw42我在代码的第一部分检查它。它似乎工作得很好,但当我在while循环中重复时,问题就出现了。这很有意义。我如何绕过这个问题?就像我写的,检查你是否没有,当没有圆圈时,做你想做的事情。例如:通过继续跳过此帧,或仅执行writer来保持帧的原样。如果没有圆,则写入:在圆=cv2.HoughCircles模糊,cv2.HOUGH_GRADIENT,1120,param1=50,param2=30,minRadius=0,maxRadius=0的直线之后继续。似乎解决了此问题。非常感谢你!