minAreaRect OpenCV [Python]返回的裁剪矩形 [英] Crop Rectangle returned by minAreaRect OpenCV [Python]
本文介绍了minAreaRect OpenCV [Python]返回的裁剪矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
minAreaRect
返回一个旋转的矩形。如何裁剪矩形内部图像的这一部分?
minAreaRect
in OpenCV returns a rotated rectangle. How do I crop this part of the image which is inside the rectangle?
boxPoints
返回旋转矩形的角点的坐标,以便通过循环访问像素通过框内的点,但有没有更快的方法在Python中裁剪?
boxPoints
returns the co-ordinates of the corner points of the rotated rectangle so one can access the pixels by looping through the points inside the box, but is there a faster way to crop in Python?
编辑
参见代码
在我的答案中。
推荐答案
您还没有提供示例代码,所以我也在没有代码的情况下回答。
您可以按以下步骤操作:
You have not given sample code, so I am answering without code also. You could proceed as follows:
- 从矩形的角落确定旋转角度α与水平轴的对比。
- 按alpha旋转图像,使裁剪的矩形与图像边框平行。确保临时图像的大小更大,以免信息丢失(参见:无图像旋转图像裁剪OpenCV )
- 使用numpy切片裁剪图像(cf:如何使用Python在OpenCV中裁剪图像)
- 通过-alpha转回图像。
- From corners of rectangle, determine angle alpha of rotation against horizontal axis.
- Rotate image by alpha so that cropped rectangle is parallel to image borders. Make sure that the temporary image is larger in size so that no information gets lost (cf: Rotate image without cropping OpenCV)
- Crop image using numpy slicing (cf: How to crop an image in OpenCV using Python)
- Rotate image back by -alpha.
这篇关于minAreaRect OpenCV [Python]返回的裁剪矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文