1つの色の背景に異なる色の円の画像があると想像してみてください。円の中心の座標を見つける最も簡単な方法は何ですか(もちろんプログラムで)?
他の回答と同じ開始イメージを使用して、OpenCVを使用してPythonでもそれを実行したいと思いました。
コードは次のようになります。
#!/usr/bin/env python3
import numpy as np
import cv2
# Load image
im = cv2.imread('start.png')
# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)
# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)
# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))
それはターミナルでこれを与えます:
Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40
そしてこれは結果画像として:
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加