【问题标题】:Is there any function in openCV or another library that can tile squares within an arbitrary contour?openCV 或其他库中是否有任何函数可以在任意轮廓内平铺正方形?
【发布时间】:2024-01-22 05:03:01
【问题描述】:

我在一些图片中发现了狗周围的一些轮廓,例如:

我想在轮廓内平铺正方形/矩形。是否有为此的openCV(或其他库)功能?我正在使用 Python。我希望它看起来像这样:

【问题讨论】:

    标签: python python-3.x opencv computer-vision


    【解决方案1】:

    我能够通过首先在整个图像上绘制矩形来解决这个问题,然后检查哪些矩形在狗所在的区域内:

    # the image here is stored as the variable fg
    # with b, g, r, and alpha channels
    # the alpha channel is masking the dog part of the image
    import cv2
    
    b, g, r, a = cv2.split(fg)
    fgcp = fg.copy()
    h, w = fg.shape[:2]
    h -= 1
    w -= 1 # avoid indexing error
    rectDims = [10, 10] # w, h of rectangles
    hRects = h / rectDims[0]
    wRects = w / rectDims[1]
    for i in range(wRects):
        for j in range(hRects):
            pt1 = (i * rectDims[0], j * rectDims[1])
            pt2 = ((i + 1) * rectDims[0], (j + 1) * rectDims[1])
            # alpha is 255 over the part of the dog
            if a[pt1[1], pt1[0]] == 255 and a[pt2[1], pt2[0]] == 255:
                cv2.rectangle(fgcp, pt1, pt2, [0, 0, 255], 2)
    
    cv2.imshow('', fgcp), cv2.waitKey(0)
    

    这不一定是理想的实现,但效果很好。

    【讨论】:

      最近更新 更多