- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章opencv图片的任意角度旋转实现示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
。
1.如果O点为圆心,则点P绕点O旋转redian弧度之后,点P的坐标变换为点Q的计算公式为
Q.x=P.x*cos(redian)-P.y*sin(redian) 。
Q.y=P.x*sin(redian)+P.y*cos(redian) 。
redian表示的为弧度 。
弧度与角度的变换公式为
redian=pi*180/angle 。
2. 如果O点不是圆心,则点P绕点O旋转redian弧度之后,点P的坐标变换为Q的计算公式如下
Q.x=(P.x-O.x)*cos(redian)-(P.y-O.y)*sin(redian)+O.x 。
Q.y=(P.x-O.x)*sin(redian)+(P.y-O.y)*cos(redian)+O.y 。
。
1.首先默认旋转45度时,所扩展的图像最大,即为根号2倍的长或宽的最大值,将图像填充到可能达到的最大 。
2 使用getRotationMatrix2D函数求取旋转矩阵,使用warpAffine函数旋转矩阵 。
3 求旋转之后包括图像的最大的矩形 。
4 删除多余的黑色边框 。
。
#include <iostream>#include<opencv2/opencv.hpp> using namespace cv; void rotate_arbitrarily_angle(Mat &src,Mat &dst,float angle){ float radian = (float) (angle /180.0 * CV_PI); //填充图像 int maxBorder =(int) (max(src.cols, src.rows)* 1.414 ); //即为sqrt(2)*max int dx = (maxBorder - src.cols)/2; int dy = (maxBorder - src.rows)/2; copyMakeBorder(src, dst, dy, dy, dx, dx, BORDER_CONSTANT); //旋转 Point2f center( (float)(dst.cols/2) , (float) (dst.rows/2)); Mat affine_matrix = getRotationMatrix2D( center, angle, 1.0 );//求得旋转矩阵 warpAffine(dst, dst, affine_matrix, dst.size()); //计算图像旋转之后包含图像的最大的矩形 float sinVal = abs(sin(radian)); float cosVal = abs(cos(radian)); Size targetSize( (int)(src.cols * cosVal +src.rows * sinVal), (int)(src.cols * sinVal + src.rows * cosVal) ); //剪掉多余边框 int x = (dst.cols - targetSize.width) / 2; int y = (dst.rows - targetSize.height) / 2; Rect rect(x, y, targetSize.width, targetSize.height); dst = Mat(dst,rect);} int main() { cv::Mat src=cv::imread("../3.png"); cv::Mat dst; rotate_arbitrarily_angle(src,dst,30); cv::imshow("src",src); cv::imshow("dst",dst); cv::waitKey(0); return 0;}
原图 。
绕中心点旋转30度的结果 。
需要注意的是该方法仅适用于水平图像旋转到有角度的图像,至于可以随意旋转角度的方法我现在还不知道如何完成,以后有机会再做. 。
以上做法还有个最大的缺点是在旋转之后像素大小发生了变化,如果你要对像素操作就会产生很多问题,接下来的代码会将像素固定下来,不过也是针对旋转到一定角度之后再返回到水平位置的代码,具有很大的局限性,研究明白之后再更新其他情况 。
cv::Mat rotate_arbitrarily_angle1(cv::Mat matSrc, float angle, bool direction,int height,int width) { float theta = angle * CV_PI / 180.0; int nRowsSrc = matSrc.rows; int nColsSrc = matSrc.cols; // 如果是顺时针旋转 if (!direction) theta = 2 * CV_PI - theta; // 全部以逆时针旋转来计算 // 逆时针旋转矩阵 float matRotate[3][3]{ { std::cos(theta), -std::sin(theta), 0}, {std::sin(theta), std::cos(theta), 0 }, {0, 0, 1} }; float pt[3][2]{ { 0, nRowsSrc }, {nColsSrc, nRowsSrc}, {nColsSrc, 0} }; for (int i = 0; i < 3; i++) { float x = pt[i][0] * matRotate[0][0] + pt[i][1] * matRotate[1][0]; float y = pt[i][0] * matRotate[0][1] + pt[i][1] * matRotate[1][1]; pt[i][0] = x; pt[i][1] = y; } // 计算出旋转后图像的极值点和尺寸 float fMin_x = std::min(std::min(std::min(pt[0][0], pt[1][0]), pt[2][0]), (float)0.0); float fMin_y = std::min(std::min(std::min(pt[0][1], pt[1][1]), pt[2][1]), (float)0.0); float fMax_x = std::max(std::max(std::max(pt[0][0], pt[1][0]), pt[2][0]), (float)0.0); float fMax_y = std::max(std::max(std::max(pt[0][1], pt[1][1]), pt[2][1]), (float)0.0); int nRows = cvRound(fMax_y - fMin_y + 0.5) + 1; int nCols = cvRound(fMax_x - fMin_x + 0.5) + 1; int nMin_x = cvRound(fMin_x + 0.5); int nMin_y = cvRound(fMin_y + 0.5); // 拷贝输出图像 cv::Mat matRet(nRows, nCols, matSrc.type(), cv::Scalar(0)); for (int j = 0; j < nRows; j++) { for (int i = 0; i < nCols; i++) { // 计算出输出图像在原图像中的对应点的坐标,然后复制该坐标的灰度值 // 因为是逆时针转换,所以这里映射到原图像的时候可以看成是,输出图像 // 到顺时针旋转到原图像的,而顺时针旋转矩阵刚好是逆时针旋转矩阵的转置 // 同时还要考虑到要把旋转后的图像的左上角移动到坐标原点。 int x = (i + nMin_x) * matRotate[0][0] + (j + nMin_y) * matRotate[0][1]; int y = (i + nMin_x) * matRotate[1][0] + (j + nMin_y) * matRotate[1][1]; if (x >= 0 && x < nColsSrc && y >= 0 && y < nRowsSrc) { matRet.at<uchar>(j, i) = matSrc.at<uchar>(y, x); } } } if(direction== false){//当需要顺时针旋转回水平位置时 int x = (matRet.cols -width) / 2; int y = (matRet.rows -height) / 2; //width和height是水平条件下图像的宽高 cv::Rect rect(x, y, width, height); matRet = cv::Mat(matRet,rect); } return matRet;}
到此这篇关于opencv图片的任意角度旋转实现示例的文章就介绍到这了,更多相关opencv图片任意角度旋转内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/u013263891/article/details/83932479 。
最后此篇关于opencv图片的任意角度旋转实现示例的文章就讲到这里了,如果你想了解更多关于opencv图片的任意角度旋转实现示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
...沮丧。我希望我的游戏仅在横向模式下运行。我已将适当的键/值添加到 Info.plist 文件中,以强制设备方向在启动时正确。 我现在正在尝试旋转 OpenGL 坐标空间以匹配设备的坐标空间。我正
我如何创建一个旋转矩阵,将 X 旋转 a,Y 旋转 b,Z 旋转 c? 我需要公式,除非您使用的是 ardor3d api 的函数/方法。 矩阵是这样设置的 xx, xy, xz, yx, yy, y
假设我有一个包含 3 个 vector 的类(一个用于位置,一个用于缩放,一个用于旋转)我可以使用它们生成一个变换矩阵,该矩阵表示对象在 3D 空间中的位置、旋转和大小。然后我添加对象之间的父/子关系
所以我只是在玩一个小的 javascript 游戏,构建一个 pacman 游戏。你可以在这里看到它:http://codepen.io/acha5066/pen/rOyaPW 不过我对旋转有疑问。你
在我的应用程序中,我有一个 MKMapView,其中显示了多个注释。 map 根据设备的航向旋转。要旋转 map ,请执行以下语句(由方法 locationManager 调用:didUpdateHe
使用此 jquery 插件时:http://code.google.com/p/jqueryrotate/wiki/Documentation我将图像旋转 90 度,无论哪个方向,它们最终都会变得模糊
我有以下代码:CSS: .wrapper { margin:80px auto; width:300px; border:none; } .square { widt
我只想通过小部件的轴移动图像并围绕小部件的中心旋转(就像任何数字绘画软件中的 Canvas ),但它围绕其左顶点旋转...... QPainter p(this); QTransform trans;
我需要先旋转图像,然后再将其加载到 Canvas 中。据我所知,我无法使用 canvas.rotate() 旋转它,因为它会旋转整个场景。 有没有好的JS方法来旋转图片? [不依赖于浏览器的方式] 最
我需要知道我的 Android 设备屏幕何时从一个横向旋转到另一个横向(rotation_90 到 rotation_270)。在我的 Android 服务中,我重新实现了 onConfigurati
**摘要:**本篇文章主要讲解Python调用OpenCV实现图像位移操作、旋转和翻转效果,包括四部分知识:图像缩放、图像旋转、图像翻转、图像平移。 本文分享自华为云社区《[Python图像处理] 六
我只是在玩MTKView中的模板设置;并且,我一直在尝试了解以下内容: 相机的默认位置。 使用MDLMesh和MTKMesh创建基元时的默认位置。 为什么轮换还涉及翻译。 相关代码: matrix_f
我正在尝试使用包 dendexend 创建一个树状图。它创建了非常好的 gg 树状图,但不幸的是,当你把它变成一个“圆圈”时,标签跟不上。我将在下面提供一个示例。 我的距离对象在这里:http://s
我想将一个完整的 ggplot 对象旋转 90°。 我不想使用 coord_flip因为这似乎会干扰 scale="free"和 space="free"使用刻面时。 例如: qplot(as.fac
我目前可以通过首先平移到轴心点然后执行旋转最后平移回原点来围绕轴心点旋转。在我的例子中,我很容易为肩膀做到这一点。但是,我不知道如何为前臂添加绕肘部的旋转。 我已经尝试了以下围绕肘部旋转的前臂: 平移
我想使用此功能旋转然后停止在特定点或角度。现在该元素只是旋转而不停止。代码如下: $(function() { var $elie = $("#bkgimg");
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我正在尝试创建一个非常简单的关键帧动画,其中图形通过给定的中点从一个角度旋转到另一个角度。 (目的是能够通过大于 180 度的 OBTUSE 弧角来制作旋转动画,而不是让动画“作弊”并走最短路线,即通
我需要旋转 NSView 实例的框架,使其宽度变为其高度,其高度变为其宽度。该 View 包含一个字符串,并且该字符串也被旋转,这一点很重要。 我查看了 NSView 的 setFrameRotati
我正在编写一个脚本,用于在 javascript 中旋转/循环浏览图像,同时遵守循环浏览图像的次数限制。我所拥有的如下: var delay = 3000; //6000 = change to
我是一名优秀的程序员,十分优秀!