gpt4 book ai didi

python - 我可以将小图像(13x12)随机添加到像素数组(与小图像尺寸相同的随机像素)吗?

转载 作者:行者123 更新时间:2023-12-02 17:31:10 25 4
gpt4 key购买 nike

我有20张小图像(我想在背景图像的目标区域(13x12)中放置。我已经用圆圈标记了我的目标区域,我将圆圈的坐标设置为两个像素阵列。现在我想要知道如何将我的20个小图像随机添加到我的像素阵列(基本上是目标区域(绘制的圆圈))的随机区域中。

在我的代码中,我只尝试获取一张图像,如果可以,我将传递20张小图像的文件夹。

Input 1

# Depencies importation
import cv2
import numpy as np

# Saving directory
saving_dir = "../Saved_Images/"

# Read the background image
bgimg = cv2.imread("../Images/background.jpg")

# Resizing the bacground image
bgimg_resized = cv2.resize(bgimg, (2050,2050))

# Read the image that will be put in the background image (exemple of 1)
# I'm just trying with one, if it works, I'll pass the folder of the 20
small_img = cv2.imread("../Images/small.jpg")

# Convert the resized background image to gray
bgimg_gray = cv2.cvtColor(bgimg, cv2.COLOR_BGR2GRAY)
# Convert the grayscale image to a binary image
ret, thresh = cv2.threshold(bgimg_gray,127,255,0)
# Determine the moments of the binary image
M = cv2.moments(thresh)
# calculate x,y coordinate of center
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

# Drawing the circle in the background image
circle = cv2.circle(bgimg, (cX, cY), 930, (0,0,255), 9)

print(circle) # This returns None

# Getting the coordinates of the circle
combined = bgimg[:,:,0] + bgimg[:,:,1] + bgimg[:,:,2]
rows, cols = np.where(combined >= 0)

# I have those pixels in rows and cols, but I don't know
# How to randomly put my small image in those pixel

# Saving the new image
cv2.imwrite(saving_dir+"bgimg"+".jpg", bgimg)

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.resizeWindow("Test", 1000, 1200)
# Showing the images
cv2.imshow("image", bgimg)
# Waiting for any key to stop the program execution
cv2.waitKey(0)

为了达到预期的效果,必须将小图像随机放置在背景图像中。

最佳答案

如果您具有圆的中心和半径,则可以通过从theta中随机选择一个 Angular [0, 2*pi],通过xy计算相应的cos(theta)sin(theta)值,并通过从[0, radius]中随机选择的缩放比例来缩放它们来轻松生成随机坐标。我为您准备了一些代码,请参见下文。

我从您的代码中省去了很多代码(阅读,预处理,保存),以专注于相关部分(请参阅how to create a minimal, complete, and verifiable example)。希望您可以将我的解决方案的主要思想自己集成到您的代码中。如果没有,我将提供进一步的解释。

import cv2
import numpy as np

# (Artificial) Background image (instead of reading an actual image...)
bgimg = 128 * np.ones((401, 401, 3), np.uint8)

# Circle parameters (obtained somehow...)
center = (200, 200)
radius = 100

# Draw circle in background image
cv2.circle(bgimg, center, radius, (0, 0, 255), 3)

# Shape of small image (known before-hand...?)
(w, h) = (13, 12)

for k in range(200):

# (Artificial) Small image (instead of reading an actual image...)
smallimg = np.uint8(np.add(128 * np.random.rand(w, h, 3), (127, 127, 127)))

# Select random angle theta from [0, 2*pi]
theta = 2 * np.pi * np.random.rand()

# Select random distance factors from center
factX = (radius - w/2) * np.random.rand()
factY = (radius - h/2) * np.random.rand()

# Calculate random coordinates for small image from angle and distance factors
(x, y) = np.uint16(np.add((np.cos(theta) * factX - w/2, np.sin(theta) * factY - h/2), center))

# Replace (rather than "add") determined area in background image with small image
bgimg[x:x+smallimg.shape[0], y:y+smallimg.shape[1]] = smallimg

cv2.imshow("bgimg", bgimg)
cv2.waitKey(0)

示例输出:

Output

注意:如果小图像可能会违反圆边界,我不会注意。因此,必须添加一些对比例因子的附加检查或限制。

编辑:我编辑了上面的代码。为了考虑 below comment,我将小图像移动了 (width/2, height/2),并相应地限制了半径比例因子,这样就不会违反圆的边界,无论是上/左还是下/右。

以前,有可能在底部/右侧部分( n = 200)中违反了边界:

Violation

编辑后,应避免这种情况( n = 20000):

No violation

图像中红线的触摸是由于该线的粗细。出于“安全原因”,可以增加另一个1像素距离。

关于python - 我可以将小图像(13x12)随机添加到像素数组(与小图像尺寸相同的随机像素)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55609337/

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