gpt4 book ai didi

python - 如何将物体随机放置在不同角度和不同位置的python上

转载 作者:行者123 更新时间:2023-12-05 04:44:27 25 4
gpt4 key购买 nike

我想做一个生成数据集的函数,对象将以不同的角度、不同的大小放置在黑色图像上的不同位置,并在图像中随机放置最多 20 次。并在文本文件中保存 x、y 和角度位置。

Input image

下图是五个不同位置和角度的物体。

Output image for five objects

import numpy as np
import cv2
patch=cv2.imread('imagersult.png')
img = np.zeros((2048, 2048, 1), dtype = "uint8")

最佳答案

以下是您可以如何使用 scipy.ndimage轮换补丁的模块:

import numpy as np
import cv2
from random import randrange
from scipy import ndimage

def patch_img(img, patch, amt=5):
h, w, _ = img.shape
for _ in range(amt):
p = ndimage.rotate(patch, randrange(360))
p_h, p_w, _ = p.shape
x = randrange(w - p_w)
y = randrange(h - p_h)
seg = img[y: y + p_h, x: x + p_w]
seg[:] = cv2.bitwise_xor(seg, p)

patch = cv2.imread('imagersult.png')

img = np.zeros((2048, 2048, 3), dtype="uint8")
patch_img(img, patch)

cv2.imshow("Image", img)
cv2.waitKey(0)

多次运行的输出:

enter image description here

enter image description here

enter image description here

对于灰度和色 block 大小的变化:

import numpy as np
import cv2
from random import randrange, uniform
from scipy import ndimage

def patch_img(img, patch, amt=5):
h, w = img.shape
min_scale = 0.5
max_scale = 2
for _ in range(amt):
patch_h, patch_w = patch.shape
scale = uniform(min_scale, max_scale)
p = ndimage.rotate(cv2.resize(patch, (int(patch_w * scale), int(patch_h * scale))), randrange(360))
p_h, p_w = p.shape
x = randrange(w - p_w)
y = randrange(h - p_h)
seg = img[y: y + p_h, x: x + p_w]
seg[:] = cv2.bitwise_xor(seg, p)

patch = cv2.imread('imagersult.png', 0)

img = np.zeros((2048, 2048), dtype="uint8")
patch_img(img, patch)

cv2.imshow("Image", img)
cv2.imwrite("result.png", img)
cv2.waitKey(0)

示例输出:

enter image description here

关于python - 如何将物体随机放置在不同角度和不同位置的python上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69370657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com