gpt4 book ai didi

c# - 轮廓的arcLength结果是什么?

转载 作者:行者123 更新时间:2023-12-02 16:51:43 25 4
gpt4 key购买 nike

我在Google中搜索了arcLength,好吧,也许我能理解它,但是它对EmguCV或OpenCV中图像的轮廓如何起作用?我试图使用MATLAB制作一些图像。该图像为9 x 9,我在图像中画了一条线,该线为1像素。我在EmguCV中使用此代码来检测轮廓:

VectorOfVectorOfPoint cons = new VectorOfVectorOfPoint();

CvInvoke.FindContours(img_gray, cons, null, RetrType.List, ChainApproxMethod.ChainApproxNone);
for(int i=0; i<cons.Size;i++)
{
VectorOfPoint points = cons[i];
for(int x =0; x<points.Size;x++)
{
temp[points[x]] = new Gray(255);
}
double c= CvInvoke.ArcLength(cons[i], true);
textBox1.Text = c.ToString();
}

imageBox2.Image = temp;
arcLength是:
  • 当行为1像素时-> arcLength为0。
  • 当行为2像素时-> arcLength为2。
  • 当行为3像素时-> arcLength为4。

  • 这是我的图像,当线是3像素时。

    enter image description here

    有人可以向我解释结果吗?

    最佳答案

    arcLength 完全按照其要求进行:

    Calculates a contour perimeter or a curve length.



    在您的示例中,您被 findContours (!)的特定问题所迷惑,即应用于1像素宽的行! (实现问题,算法问题,带有“边界跟随”的一般问题,...!?)

    让我们看一下以下示例(很抱歉在这里使用Python API,但是概念应该清楚了)。

    示例1: 3 x 1黑色图像上的白线

    import cv2
    import numpy as np

    # Generate 5 x 5 black image
    img = np.zeros((5, 5), np.uint8)

    # Draw 3 x 1 white line
    img = cv2.rectangle(img, (1, 1), (3, 1), 255, cv2.FILLED)

    # Find contours
    cnts = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]

    # Outputs
    print(img, '\n') # Image
    print(np.squeeze(cnts[0]), '\n') # Contour
    print('Contour points:', cnts[0].shape[0], '\n')
    print('arcLength:', cv2.arcLength(cnts[0], True))

    输出:

    [[  0   0   0   0   0]
    [ 0 255 255 255 0]
    [ 0 0 0 0 0]
    [ 0 0 0 0 0]
    [ 0 0 0 0 0]]

    [[1 1]
    [2 1]
    [3 1]
    [2 1]]

    Contour points: 4

    arcLength: 4.0

    请注意, [2 1]在轮廓中出现两次,所以我们总共有四个轮廓点,两个相邻轮廓点之间的每个“距离”为1,因此轮廓周长(=弧长)也为4。

    示例2: 3 x 2黑色图像上的白色矩形

    import cv2
    import numpy as np

    # Generate 5 x 5 black image
    img = np.zeros((5, 5), np.uint8)

    # Draw 3 x 2 white rectangle
    img = cv2.rectangle(img, (1, 1), (3, 2), 255, cv2.FILLED)

    # Find contours
    cnts = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]

    # Outputs
    print(img, '\n') # Image
    print(np.squeeze(cnts[0]), '\n') # Contour
    print('Contour points:', cnts[0].shape[0], '\n')
    print('arcLength:', cv2.arcLength(cnts[0], True))

    输出:

    [[  0   0   0   0   0]
    [ 0 255 255 255 0]
    [ 0 255 255 255 0]
    [ 0 0 0 0 0]
    [ 0 0 0 0 0]]

    [[1 1]
    [1 2]
    [2 2]
    [3 2]
    [3 1]
    [2 1]]

    Contour points: 6

    arcLength: 6.0

    我们得到六个轮廓点,并且两个相邻轮廓点之间的每个“距离”均为1,因此轮廓周长(=弧长)也为6 –这似乎(更合理)。

    示例3:在黑色图像上带有半径 2的白色圆圈

    import cv2
    import numpy as np

    # Generate 5 x 5 black image
    img = np.zeros((5, 5), np.uint8)

    # Draw white circle with radius 2
    img = cv2.circle(img, (2, 2), 2, 255, cv2.FILLED)

    # Find contours
    cnts = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]

    # Outputs
    print(img, '\n') # Image
    print(np.squeeze(cnts[0]), '\n') # Contour
    print('Contour points:', cnts[0].shape[0], '\n')
    print('arcLength:', cv2.arcLength(cnts[0], True))

    输出:

    [[  0   0 255   0   0]
    [ 0 255 255 255 0]
    [255 255 255 255 255]
    [ 0 255 255 255 0]
    [ 0 0 255 0 0]]

    [[2 0]
    [1 1]
    [0 2]
    [1 3]
    [2 4]
    [3 3]
    [4 2]
    [3 1]]

    Contour points: 8

    arcLength: 11.313708305358887

    [2 0][1 1]的“距离”是1.414 ...(2的平方根)。每个相邻的两个轮廓点都具有该距离(请参见图片),因此我们的轮廓周长(=弧长)为8 * 1.414 ... = 11.313 ...

    希望能帮助理解!

    ----------------------------------------
    System information
    ----------------------------------------
    Platform: Windows-10-10.0.16299-SP0
    Python: 3.8.1
    NumPy: 1.18.1
    OpenCV: 4.2.0
    ----------------------------------------

    关于c# - 轮廓的arcLength结果是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60178004/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com