【问题标题】:Resizing an object in an image调整图像中对象的大小
【发布时间】:2021-09-10 01:39:26
【问题描述】:

我想做的是就地增加这些盒子的大小,比如如果盒子的大小为 100x200,我想要 120x240,即大小增加 20%。

调整图像的裁剪大小不适用于所有图像,因为我将它用作其他图像的蒙版,如果位置发生更改,下一步将无法工作。

我一直在寻找一种方法,但找不到。

我正在使用 python 3.9.4

【问题讨论】:

  • 能否包含生成图像的代码?
  • @IbrahimTG99 这个tutorial 有助于找到你的矩形的中心,在你这样做之后你会找到你的矩形角 [x0, y0, x1, y1] 你可以增加你的面具的尺寸通过将 x,y 更改为您想要的值。
  • @sanitizedUser 我对原始图像进行阈值处理并将黑色像素转换为白色生成的图像,因此我不知道这些框的位置。我正在使用此蒙版将其传递给 inpaint 函数以修复那些黑框。问题已解决,感谢您对我的问题感兴趣。

标签: python opencv image-processing numpy-ndarray


【解决方案1】:

您可以使用 OpenCV 中的连接组件功能来检测白框。现在,一旦您确定了所有框的中心、高度、宽度,您就可以简单地增加它们的大小并将它们替换为黑色图像。

函数官方文档:Structural Analysis and Shape Descriptors

import cv2
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
output = cv2.connectedComponentsWithStats(thresh, args["connectivity"],cv2.CV_32S)
(numLabels, labels, stats, centroids) = output

上面的代码加载了图片,并找出了图片中的所有组件

现在您可以遍历它们以找到所需的组件。

# loop over the number of unique connected component labels
for i in range(0, numLabels):
# if this is the first component then we examine the
# *background* (typically we would just ignore this
# component in our loop)
if i == 0:
    text = "examining component {}/{} (background)".format(
        i + 1, numLabels)
# otherwise, we are examining an actual connected component
else:
    text = "examining component {}/{}".format( i + 1, numLabels)
# print a status message update for the current connected
# component
print("[INFO] {}".format(text))
# extract the connected component statistics and centroid for
# the current label
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
area = stats[i, cv2.CC_STAT_AREA]
(cX, cY) = centroids[i]

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2011-10-24
    • 2010-11-20
    相关资源
    最近更新 更多