猿问

我可以将小图像(13x12)随机添加到像素数组中吗(与我的小图像大小相同的随机像素)

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

在我的代码中,我只尝试了一张图片,如果有效,我将传递我的 20 张小图片的文件夹。

# 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)

在预期的结果中,小图像必须随机放置在背景图像中


杨魅力
浏览 174回答 1
1回答

芜湖不芜

如果您有圆的中心和半径,您可以通过theta从 中随机选择一个角度[0, 2*pi],通过和计算相应的x和y值,cos(theta)并sin(theta)通过从 中随机选择的一些缩放因子来缩放这些值,从而轻松生成随机坐标[0, radius]。我为你准备了一些代码,见下文。我省略了你的很多代码(阅读、预处理、保存)以专注于相关部分(参见如何创建一个最小、完整和可验证的示例)。希望您可以自行将我的解决方案的主要思想集成到您的代码中。如果没有,我将提供进一步的解释。import cv2import 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 imagecv2.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]] = smallimgcv2.imshow("bgimg", bgimg)cv2.waitKey(0)示例输出:警告:我没有注意,如果小图像可能会违反圆形边界。因此,必须对缩放因子添加一些额外的检查或限制。编辑:我编辑了上面的代码。考虑到以下评论,我将小图像移动了(width/2, height/2),并相应地限制了半径比例因子,以便不违反圆形边界,无论是上/左还是下/右。之前,有可能在底部/右侧部分 ( n = 200) 中违反了边界:编辑后,应防止这种情况 ( n = 20000):图像中红线的接触是由于线的粗细。出于“安全原因”,可以再增加 1 个像素的距离。
随时随地看视频慕课网APP

相关分类

Python
我要回答