Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在二维阵列中随机旋转正方形?_Python_Arrays_Numpy_Matplotlib - Fatal编程技术网

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

Python 如何在二维阵列中随机旋转正方形?,python,arrays,numpy,matplotlib,Python,Arrays,Numpy,Matplotlib,我的任务是检测一个矩形,不管它在图片中的什么位置。为此,我在图片中生成了随机像素和随机生成的正方形。它的形状和大小各不相同 唯一缺少的是矩形始终是一个直的角度我希望矩形随机旋转。 我现在拥有的: 我想要什么: 我的第一个(直线)绘图代码: plt.图(figsize=(4,4)) s=100#绘图大小(100100) 最小值=10#最小矩形高度 最大值=20#最大矩形高度 background=np.random.randint(0,256,(s,s,3))#随机背景像素 a=np.rand

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

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

我现在拥有的:

我想要什么:

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

plt.图(figsize=(4,4))
s=100#绘图大小(100100)
最小值=10#最小矩形高度
最大值=20#最大矩形高度
background=np.random.randint(0,256,(s,s,3))#随机背景像素
a=np.random.randint(最小,最大)#矩形的小边
b=np.random.randint(a*1.5,a*2.5)#矩形的大边
xx,yy=np.其中(np.one((a,b))==1)#矩形的初始坐标
cx=np.random.randint(0+a,s-a)#随机水平位置
cy=np.random.randint(0+a,s-b)#随机垂直位置
背景[xx+cx,yy+cy]=np.random.randint(200255,背景[xx+cx,yy+cy].shape)
plt.imshow(背景)
plt.xlim(0,s)
plt.ylim(0,s)
打印标题(“随机方块”)
plt.show()

<>代码> > P>由于图片中有随机像素,我建议在图片中间画矩形,然后旋转它(使用OpenCV,特别是<代码> CV2 .GETRooTimeMatx2D-()/<代码>和<代码> CV2..WaPAffIn()/代码>,然后在某个方向上转换(偏移),使得位置也变为随机。 旋转和平移完成后,您可以将图片剪裁到您的大小——因为旋转,您可能希望从一张大约比您希望得到的结果大两倍的图片开始


如果你需要更多关于旋转的帮助,这里有一个链接,教程:

因为你的图片是用随机像素填充的,我建议在图片中间画矩形,然后旋转它(使用OpenCV,特别是<代码> CV2..GETRooTimeMatx2D-()/<代码>和<代码> CV2..WaPAffIn()/代码>,然后翻译(偏移)在某个方向上,位置也变得随机

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


如果您需要更多关于旋转的帮助,这里有一个教程链接:

您的形状很简单,但我喜欢用于具有数百个点的形状

你可以从中得到这个想法
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

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

你可以从中得到这个想法
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

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

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()
输出:


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

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()
输出:


您没有问任何问题。您没有问任何问题。“由于旋转,您可能希望以一张大约比您想要得到的结果大两倍的图片开始“您的意思是什么?”因为旋转,您可能希望以一张大约比您想要得到的结果大两倍的图片开始“您的意思是什么?”?