Warm tip: This article is reproduced from serverfault.com, please click

其他-在OpenCV-python上执行霍夫圆变换时出错

(其他 - Error when performing Hough circle transformation on OpenCV-python)

发布于 2020-11-27 21:07:59

以下代码旨在对视频执行Hough Circle转换。当从视频中提取帧并执行霍夫环变换时,该代码将按预期工作。但是,当我尝试复制类似的方法来制作视频时,出现此错误loop of ufunc does not support argument 0 of type NoneType which has no callable rint method该错误的原因似乎在while循环之下。可能是此问题的原因?

提前谢谢了。

更新:我已将更改blurred = cv2.medianBlur(dframe, 25)blurred = cv2.GaussianBlur(dframe,(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()
Questioner
GitGoodCodes
Viewed
11
Lior Cohen 2020-11-29 02:52:39

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

circles = np.uint16(np.around(circles))

发生的情况是,如果找不到圈子,则cv2.HoughCircles返回None该值,则表示该np.around值与None无关。

你应该做的是先检查一下你的圈子 if circles is not None: