【问题标题】:How to draw the area inside a contour using opencv? (the picture was exported from dxf file) [closed]如何使用opencv绘制轮廓内的区域? (图片是从 dxf 文件导出的)[关闭]
【发布时间】:2022-01-24 06:43:13
【问题描述】:

我有一张包含多个机械部件的图片。它们是使用 ezdxf 直接从 dxf 文件导出的。我怎样才能将它们分别绘制到图像上?我尝试使用contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) 通过plt 通过contours 中的点来绘制它们。但是,图表会变得模糊。有什么方法可以帮助我吗? 下面是图片。提前致谢 picture

【问题讨论】:

    标签: python opencv ezdxf


    【解决方案1】:

    我正在考虑输入图像存储在变量“inImage”中。此图像是一个二值图像,每个像素只有 0/255 个值。使用下面的代码在单独的图像上获取每个机械组件。

    # Finding the contours
    Contours = cv2.findContours(inImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
    
    ComponentImages = []
    for Contour in Contours:
        # Getting the bounding box of the contour
        x, y, w, h = cv2.boundingRect(Contour)
    
        # Extracting the mechanical component from the original image
        img = inImage[y:y+h, x:x+w].copy()
    
        # Creating the mask image of the contour separately
        maskImg = np.zeros((h, w), dtype=np.uint8)
        cv2.drawContours(maskImg, [Contour], -1, 255, -1)
    
        # Performing bitwise operation to remove any part if not inside this contour
        img = cv2.bitwise_and(img, maskImg)
    
        # Storing this component image
        ComponentImages.append(img)
    
    

    最后,“ComponentImages”将存储每个机械部件的图像。

    【讨论】:

    • 有道理!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2021-09-27
    • 1970-01-01
    • 2013-06-16
    • 2020-07-11
    相关资源
    最近更新 更多