首页 > 其他分享 >opencv 以任意角度旋转图片

opencv 以任意角度旋转图片

时间:2023-04-22 15:22:05浏览次数:31  
标签:center img 旋转 opencv np import 任意 cv

opencv中有两种方式来对图像进行旋转操作:

  1. 使用rotate函数,通过rotateCode来控制图像的旋转角度,而rotateCode的取值只有90/180/270三种,自由度不够高。
  2. 使用仿射变换warpAffine来以任意角度的旋转图片。但旋转后的图片会被裁切,丢失了部分信息,代码与效果图如下所示:
import numpy as np
import cv2 as cv

img = cv.imread("lena.png", cv.IMREAD_UNCHANGED)
h, w, c = img.shape
m = cv.getRotationMatrix2D(center=(w / 2, h / 2), angle=45, scale=1)
ret = cv.warpAffine(img, m, img.shape[1::-1])
cv.imwrite("wrapAffine.png", ret)
lena.png wrapAffine.png

为解决上述问题,就有了如下代码

import numpy as np
import cv2 as cv
from typing import Tuple

def rotate(img: np.ndarray, angle: float, center: Tuple[float, float] = None) -> np.ndarray:
    h, w, c = img.shape
    # Defining the center point of rotation as the center of the image if center is not provided
    center = (w / 2, h / 2) if center is None else center
    # Ensuring that the center point lies within the image dimensions
    assert 0 <= center[0] <= w and 0 <= center[1] <= h

    # Adding a border of zeros around the input image to avoid cropping after rotation
    top, bottom, left, right = h, h, w, w
    bordered = cv.copyMakeBorder(img, top, bottom, left, right,
                                 borderType=cv.BORDER_CONSTANT, value=cv.BORDER_TRANSPARENT)
    # Computing the rotation matrix with respect to the center point of the image
    m = cv.getRotationMatrix2D(center=(center[0] + left, center[1] + top), angle=angle, scale=1)
    # Applying the rotation to the bordered image using the computed rotation matrix
    ret = cv.warpAffine(bordered, m, bordered.shape[1::-1], borderMode=cv.BORDER_TRANSPARENT)
    #  Removing the zero-padded border from the rotated image
    x, y = np.where(ret[..., -1] != 0)
    return ret[x.min():x.max(), y.min():y.max(), :]

使用rotate函数绕中心旋转45°的图片如下:

rotate

标签:center,img,旋转,opencv,np,import,任意,cv
From: https://www.cnblogs.com/5p2o/p/17343095.html

相关文章

  • 【Unity】旋转木马
    对三角函数进行实际操作,需要对木马移动进行平滑插值木马起伏采用的Cos函数的周期实现usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassMerryGoRound:MonoBehaviour{publicTransformnode;publicfloatrudis;......
  • iOS 屏幕旋转的设置方法
    VC上屏幕旋转的方式有2种1.因重力导致的屏幕旋转条件:shouldAutorotate返回true,设备开启了屏幕旋转开关。设备发生重力旋转。2.单页面强制旋转条件:无。设置设备旋转方向。NSNumber*orientationTarget=[NSNumbernumberWithInteger:isLaunchScreen?UIInterfaceOrient......
  • OpenCv人脸检测技术-(实现抖音特效-给人脸戴上墨镜)
    OpenCv人脸检测技术-(实现抖音特效-给人脸戴上墨镜)本文章用的是Python库里的OpenCv。OpenCv相关函数说明importcv2#导入OpenCv库cv2.imread(filename)#读取图像object=cv2.CascadeClassifier()#括号里面填Haar级联分类器"""CascadeClassifier,是Opencv中做人脸检......
  • Qt5.14+CMake3.22+OpenCV4.5
    原文地址zhuanlan.zhihu.com下载链接首先需要下载对应的软件,下载链接和我选择的版本如下(仅作参考):QtIndexof/archive/qt/5.14/5.14.2,qt-opensource-windows-x86-5.14.2.exeCMakeDownload|CMake,cmake-3.23.0-windows-x86_64.msi注意x86指的是32位系统;x64指的是64位系......
  • OpenCV实现银行卡数字识别
    目录1.预处理模板图像(1)读入模板图像(2)化为灰度图(3)化为二值图(4)画出0-9这10个数字的外轮廓(5)计算外接矩形并且resize成合适大小2.预处理银行卡图像(1)读入需识别的银行卡并化为灰度图(2)礼帽操作(3)梯度运算(Sobel算子)(4)闭操作(5)阈值分割(6)再进行闭操作(7)计算外轮廓(8)计......
  • opencv 407 resize 类型
    enumInterpolationFlags{/**nearestneighborinterpolation*/INTER_NEAREST=0,/**bilinearinterpolation*/INTER_LINEAR=1,/**bicubicinterpolation*/INTER_CUBIC=2,/**resamplingusingpixel......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • 使用 OpenCV4 和 C++ 构建计算机视觉项目:1~5
    原文:BuildingComputerVisionProjectswithOpenCV4andC++协议:CCBY-NC-SA4.0译者:飞龙本文来自【ApacheCN计算机视觉译文集】,采用译后编辑(MTPE)流程来尽可能提升效率。当别人说你没有底线的时候,你最好真的没有;当别人说你做过某些事的时候,你也最好真的做过。一、Open......
  • 二分查找:剑指 Offer 11. 旋转数组的最小数字
    题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。例如,数组 [3,4,5,1,2]为[1,2,3,4,5]的一次旋转,该数组的最......