OPENCV 旋转图像算法-汇总
void ImgRotate(cv::Mat imgIn, float theta, cv::Mat& imgOut)
{
int oldWidth = imgIn.cols;
int oldHeight = imgIn.rows; // 源图四个角的坐标(以图像中心为坐标系原点)
float fSrcX1, fSrcY1, fSrcX2, fSrcY2, fSrcX3, fSrcY3, fSrcX4, fSrcY4;
fSrcX1 = (float)(-(oldWidth - ) / );
fSrcY1 = (float)((oldHeight - ) / );
fSrcX2 = (float)((oldWidth - ) / );
fSrcY2 = (float)((oldHeight - ) / );
fSrcX3 = (float)(-(oldWidth - ) / );
fSrcY3 = (float)(-(oldHeight - ) / );
fSrcX4 = (float)((oldWidth - ) / );
fSrcY4 = (float)(-(oldHeight - ) / ); // 旋转后四个角的坐标(以图像中心为坐标系原点)
float fDstX1, fDstY1, fDstX2, fDstY2, fDstX3, fDstY3, fDstX4, fDstY4;
fDstX1 = cos(theta) * fSrcX1 + sin(theta) * fSrcY1;
fDstY1 = -sin(theta) * fSrcX1 + cos(theta) * fSrcY1;
fDstX2 = cos(theta) * fSrcX2 + sin(theta) * fSrcY2;
fDstY2 = -sin(theta) * fSrcX2 + cos(theta) * fSrcY2;
fDstX3 = cos(theta) * fSrcX3 + sin(theta) * fSrcY3;
fDstY3 = -sin(theta) * fSrcX3 + cos(theta) * fSrcY3;
fDstX4 = cos(theta) * fSrcX4 + sin(theta) * fSrcY4;
fDstY4 = -sin(theta) * fSrcX4 + cos(theta) * fSrcY4; int newWidth = (max(fabs(fDstX4 - fDstX1), fabs(fDstX3 - fDstX2)) + 0.5);
int newHeight = (max(fabs(fDstY4 - fDstY1), fabs(fDstY3 - fDstY2)) + 0.5); imgOut.create(newHeight, newWidth, imgIn.type()); float dx = -0.5*newWidth*cos(theta) - 0.5*newHeight*sin(theta) + 0.5*oldWidth;
float dy = 0.5*newWidth*sin(theta) - 0.5*newHeight*cos(theta) + 0.5*oldHeight; int x, y;
for (int i = ; i < newHeight; i++)
{
for (int j = ; j < newWidth; j++)
{
x = float(j)*cos(theta) + float(i)*sin(theta) + dx;
y = float(-j)*sin(theta) + float(i)*cos(theta) + dy; if ((x < ) || (x >= oldWidth) || (y < ) || (y >= oldHeight))
{
if (imgIn.channels() == )
{
imgOut.at<cv::Vec3b>(i, j) = cv::Vec3b(, , );
}
else if (imgIn.channels() == )
{
imgOut.at<uchar>(i, j) = ;
}
}
else
{
if (imgIn.channels() == )
{
imgOut.at<cv::Vec3b>(i, j) = imgIn.at<cv::Vec3b>(y, x);
}
else if (imgIn.channels() == )
{
imgOut.at<uchar>(i, j) = imgIn.at<uchar>(y, x);
}
}
}
}
} void getAfterRotateRoi(cv::Mat& imgIn, int cols, int rows, Mat& imgOut)
{ imgOut.create(cols, rows, imgIn.type());
Point center(imgIn.cols / , imgIn.rows / ); imgOut = imgIn(Rect(imgIn.cols / - cols / , imgIn.rows / - rows / , cols,rows)); } Mat ImageRotate2NewSize(Mat& src, const CvPoint &_center, double angle, double scale)
{
double angle2 = angle * CV_PI / ;
int width = src.cols;
int height = src.rows; double alpha = cos(angle2) * scale;
double beta = sin(angle2) * scale; int new_width = (int)(width * fabs(alpha) + height * fabs(beta));
int new_height = (int)(width * fabs(beta) + height * fabs(alpha)); CvPoint2D32f center;
center.x = float(width / );
center.y = float(height / );
//计算二维旋转的仿射变换矩阵
Mat M = getRotationMatrix2D(center, angle, scale); // 给计算得到的旋转矩阵添加平移
M.at<double>(, ) += (int)((new_width - width) / );
M.at<double>(, ) += (int)((new_height - height) / ); // rotate
Mat dst;
warpAffine(src, dst, M, cvSize(new_width, new_height), CV_INTER_LINEAR);
return dst;
}
参考:http://www.tuicool.com/articles/RZz2Eb
OPENCV 旋转图像算法-汇总的更多相关文章
- OpenCV——旋转模糊
参考来源: 学习OpenCV:滤镜系列(5)--径向模糊:缩放&旋转 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #defi ...
- OpenCV——旋转模糊 (二)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- python 下 安装openCV
安装openCV openCV是Intel 创建的计算机视觉库,用于计算机图像处理. 安装anaconda,在命令行中输入conda install cv2/opencv 报错汇总 网络连接问题 报错 ...
- python手记(36)
#!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 import numpy as np fn=& ...
- 提升Tesseract-OCR输出的质量
图片处理 修改比例(Rescaling) 二值化(Binarisation) 去除噪点(Noise Removal) 旋转/偏移校正(Rotation / Deskewing) 边界(Borders) ...
- Excel2010数据透视表1
“透视”作为一个动词,意思是旋转.如果将数据看成是一个物体,数据透视表允许旋转数据汇总,从不同角度或观点来看它.数据透视表能够轻松地移动字段,交换字段位置,设置创建项目的特定组. 如果给出一个陌生的物 ...
- Day7 Scrum 冲刺博客
一.站立式会议1. 会议照片 2. 工作进度+燃尽图 团队成员 昨日完成工作 今日工作计划 遇到的困难 周梓波 添加键盘监听事件 由于游戏中计分规则不合理,进行重新设计 无 纪昂学 添加方块 ...
- 【图像算法OpenCV】几何不变矩--Hu矩
原文地址 http://blog.csdn.NET/daijucug/article/details/7535370 [图像算法OpenCV]几何不变矩--Hu矩 一 原理 几何矩是由Hu(Visu ...
- 图像旋转 OpenCV实现
经常对一幅图像进行旋转操作,OpenCV中提供了很方便易用的仿射变换函数warpAffine, 通过getRotationMatrix2D可以得到放射变换矩阵(矩阵大小2x3) #include &l ...
随机推荐
- 错误:Unsupported major.minor version 51.0的解决
问题: 在电脑上双击打开一个可执行的jar时报错:Unsupported major.minor version 51.0.原因是版本问题,该jar使用jdk1.7编译,而我电脑装的是jdk1.6. ...
- 在 ios 中的日期格式
var d="2017-1-1" ; new Date(d) //生成一个日期对象 这样写在 Android 中没有问题,但是在 ios 中,d 的格式不对,应该设为 2017- ...
- 123 A. Prime Permutation
链接 http://codeforces.com/contest/123/problem/A 题目 You are given a string s, consisting of small Lati ...
- shiro权限控制(二):分布式架构中shiro的实现
前言:前段时间在搭建公司游戏框架安全验证的时候,就想到之前web最火的shiro框架,虽然后面实践发现在netty中不太适用,最后自己模仿shiro写了一个缩减版的,但是中间花费两天时间弄出来的shi ...
- 使用hexo搭建个人博客
安装前提 node.js git 如果缺少以上条件,则前往相应的官网下载安装即可.. 安装hexo $ npm install hexo-cli -g 待安装完成后,执行相关命令查看hexo的信息. ...
- Jquery之isPlainObject源码分析
今天对Jquery中 isPlainObject 源码分析. 1. isPlainObject 方法的作用: 用来判断传入参数,是否是对象. 2. 源码分析:isPlainObject: funct ...
- (转)Unity控制反转和依赖注入
昨天,面试官说他们的项目使用的是Unity,我们的项目中使用的是autofac,看了一下,用法都差不多,就连方法的名字都是一样的哈,想了解的朋友可以看看这篇文章,作者讲解的挺详细的,关于autofac ...
- BZOJ 2733: [HNOI2012]永无乡 [splay启发式合并]
2733: [HNOI2012]永无乡 题意:加边,询问一个连通块中k小值 终于写了一下splay启发式合并 本题直接splay上一个节点对应图上一个点就可以了 并查集维护连通性 合并的时候,把siz ...
- 高仿QQ Xplan的H5页面
概述 这个h5的主要玩法很简单:地球自转的时候会播放背景音乐(比如海浪声),为了找到这个声音是从哪个地球上哪个地方传来的,需要长按下方的按钮,这时地球会自动转动到目标地点,然后镜头拉近,穿过云层,最后 ...
- Node.js爬取豆瓣数据
一直自以为自己vue还可以,一直自以为webpack还可以,今天在慕课逛node的时候,才发现,自己还差的很远.众所周知,vue-cli基于webpack,而webpack基于node,对node不了 ...