簡體   English   中英

視頻上的霍夫圓變換

[英]Hough Circle Transform on Video

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while(1):

# Take each frame
_, frame = cap.read() # frame olarak goruntuyu aldık

# BGR ' yi HSV ye çevirdik
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# HSV nin içindeki renk aralıgını belirledik
lower_yellow = np.array([20,0,0])
upper_yellow = np.array([40,255,255])

# Yukarıda belırledıgımız eşik değerlerini gray goruntunun içinde eşleştirdik.
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)

# bitwise and operatörü ile de ana goruntude yukarıda buldugumuz mask'i aldık.
res = cv2.bitwise_and(frame,frame, mask= mask)
img = cv2.medianBlur(res, 5)

cimg = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 5, 20,
                           param1=50, param2=30, minRadius=0, maxRadius=10)

circles = np.uint16(np.around(circles))
for i in circles[0, :]:
    # draw the outer circle
    cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
    # draw the center of the circle
    cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)

cv2.imshow('detected circles', cimg)
#ayarladıgımız 3 görüntüyü gösterdik

cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
    break

cv2.destroyAllWindows()

當我運行時,有一個錯誤叫做:

cv2.error: /io/opencv/modules/imgproc/src/hough.cpp:1494: error: (-215) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function HoughCircles

我想在視頻上找到圓圈。

是啊! 霍夫圓變換僅適用於灰度圖像。 因此,您必須將“ cimg ”(來自您的代碼)作為HoughCircles 的輸入。

此外,您不必編寫這一行:

cimg = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)

HoughTransform僅支持灰度圖像

cv2.HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) → circles

參數: image – 8 位、單通道、灰度輸入圖像。

選擇一個通道或將您的圖像轉換為單通道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM