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

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

问题描述

下图会告诉你我想要什么.

我有图像中矩形的信息(宽度、高度、中心点和旋转度).现在,我想编写一个脚本来将它们剪下来并将它们保存为图像,同时还要将它们拉直.如,我想从图像内部显示的矩形转到外部显示的矩形.

我正在使用 OpenCV Python.告诉我实现此目的的方法.

展示一些代码,因为 OpenCV Python 的例子很难找到.

解决方案

您可以使用 来剪切图像.

导入 cv2将 numpy 导入为 np定义子图像(图像,中心,θ,宽度,高度):'''以角度 theta(以度为单位)围绕中心旋转 OpenCV 图像然后根据宽度和高度裁剪图像.'''# 取消以弧度表示的 theta 的注释#theta *= 180/np.pishape = ( image.shape[1], image.shape[0] ) # cv2.warpAffine 期望形状在(长度,高度)矩阵 = cv2.getRotationMatrix2D(中心=中心,角度=θ,比例=1)图像 = cv2.warpAffine(src=图像,M=矩阵,dsize=形状)x = int( center[0] - width/2 )y = int( 中心[1] - 高度/2)图像 = 图像[ y:y+height, x:x+width ]返回图像

请记住,dsize输出 图像的形状.如果补丁/角度足够大,如果使用原始形状作为简单的方法,边缘会被截断(比较上面的图像)——上面已经完成.在这种情况下,您可以为 shape(放大输出图像)和切片的参考点(此处为 center)引入缩放因子.

以上函数可以如下使用:

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

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 an image, but straighten them as well. 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[1], image.shape[0] ) # cv2.warpAffine expects shape in (length, height)

   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天全站免登陆