python - 用python找到不完整圆的交点

标签 python opencv cv2

我试图找到不完整圆的交点,如下图所示:

enter image description here

引用这个链接的解决方法: Detect semi-circle in opencv

我正在尝试将 C++ 代码转换为 Python 代码,我已经转换了大部分代码,但我不理解下面的两行 C++ 代码:

  1. 为什么半径需要除以 25?

    // maximal distance of inlier might depend on the size of the circle
    float maxInlierDist = radius/25.0f;
    
  2. 我完全不知道如何将此 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]

关于python - 用python找到不完整圆的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56967444/

相关文章:

python - TypeError:labels不是numpy数组,也不是标量

python - pybind11 将 python sys.stdout 从 print() 重定向到 C++

python - ElementTree : Element. remove() 跳跃迭代

Python构造函数和默认值

c# - 如何在 EmguCV 中将 Bgr 像素转换为 Hsv 像素?

java - Java Android 上 OpenCV 中的 CaptureFromFile

python - 为什么 pandas 比较运算符不在索引上对齐?

python - 为什么在openCV手势识别中必须清除MemStorage?

python - cv2 (cv2.cv2) 中未解析的引用 "cv2"

python - (-215:声明失败)number <max_number在函数 'cv::icvExtractPattern'中出错吗?