【发布时间】:2021-11-08 02:12:22
【问题描述】:
图像中的对象处于不同的方向。我想改变垂直方向的所有对象方向。代码如下所示。代码并非所有面向对象都是垂直的。 [Image]
image = cv2.imread(r'C:\Users\Desktop\Sam.jpg')
Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# create a binary image
_, binary = cv2.threshold(Gray, 127, 255, cv2.THRESH_BINARY_INV)
# find the contours from the binary image
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
[-2:]
for i in range(len(contours)):
rect = cv2.minAreaRect(contours[i])
angle=rect[2]
print('rect',rect)
print('ANgle:',angle)
if angle>0:
rangle = 90-angle
print('rangle:',rangle)
else:
rangle =angle
print('rangle:',rangle)
rotate_img= ndimage.rotate(cimg, rangle,reshape=True)
print('rotate_img shape:',rotate_img.shape)
plt.figure(figsize=(8, 8))
plt.title('rotate_img')
plt.imshow(rotate_img, cmap='gray')
plt.show()
【问题讨论】:
-
如何对图像进行阈值处理并使用skimage.label 查找图像中的每个组件。接下来,找到每个组件的质心和围绕它的边界框。在 ROI 中找到对象的角度并旋转对象,使其垂直。最后将旋转后的 ROI 粘贴到旧图像或新图像上,使旋转对象的质心位于原始对象的质心。
-
@yannziselman 谢谢你的建议。是的,我想找到旋转角度,使图像中的对象始终垂直。
-
您可以通过查找属于对象的像素的 x 索引与其 y 索引之间的相关性来找到角度
-
@yannziselman 能否详细描述一下。
标签: image image-processing computer-vision rotation image-rotation