在OpenCV上进行霍夫圆变换时出错-python
Error when performing Hough circle transformation on OpenCV-python
下面的代码旨在对视频执行霍夫圆变换。当从视频中提取帧并执行霍夫圆变换时,代码会按预期工作。但是,当我尝试复制类似的方法来制作视频时,我得到了这个错误 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()
您没有显示完整堆栈和引发错误的确切行。
但是,看你的代码我猜问题出在:
circles = np.uint16(np.around(circles))
发生的情况是 cv2.HoughCircles
可以 return None
如果找不到圆圈,则 np.around
无法与 None 一起使用。
你应该做的是首先检查你的圈子是否由 if circles is not None:
下面的代码旨在对视频执行霍夫圆变换。当从视频中提取帧并执行霍夫圆变换时,代码会按预期工作。但是,当我尝试复制类似的方法来制作视频时,我得到了这个错误 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()
您没有显示完整堆栈和引发错误的确切行。 但是,看你的代码我猜问题出在:
circles = np.uint16(np.around(circles))
发生的情况是 cv2.HoughCircles
可以 return None
如果找不到圆圈,则 np.around
无法与 None 一起使用。
你应该做的是首先检查你的圈子是否由 if circles is not None: