我的任务是检测一个矩形,不管它在图片中的什么位置。为此,我在图片中生成了随机像素和随机生成的正方形。它的形状和大小各不相同
唯一缺少的是矩形始终是一个直的角度我希望矩形随机旋转。
我现在拥有的:
我想要什么:
我的第一个(直线)绘图代码:
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()
因为你的图片是用随机像素填充的,所以我建议在图片中间画矩形,然后旋转它(使用OpenCV,特别是{{CD1>}和^ {CD2>}),然后在某个方向上转换(偏移),使得位置也变成随机的。p>
完成旋转和平移后,您可能会将图片剪裁到您的大小,因为您可能希望从一张大约比您希望得到的结果大两倍的图片开始旋转
如果您需要更多关于旋转的帮助,这里有一个教程链接:https://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/
您的形状很简单,但我喜欢使用einsum来处理具有数百个点的形状
你可以从中得到这个想法
s00
只是一个多边形,由一个数组表示:我做了一些调整。它们在代码中有注释:
输出:
相关问题 更多 >
编程相关推荐