调整图像中对象的大小
Resizing an object in an image
我想做的是就地增加这些盒子的尺寸,比如如果盒子的尺寸是 100x200,我想变成 120x240,即尺寸增加 20%。
调整裁剪图片的大小不适用于所有图片,因为我将它用作另一张图片的蒙版,如果更改位置,下一步将不起作用。
我一直在寻找一种方法,但找不到。
我正在使用 python 3.9.4
您可以使用 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]
我想做的是就地增加这些盒子的尺寸,比如如果盒子的尺寸是 100x200,我想变成 120x240,即尺寸增加 20%。
调整裁剪图片的大小不适用于所有图片,因为我将它用作另一张图片的蒙版,如果更改位置,下一步将不起作用。
我一直在寻找一种方法,但找不到。
我正在使用 python 3.9.4
您可以使用 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]