繁体   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