如何在python中使用opencv来拉直图像的旋转矩形区域? [英] How to straighten a rotated rectangle area of an image using opencv in python?

查看:844
本文介绍了如何在python中使用opencv来拉直图像的旋转矩形区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图将告诉你我想要的是什么。



我有图像中的矩形信息,宽度,高度,中心点和旋转度。现在,我想编写一个脚本来剪切它们并将它们保存为图像,但要整理它们。因为我想从图像内部显示的矩形转到外面显示的矩形。



我正在使用OpenCV python,告诉我一种方法来实现这一目标。



显示一些代码作为OpenCV Python的例子很难找到。



解决方案

您可以使用来剪切图像。



  import cv2 
import numpy as np

def subimage(image,center,theta,width,height):

'''
使用角度θ(以度为单位)围绕中心旋转OpenCV图像
然后根据宽度和高度裁剪图像。
'''

#以弧度为单位取消对theta的注释
#theta * = 180 / np.pi

shape = image.shape [:2 ]

matrix = cv2.getRotationMatrix2D(center = center,angle = theta,scale = 1)
image = cv2.warpAffine(src = image,M = matrix,dsize = shape)

x = int(center [0] - width / 2)
y = int(center [1] - height / 2)

image = image [y:y +高度,x:x +宽度]

返回图片

保持请注意 dsize 输出图像的形状。如果贴片/角度足够大,如果使用原始形状,则边缘会被切断(比较上面的图像) - 为简单起见 - 在上面完成。在这种情况下,您可以将缩放因子引入 shape (放大输出图像)和切片参考点(此处 center )。



上述功能可按如下方式使用:

  image = cv2.imread('owl.jpg')
image = subimage(image,center =(110,125),theta = 30,width = 100,height = 200)
cv2.imwrite('patch.jpg',image)


The following picture will tell you what I want.

I have the information of the rectangles in the image, width, height, center point and rotation degree. Now, I want to write a script to cut them out and save them as image, but straighten them. As in I want to go from the rectangle shown inside the image to the rectangle that is shown outside.

I am using OpenCV python, please tell me a way to accomplish this.

Kindly show some code as examples of OpenCV Python are hard to find.

解决方案

You can use the warpAffine function to rotate the image around a defined center point. The suitable rotation matrix can be generated using getRotationMatrix2D (where theta is in degrees).

You then can use Numpy slicing to cut the image.

import cv2
import numpy as np

def subimage(image, center, theta, width, height):

   ''' 
   Rotates OpenCV image around center with angle theta (in deg)
   then crops the image according to width and height.
   '''

   # Uncomment for theta in radians
   #theta *= 180/np.pi

   shape = image.shape[:2]

   matrix = cv2.getRotationMatrix2D( center=center, angle=theta, scale=1 )
   image = cv2.warpAffine( src=image, M=matrix, dsize=shape )

   x = int( center[0] - width/2  )
   y = int( center[1] - height/2 )

   image = image[ y:y+height, x:x+width ]

   return image

Keep in mind that dsize is the shape of the output image. If the patch/angle is sufficiently large, edges get cut off (compare image above) if using the original shape as--for means of simplicity--done above. In this case, you could introduce a scaling factor to shape (to enlarge the output image) and the reference point for slicing (here center).

The above function can be used as follows:

image = cv2.imread('owl.jpg')
image = subimage(image, center=(110, 125), theta=30, width=100, height=200)
cv2.imwrite('patch.jpg', image)

这篇关于如何在python中使用opencv来拉直图像的旋转矩形区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆