Reputation: 23
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()
When I run , There is a error called :
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
I want to find circle on video .
Upvotes: 0
Views: 2838
Reputation: 771
Yeah! Hough Circle Transform is for Grayscale images only. So you have to give "cimg" (from your code) as input to HoughCircles.
Also, you don't have to write this line:
cimg = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
Upvotes: 1
Reputation: 28950
HoughTransform
is only supported on greyscale images
cv2.HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) → circles
Parameters: image – 8-bit, single-channel, grayscale input image.
Either pick one channel or convert your image to single channel.
Upvotes: 0