边界框不重叠时无法获取轮廓

问题描述 投票:0回答:1

我有一些精灵表。在某些情况下,即使精灵本身不重叠,精灵的边界框也会重叠:

在其他情况下,边界框不重叠:

要提取单个精灵,我正在执行以下操作:

im = cv2.imread("trees.png") # read image
imGray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # convert to gray
contours, _ = cv2.findContours(imGray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # contouring
sortedContours = sorted(contours, key=cv2.contourArea, reverse=True) # sorting, not necessary...
for contourIdx in range(0,len(sortedContours)-1): # loop with index for easier saving
    contouredImage = im.copy() # copy image
    contouredImage = cv2.drawContours(contouredImage, sortedContours, contourIdx, (255,255,255), -1) # fill contour with white
    extractedImage = cv2.inRange(contouredImage, (254,254,254), (255,255,255)) # extract white from image
    resultImage = cv2.bitwise_and(im, im, mask=extractedImage) # AND operator to get only one filled contour at a time
    x, y, w, h = cv2.boundingRect(sortedContours[contourIdx]) # get bounding box
    croppedImage = resultImage[y:y + h, x:x + w] # crop
    cv2.imwrite("contour_"+str(contourIdx)+".png", croppedImage) # save

这对于边界框重叠的前一个图像非常有用,但对于边界框不重叠的后一种情况则失败。这是为什么?我该如何解决它?

编辑: 在前一种情况下,正如预期的那样,它会检测各个轮廓并分别输出。但在后一种情况下,它似乎没有检测到任何单独的轮廓,或者更确切地说,输出了整个图像。

python opencv computer-vision sprite image-segmentation
1个回答
0
投票

问题是您的图像没有以相同的方式创建。如果您在重叠图像上运行 ExifTool,它会显示以下内容:

Bit Depth                       : 8
Color Type                      : RGB with Alpha
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Adam7 Interlace
SRGB Rendering                  : Perceptual
Exif Byte Order                 : Big-endian (Motorola, MM)
Orientation                     : Horizontal (normal)
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches
Software                        : Pixelmator 3.9.11
Modify Date                     : 2024:01:21 12:01:94
Color Space                     : sRGB
Exif Image Width                : 526
Exif Image Height               : 244
Pixels Per Unit X               : 2835
Pixels Per Unit Y               : 2835
Pixel Units                     : meters
XMP Toolkit                     : XMP Core 6.0.0
...
Creator Tool                    : Pixelmator 3.9.11

以及无重叠图像:

Bit Depth                       : 8
Color Type                      : RGB with Alpha
Compression                     : Deflate/Inflate
Filter                          : Adaptive
Interlace                       : Adam7 Interlace
Software                        : Adobe ImageReady
XMP Toolkit                     : Adobe XMP Core 5.0-c060 61.134777, 2010/02/12-17:32:00
Creator Tool                    : Adobe Photoshop CS5 Windows

究竟是哪一个方面导致了问题并不重要,但是如果您在使用 OpenCV 加载图像后检查图像,您会发现无重叠图像中的透明像素是绿色的,灰度级是绿色的。转换将所有像素变成相同的颜色。

但是,此代码适用于这两个图像:

# read the image as is, no interpretation
im = cv2.imread("trees.png", cv2.IMREAD_UNCHANGED)
# get the first three channels as RGB
rgb = im[:, :, :3]
# replace all the transparent pixels (alpha = 0) with black
rgb[im[:, :, 3] == 0] = [0, 0, 0]
# generate a greyscale image from the resulting RGB channels
imGray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)

您可以选择白色,而不是选择黑色,具体取决于您期望在图像中看到的内容。上述更改有效,但这并不意味着这是解决此问题的最佳方法。我想 Alpha 通道 (

im[:, :, 3]
) 实际上拥有您需要的一切,也许您应该使用它而不是 RGB 通道来查找轮廓。

© www.soinside.com 2019 - 2024. All rights reserved.