1 回答

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
我不知道你到底想做什么,但你的錯(cuò)誤是你使用了 second capture_continuous。您應(yīng)該只使用一個(gè)capture_continuous并發(fā)送frame(甚至image)作為參數(shù),function(frame)并且它應(yīng)該只使用 this frame。在處理完這個(gè)單幀后,它將返回到主循環(huán),主循環(huán)將function(frame)再次運(yùn)行 next frame- 所以它會(huì)像在capture_continuous
# --- functions ---
def function(frame):
imageCirles = frame.array
gray = cv2.cvtColor(imageCirles, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 400)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# 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
cv2.circle(imageCirles, (x, y), r, (255, 255, 0), 4)
cv2.rectangle(imageCirles, (x - 5, y - 5), (x + 5, y + 5), (0, 0, 255), -1)
cv2.putText(imageCirles, str(((x - 160) / 10) - 24), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2)
# --- main ---
rawCapture = PiRGBArray(camera, size=(320, 180))
counter = 0
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
counter += 1
image = frame.array
line = cv2.inRange(image, (0, 0, 0), (255, 255, 75))
if counter > 10: #call function
function(frame)
rawCapture.truncate(0)
編輯:
最終你可以使用break退出一個(gè)循環(huán)然后運(yùn)行另一個(gè)循環(huán)。
counter = 0
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
counter += 1
image = frame.array
line = cv2.inRange(image, (0, 0, 0), (255, 255, 75))
if counter > 10: #call function
break
rawCapture.truncate(0)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
function(frame)
rawCapture.truncate(0)
我不確定,但它可能會(huì)造成很小的延遲。
如果第一個(gè)循環(huán)用于跟隨線,那么它將不再跟隨?,F(xiàn)在它將只檢查圓圈。如果你想同時(shí)跟隨線和檢查圓圈,那么你應(yīng)該capture_continuous像第一個(gè)例子一樣運(yùn)行所有的東西。
添加回答
舉報(bào)