图像旋转代码
2017-05-09 12:53
141 查看
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 - 1) / 2);
fSrcY1 = (float) ( (oldHeight - 1) / 2);
fSrcX2 = (float) ( (oldWidth - 1) / 2);
fSrcY2 = (float) ( (oldHeight - 1) / 2);
fSrcX3 = (float) (- (oldWidth - 1) / 2);
fSrcY3 = (float) (- (oldHeight - 1) / 2);
fSrcX4 = (float) ( (oldWidth - 1) / 2);
fSrcY4 = (float) (- (oldHeight - 1) / 2);
// 旋转后四个角的坐标(以图像中心为坐标系原点)
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=0; i<newHeight; i++)
{
for (int j=0; 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<0) || (x>=oldWidth) || (y<0) || (y>=oldHeight))
{
if (imgIn.channels() == 3)
{
imgOut.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);
}
else if (imgIn.channels() == 1)
{
imgOut.at<uchar>(i,j) = 0;
}
}
else
{
if (imgIn.channels() == 3)
{
imgOut.at<cv::Vec3b>(i,j) = imgIn.at<cv::Vec3b>(y,x);
}
else if (imgIn.channels() == 1)
{
imgOut.at<uchar>(i,j) = imgIn.at<uchar>(y,x);
}
}
}
}
}
{
int oldWidth = imgIn.cols;
int oldHeight = imgIn.rows;
// 源图四个角的坐标(以图像中心为坐标系原点)
float fSrcX1,fSrcY1,fSrcX2,fSrcY2,fSrcX3,fSrcY3,fSrcX4,fSrcY4;
fSrcX1 = (float) (- (oldWidth - 1) / 2);
fSrcY1 = (float) ( (oldHeight - 1) / 2);
fSrcX2 = (float) ( (oldWidth - 1) / 2);
fSrcY2 = (float) ( (oldHeight - 1) / 2);
fSrcX3 = (float) (- (oldWidth - 1) / 2);
fSrcY3 = (float) (- (oldHeight - 1) / 2);
fSrcX4 = (float) ( (oldWidth - 1) / 2);
fSrcY4 = (float) (- (oldHeight - 1) / 2);
// 旋转后四个角的坐标(以图像中心为坐标系原点)
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=0; i<newHeight; i++)
{
for (int j=0; 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<0) || (x>=oldWidth) || (y<0) || (y>=oldHeight))
{
if (imgIn.channels() == 3)
{
imgOut.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);
}
else if (imgIn.channels() == 1)
{
imgOut.at<uchar>(i,j) = 0;
}
}
else
{
if (imgIn.channels() == 3)
{
imgOut.at<cv::Vec3b>(i,j) = imgIn.at<cv::Vec3b>(y,x);
}
else if (imgIn.channels() == 1)
{
imgOut.at<uchar>(i,j) = imgIn.at<uchar>(y,x);
}
}
}
}
}
相关文章推荐
- 数字图像处理基础(十一)---缩放、角度旋转和仿射变换及代码实现
- 图像旋转c代码实现(90度, -90度, 180度)
- Delphi实现图像文本旋转特效完整实例代码
- opencv 拉伸、扭曲、旋转图像-仿射变换 opencv1 / opencv2 / python cv2(代码)
- 旋转图像代码更新(减小了旋转失真)
- 图像旋转任意角度,python代码,亲测可用
- OpenCV:OpenCV图像旋转的代码
- 利用OpenCV实现旋转文本图像矫正的原理及OpenCV代码
- Opencv实现图像旋转,非常简单,几行代码
- opencv实现图像任意角度旋转的算法解析及代码实现
- mtk平台上旋转图像的代码,可以任意角度旋转。
- 最近邻方法进行图像旋转 c++代码 旋转后图像内容无损失
- Android ImageUtils 图片工具类,包括:小米从相册调图像空指针,三星旋转照片,压缩图片,保存Bitmap到本地,获取本地视频的缩略图,毛化玻璃效果,代码以后会更新
- 图像旋转和缩放的代码编写
- 最近邻图像旋转代码 灰度图像 c++实现
- python图像旋转代码
- opencv 拉伸、扭曲、旋转图像-仿射变换 opencv1 / opencv2 / python cv2(代码)
- rotate an image 图像旋转代码
- 图像旋转以及C代码实现