Open CV实现图像的旋转,平移,缩放

#include "stdafx.h"
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include"cv.h"
#include"highgui.h"


using namespace cv;






int main()
{
Mat image;   //原图像
    Mat result;  //结果图像
    CvSize size;

image = imread("E:\\pic\\pic2.jpg");//读取图像


size.width = image.cols;            //获取原图像大小
size.height = image.rows;
int width=size.width;
int height=size.height;


result.create(size.height, size.width, CV_8UC3);


int num=0;
printf("选择操作:\n");
while(num!=4){
printf("1.图像缩放 2.图像旋转 3.图像平移 4.退出\n");
scanf("%d",&num);


if(num==1){


    //图像缩放
Mat result1;
result1.create(size.height, size.width, CV_8UC3);
double scale;
printf("请输入缩放倍数:\n");       //获取缩放倍数
scanf("%lf",&scale);
Size dsize = Size(image.cols*scale,image.rows*scale);  //计算缩放矩阵
resize(image, result1, dsize); 
 //结果显示
namedWindow( "image", 1 ); 
namedWindow( "result", 1 );
imshow( "image", image );
imshow( "result", result1 );


}


if(num==2)
{
    //图像旋转
       


    double angleDu = 0;  // 旋转角度  
printf("请输入旋转角度(0-360):\n");
scanf("%lf",&angleDu);


    double scale = 1; //不缩放
double angle = angleDu * CV_PI / 180.; // 转化为弧度 
    double a = sin(angle), b = cos(angle); 

//计算旋转后的图像大小
    int width_rotate= int(size.height * fabs(a) + size.width * fabs(b)); 
int height_rotate=int(size.width * fabs(a) + size.height * fabs(b)); 
for(int i=1;i<5;i++)
{ if(angleDu/i/90==0)
{
     width_rotate=width;
 height_rotate=height;}}
    
int dx=int((width_rotate-width)/2);
int dy=int((height_rotate-height)/2);


//将图像放在一个和结果图像一样大的图像中
Vec3b *p;
Mat temp;
temp=Mat::zeros( height_rotate, height_rotate, CV_8UC3 );
//temp.create(height_rotate,width_rotate,CV_8UC3);
for(int i=0;i<height;i++)
{p = temp.ptr<Vec3b>(i+dy);
for(int j=0;j<width;j++)
 {
 p[j+dx] = image.ptr<Vec3b>(i)[j];
     }
       }




    Mat rotateMat;  //计算缩放矩阵
//rotateMat.create(height_rotate,width_rotate,CV_8UC3);
Point2f center = Point2f(temp.cols / 2, temp.rows / 2);  // 旋转中心
    rotateMat = getRotationMatrix2D(center, angleDu, scale);  
    


    Mat result2;  //得到结果矩阵
result2.create(height_rotate,width_rotate,CV_8UC3);
    warpAffine(temp, result2, rotateMat, result2.size()); 





//rotateImg.create(height_rotate,width_rotate,CV_8UC3);


//结果显示
namedWindow( "image", 1 ); 
namedWindow( "result", 1 );
imshow( "image", image );
imshow( "result", result2 );
}


if(num==3)
{
//图像平移
Mat result3;
result3.create(size.height, size.width, CV_8UC3);
printf("请输入水平方向平移量:\n");
int dx=0;
scanf("%d",&dx);
printf("请输入竖直方向平移量:\n");
int dy=0;
scanf("%d",&dy);
Vec3b *q;
for(int i=0;i<height;i++)
{q = result3.ptr<Vec3b>(i);
for(int j=0;j<width;j++)
 {int x=j-dx;
      int y=i-dy;
 if(x>=0&&y>=0&&x<width&&y<height)
 q[j] = image.ptr<Vec3b>(y)[x];
      }
         }


//结果显示
namedWindow( "image", 1 ); 
namedWindow( "result", 1 );
imshow( "image", image );
imshow( "result", result3 );
    }






//imwrite("../result.jpg", result);
waitKey(0); // wait for key to close the windows
}
return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值