Pyhtonで円検出
円形の検出処理の勉強
結果
OpenCVのハフ変換で円検出した。
左が元画像、左が検出結果をオーバレイした画像
プログラム
# 19_CircleDetection_001.py # python 3.8.1 # coding: utf-8 # import cv2 import numpy as np from matplotlib import pylab as plt img = cv2.imread('19_test2.jpg') output = img.copy() #Prep the detection circles gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray ,5) #Detect circles circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 10, param1=100, param2=20, minRadius=5, maxRadius=40) circles = np.uint16(np.around(circles)) n = 0 for i in circles[0,:]: n+=1 str_n = str(n) #Draw the outer circle cv2.circle(output,(i[0],i[1]),i[2],(0,255,0),2) #Draw the center of the circle #cv2.circle(cimg,(i[0],i[1]),1,(0,0,255),2) #Number the circles cv2.putText(output, str_n, (i[0], i[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), thickness=2) #Show each radius #print(("[%d] : %s")%(n, i[2])) cv2.imshow("output", np.hstack([img, output])) cv2.waitKey(0) cv2.destroyAllWindows()