php - Detect open circle with Python / OpenCV -
i have picture random circles shown of 1 circle open. size, position , color of circles different each time background white.
i want find coordinates of circle open programmatically. here sample picture:
this picture has coordinates of x:285 y:70. here attempt:
import numpy np import argparse import cv2 ap = argparse.argumentparser() ap.add_argument("-i", "--image", required = true, = "path image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) black = cv2.imread('black.png') output = image.copy() gray = cv2.cvtcolor(image, cv2.color_bgr2gray) ret,thresh = cv2.threshold(gray,127,255,0) contours, hierarchy = cv2.findcontours(thresh,cv2.retr_tree,cv2.chain_approx_simple) cv2.drawcontours(black,contours,-1,(250,250,250),2) newblack = cv2.cvtcolor(black, cv2.color_bgr2gray) circles = cv2.houghcircles(newblack, cv2.cv.cv_hough_gradient, 1, 1, param1=42, param2=35, minradius=15, maxradius=50) if circles not none: circles = np.round(circles[0, :]).astype("int") (x, y, r) in circles: cv2.circle(output, (x, y), r, (0, 255, 0), 4) cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) cv2.imshow("output", np.hstack([image, output])) print circles cv2.waitkey(0)
almost finished!
the param2 value determines circle found. tweaked code iterates on param2 values starting rather high value of 120.
when finds circle stops.
# import necessary packages import numpy np import argparse import cv2 import sys # construct argument parser , parse arguments ap = argparse.argumentparser() ap.add_argument("-i", "--image", required = true, = "path image") args = vars(ap.parse_args()) # load image, clone output, , convert grayscale image = cv2.imread(args["image"]) output = image.copy() gray = cv2.cvtcolor(image, cv2.color_bgr2gray) str = 120 def findcircle( str ): circles = cv2.houghcircles(gray, cv2.cv.cv_hough_gradient, 1, 1, param1=42, param2=str, minradius=10, maxradius=100) if circles not none: circles = np.round(circles[0, :]).astype("int") print circles sys.exit() while circles none: str = str-1 findcircle(str) findcircle(str)
the success rate around 80-100%
how change output of variable "circles" show 1 circle incase more 1 found , how remove unwanted spaces , brackets?
# import necessary packages import numpy np import argparse import cv2 import sys # construct argument parser , parse arguments ap = argparse.argumentparser() ap.add_argument("-i", "--image", required = true, = "path image") args = vars(ap.parse_args()) # load image, clone output, , convert grayscale image = cv2.imread(args["image"]) output = image.copy() gray = cv2.cvtcolor(image, cv2.color_bgr2gray) str = 120 def findcircle( str ): circles = cv2.houghcircles(gray, cv2.cv.cv_hough_gradient, 1, 1, param1=42, param2=str, minradius=10, maxradius=100) if circles not none: circles = np.round(circles[0, :]).astype("int") index = [2] new_circles = np.delete(circles[0], index) print new_circles sys.exit() while circles none: str = str-1 findcircle(str) findcircle(str)
Comments
Post a Comment