[英]Finding intersection point of incomplete circle with python
我試圖找到不完整圓的交點,如下圖:
參考這個鏈接的解決方案: Detect semi-circle in opencv
我正在嘗試將 c++ 代碼轉換為 python 代碼,我已經轉換了大部分代碼,但我不明白下面的 2 行 c++ 代碼:
為什么半徑需要除以25?
// maximal distance of inlier might depend on the size of the circle float maxInlierDist = radius/25.0f;
我完全不知道如何將此 C++ 行轉換為 python:
if(dt.at<float>(cY,cX) < maxInlierDist)
希望有人能幫我解決這個問題,謝謝!
我試圖用谷歌搜索一些數學公式,但找不到為什么半徑需要除以 25。我在 C++ 方面也不太好。
我轉換的代碼:
# import the necessary packages
import numpy as np
import argparse
import cv2
import math
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--thres", required = True, help = "Path to the image")
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())
# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.Canny(gray, 200,20)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1,minDist=300,
param1=200, param2=20,
minRadius=0, maxRadius=0)
#gray = (255*mask).astype(np.uint8)
dt = cv2.distanceTransform(255-gray, cv2.DIST_L2, 3)
cv2.imshow('Distance Transform', dt/255.0)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(image, (x, y), r, (0, 255, 0), 2)
cv2.rectangle(image, (x - 5, y - 5), (x + 5, y + 5),
(0, 128,255),-1)
minInlierDist = 2.0
counter =0
inlier =0
radius=r
num_circle = 50
maxInlierDist=radius/25.0
if maxInlierDist<minInlierDist:
maxInlierDist=minInlierDist
for index in range(num_circle):
counter +=1
#angle = t * math.pi / 180
angle = 2 * math.pi * index / num_circle
cX = x + math.sin(angle)*radius
cY = y + math.cos(angle)*radius
centerxy = cX,cY
cv2.circle(image,tuple(np.array(centerxy,int)),3,(0,0,255),-1)
#if(dt.at<float>(cY,cX) < maxInlierDist) #c++ ! I'm stuck here!
cv2.imshow("output", image)#np.hstack([image, gray]))
cv2.waitKey(0)
else:
print("no circles found!")
cv2.waitKey(0)
這里最重要的問題是:如何知道/識別圓圈上的綠色采樣點是內部點,藍色點是異常點。
1) 1/25 已被選為最大可接受誤差的半徑的任意分數。
2)在opencv python中,矩陣存儲為多維numpy數組。 要訪問 (cY,cX) 處的點,請使用 dt[cY,cX]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.