gpt4 book ai didi

python - 如何使用Python CV2套件测量圆心角

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

我们的团队使用摄像头,显微镜和可调透镜建立了视觉系统,以观察圆锥的内表面。

从视觉上讲,相机为一个视锥拍摄12张图像,每个图像覆盖30度。

现在,我们已经收集了许多示例图像,并希望确保每个“风扇”(如下所示)至少为30度。
Pythoncv2或其他软件包中有什么方法可以测量该中心角。谢谢。

enter image description here

最佳答案

这是在Python / OpenCV中执行此操作的一种方法。

  • 阅读图片
  • 转换为灰色
  • 阈值
  • 使用形态学打开和关闭以平滑并填充边界
  • 应用Canny边缘提取
  • 通过使每个边缘的相对侧变黑来将图像分为顶部边缘和底部边缘
  • 使线条适合顶部和底部边缘
  • 计算每个边缘的 Angular
  • 计算两个 Angular 之间的差
  • 在输入
  • 上画线
  • 保存结果

  • 输入:

    enter image description here
    import cv2
    import numpy as np
    import math

    # read image
    img = cv2.imread('cone_shape.jpg')

    # convert to grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # threshold
    thresh = cv2.threshold(gray,11,255,cv2.THRESH_BINARY)[1]

    # apply open then close to smooth boundary
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (13,13))
    morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    kernel = np.ones((33,33), np.uint8)
    morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)

    # apply canny edge detection
    edges = cv2.Canny(morph, 150, 200)
    hh, ww = edges.shape
    hh2 = hh // 2

    # split edge image in half vertically and blacken opposite half
    top_edge = edges.copy()
    top_edge[hh2:hh, 0:ww] = 0
    bottom_edge = edges.copy()
    bottom_edge[0:hh2, 0:ww] = 0

    # get coordinates of white pixels in top and bottom
    # note: need to transpose y,x in numpy to x,y for opencv
    top_white_pts = np.argwhere(top_edge.transpose()==255)
    bottom_white_pts = np.argwhere(bottom_edge.transpose()==255)

    # fit lines to white pixels
    # (x,y) is point on line, (vx,vy) is unit vector along line
    (vx1,vy1,x1,y1) = cv2.fitLine(top_white_pts, cv2.DIST_L2, 0, 0.01, 0.01)
    (vx2,vy2,x2,y2) = cv2.fitLine(bottom_white_pts, cv2.DIST_L2, 0, 0.01, 0.01)

    # compute angle for vectors vx,vy
    top_angle = (180/math.pi)*math.atan(vy1/vx1)
    bottom_angle = (180/math.pi)*math.atan(vy2/vx2)
    print(top_angle, bottom_angle)

    # cone angle is the difference
    cone_angle = math.fabs(top_angle - bottom_angle)
    print(cone_angle)

    # draw lines on input
    lines = img.copy()
    p1x1 = int(x1-1000*vx1)
    p1y1 = int(y1-1000*vy1)
    p1x2 = int(x1+1000*vx1)
    p1y2 = int(y1+1000*vy1)
    cv2.line(lines, (p1x1,p1y1), (p1x2,p1y2), (0, 0, 255), 1)
    p2x1 = int(x2-1000*vx2)
    p2y1 = int(y2-1000*vy2)
    p2x2 = int(x2+1000*vx2)
    p2y2 = int(y2+1000*vy2)
    cv2.line(lines, (p2x1,p2y1), (p2x2,p2y2), (0, 0, 255), 1)

    # save resulting images
    cv2.imwrite('cone_shape_thresh.jpg',thresh)
    cv2.imwrite('cone_shape_morph.jpg',morph)
    cv2.imwrite('cone_shape_edges.jpg',edges)
    cv2.imwrite('cone_shape_lines.jpg',lines)

    # show thresh and result
    cv2.imshow("thresh", thresh)
    cv2.imshow("morph", morph)
    cv2.imshow("edges", edges)
    cv2.imshow("top edge", top_edge)
    cv2.imshow("bottom edge", bottom_edge)
    cv2.imshow("lines", lines)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    阈值图片:

    enter image description here

    形态处理图像:

    enter image description here

    边缘图像:

    enter image description here

    输入行:

    enter image description here

    锥角(度):

    42.03975696357633

    关于python - 如何使用Python CV2套件测量圆心角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61595076/

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