python - Detecting colored circle and it's center using OpenCV -
i trying detect blue colored circle , it's center. draw circle on detected circle , small circle on it's center. few errors. (i using opencv 3.1.0, python 2.7 anaconda 64 bits, pycharm ide) (please me using python codes) run following code:
import cv2 import numpy np cap = cv2.videocapture(0) if cap.isopened(): while(true): frame, _ = cap.read() # blurring frame that's captured frame_gau_blur = cv2.gaussianblur(frame, (3, 3), 0) # converting bgr hsv hsv = cv2.cvtcolor(frame_gau_blur, cv2.color_bgr2hsv) # range of blue color in hsv lower_blue = np.array([110, 50, 50]) higher_blue = np.array([130, 255, 255]) # getting range of blue color in frame blue_range = cv2.inrange(hsv, lower_blue, higher_blue) # getting v channel gray channel blue_s_gray = blue_range[::2] # applying houghcircles circles = cv2.houghcircles(blue_s_gray, cv2.hough_gradient, 1, 10, 100, 30, 5, 50) circles = np.uint16(np.around(circles)) in circles[0,:]: # drawing on detected circle , center cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2) cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) cv2.imshow('circles', frame) k = cv2.waitkey(5) & 0xff if k == 27: break cv2.destroyallwindows() else: print "can't find camera"
the error when run code is:
opencv error: assertion failed (depth == cv_8u || depth == cv_16u || depth == cv_32f) in cv::cvtcolor, file c:\builds\master_packslaveaddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7935 traceback (most recent call last): file "c:/users/meliodas/pycharmprojects/opencv_by_examples/code_tester.py", line 11, in hsv = cv2.cvtcolor(frame_gau_blur, cv2.color_bgr2hsv) cv2.error: c:\builds\master_packslaveaddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7935: error: (-215) depth == cv_8u || depth == cv_16u || depth == cv_32f in function cv::cvtcolor
thanks lot in advance help!
i have solved problem , after looking meanings of errors online (the one's got), able find solutions them , hence able solve them. if run following code given below should able detect blue circles pretty well. lot people tried me solve problem.
the code given below:
import cv2 import numpy np cap = cv2.videocapture(0) if cap.isopened(): while(true): ret, frame = cap.read() # blurring frame that's captured frame_gau_blur = cv2.gaussianblur(frame, (3, 3), 0) # converting bgr hsv hsv = cv2.cvtcolor(frame_gau_blur, cv2.color_bgr2hsv) # range of blue color in hsv lower_blue = np.array([110, 50, 50]) higher_blue = np.array([130, 255, 255]) # getting range of blue color in frame blue_range = cv2.inrange(hsv, lower_blue, higher_blue) res_blue = cv2.bitwise_and(frame_gau_blur,frame_gau_blur, mask=blue_range) blue_s_gray = cv2.cvtcolor(res_blue, cv2.color_bgr2gray) canny_edge = cv2.canny(blue_s_gray, 50, 240) # applying houghcircles circles = cv2.houghcircles(canny_edge, cv2.hough_gradient, dp=1, mindist=10, param1=10, param2=20, minradius=100, maxradius=120) cir_cen = [] if circles != none: # circles = np.uint16(np.around(circles)) in circles[0,:]: # drawing on detected circle , center cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2) cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3) cir_cen.append((i[0],i[1])) print cir_cen cv2.imshow('circles', frame) cv2.imshow('gray', blue_s_gray) cv2.imshow('canny', canny_edge) k = cv2.waitkey(5) & 0xff if k == 27: break cv2.destroyallwindows() else: print 'no cam'
Comments
Post a Comment