如何在二维阵列中随机旋转正方形?

2024-03-28 16:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是检测一个矩形,不管它在图片中的什么位置。为此,我在图片中生成了随机像素和随机生成的正方形。它的形状和大小各不相同

唯一缺少的是矩形始终是一个直的角度我希望矩形随机旋转。

我现在拥有的:

Enter image description here

我想要什么:

Enter image description here

我的第一个(直线)绘图代码:

plt.figure(figsize=(4, 4))
s = 100                                             # Plot size (100, 100)
min = 10                                            # Minimum rectangle height
max = 20                                            # Maximum rectangle height

background = np.random.randint(0, 256, (s, s, 3))   # Random background pixels

a = np.random.randint(min, max)                     # Little side of rectangle
b = np.random.randint(a*1.5, a*2.5)                 # Big side of rectangle

xx, yy = np.where(np.ones((a, b)) == 1)             # Initial coordinates of the rectangle

cx = np.random.randint(0 + a, s - a)                # Random horizontal location
cy = np.random.randint(0 + a, s - b)                # Random vertical location

background[xx + cx, yy + cy] = np.random.randint(200, 255, background[xx + cx, yy + cy].shape)

plt.imshow(background)
plt.xlim(0, s)
plt.ylim(0, s)
plt.title('Random Square')
plt.show()

Tags: ofnp图片pltrandomminbackgroundcx
3条回答

因为你的图片是用随机像素填充的,所以我建议在图片中间画矩形,然后旋转它(使用OpenCV,特别是{{CD1>}和^ {CD2>}),然后在某个方向上转换(偏移),使得位置也变成随机的。p>

完成旋转和平移后,您可能会将图片剪裁到您的大小,因为您可能希望从一张大约比您希望得到的结果大两倍的图片开始旋转

如果您需要更多关于旋转的帮助,这里有一个教程链接:https://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/

您的形状很简单,但我喜欢使用einsum来处理具有数百个点的形状

你可以从中得到这个想法s00只是一个多边形,由一个数组表示:

array([[ 1.5,  1.5],
       [ 0.0,  10.0],
       [ 10.0,  10.0],
       [ 10.0,  0.0],
       [ 1.5,  1.5]])

angle = 22.5
angle = np.radians(angle)
c, s = np.cos(angle), np.sin(angle)
R = np.array(((c, s), (-s, c)))
cent = np.mean(s00, axis=0)
new_cent = [10., 10.]
ch = np.einsum('ij,jk->ik', s00 - cent, R) + new_cent

Enter image description here

我做了一些调整。它们在代码中有注释:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
import matplotlib as mpl

fig = plt.figure(figsize=(4, 4))
ax = fig.add_subplot(111)

s = 100                                             # Plot size (100, 100)
min = 10                                            # Min rectangle height
max = 20                                            # Max rectangle height

degree = np.random.randint(0, 360)                  # Random rectangle rotation
background = np.random.randint(0, 256, (s, s, 3))   # Random background pixels

a = np.random.randint(min, max)                     # Little side of rectangle
b = np.random.randint(a*1.5, a*2.5)                 # Big side of rectangle

cx = np.random.randint(0 + a, s - a)                # Random horizontal location
cy = np.random.randint(0 + a, s - b)                # Random vertical location

pt = patches.Rectangle((cx,cy), a, b, color="white",  alpha=0.7)   # Rectangle with random sides and location
rt = mpl.transforms.Affine2D().rotate_deg_around(cx+a/2, cy+b/2, degree) + ax.transData # Set random rotation
pt.set_transform(rt)                                                 # Use random rotation to rotate rectangle
ax.add_patch(pt)                                                     # Add rectangle to plot


background[0, 0] = np.random.randint(200, 255, background[0, 0].shape)

plt.imshow(background)
plt.xlim(0, s)
plt.ylim(0, s)
plt.title('Random Square')
plt.show()

输出:

Output image

相关问题 更多 >