简体   繁体   English

如何使用 opencv 绘制轮廓内的区域? (图片是从dxf文件导出的)

[英]How to draw the area inside a contour using opencv? (the picture was exported from dxf file)

I have a picture which has multiple mechanical components.我有一张包含多个机械部件的图片。 They were exported directly from the dxf file using ezdxf.它们是使用 ezdxf 直接从 dxf 文件导出的。 How can I divide them draw each one to an image separately?我怎样才能将它们分别绘制到图像上? I have tried using contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) to draw them by plt by points in contours .我尝试使用contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)通过plt by points in contours绘制它们。 However, the graph will be blurred.但是,图表会变得模糊。 Is there any methods that can help me out?有什么方法可以帮助我吗? Below is the picture.下面是图片。 Thanks in advance picture先谢谢了图片

I am considering that the input image is stored in the variable "inImage".我正在考虑输入图像存储在变量“inImage”中。 This image is a binary image with only 0/255 values for each pixel.此图像是一个二值图像,每个像素只有 0/255 个值。 Use the code below to get each of the mechanical components on separate images.使用下面的代码在单独的图像上获取每个机械组件。

# 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)

Finally, "ComponentImages" will store the images of each mechanical component.最后,“ComponentImages”将存储每个机械部件的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM