opencv 图像的几何变换

本文介绍图像处理中的基本操作,包括图像缩放、平移、旋转、转置与镜像及重映射等,并提供了详细的OpenCV函数应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图像缩放—resize()
1)resize()函数
CV_EXPORTS_W void resize(InputArray src,OutputArray dst,Size size,double fx=0,double fy=0,interpolation=INTER_LINEAR);
**src:输入图像,Mat 类型即可
dst :输出图像,当非0时 由dsize确定尺寸
dsize :Size类型,指定输出图像大小如果它等于0,由下式计算:
dsize=Size(round(fx*src.cols),round(fy*src.rows))
fx: 沿水平方向的缩放系数,默认值0,等于0时,由下式计算:
( double)dsize.width/src.cols
fy:沿垂直方向的缩放系数,默认值是0,等于0时,由下式计算:
(double)dsize.height/src.rows
interpolation:用于指定插值方式,默认为INTER_LINEAR(线性插值)
CV_INTER_NN=0,
CV_INTER_LINEAR=1,//放大
CV_INTER_CUBIC=2,//放大
CV_INTER_AREA=3,//缩小
CV_INTER_LANCZOS4=4
2)使用实例
ex1:
Mat dstimg=Mat::zeros(512,512,CV_8UC3);
Mat srcImg=imread(“1.jpg”);
resize(srcImg,dstImg,dstimg.size());
ex2:
Mat dstImg;
Mat srcImg=imread(“1.jpg”);
resize(srcImg,dstImg,Size(),0.5,0.5);
图像平移

//不改变图像大小
Mat imgTranslate(Mat &srcImg,int xOffset,int yOffset)
{
    int rows=srcImg.rows;
    int cols=srcImg.cols;
    Mat dstImg=Mat::zeros(srcImg.size(),srcImg.type());
    for(int i=0;i<rows;i++)
    {
     for(int j=0;j<cols;j++)
     {
      int x=j+xOffset;//向右平移
      int y=i+yOffset
      if(x>=0&&y>=0&&x<cols&&y<rows)//边界判断
      {
       dstImg.at<Vec3b>(y,x)=srcImg.at<Vec3b>(i,j);
      }
     }
    }
  return dstImg;
}

//改变图像大小
Mat imgTranslate2(Mat &srcImg,int x0ffset,int yOffset)
{
   int rows=srcImg.rows+abs(yOffset);
   int cols=srcImg.cols+abs(xOffset);
   Mat dstImg=Mat::zeros(rows,cols,srcImg.type());
   for(int i=0;i<rows;i++)
   {
    for(int j=0;j<cols;j++)
    {
     int x=j+xOffset;
     int y=i+yOffset;
     if(x>=0&&y>=0&&x<cols&&y<rows)
     {
      dstImg.at<Vec3b>(y,x)=srcImg.at<Vec3b>(i,j);
     }
    }
   }
  return dstImg;
}

图像旋转
1)opencv没有提供直接旋转图像的函数
2)图像旋转可能会造成图像信息的丢失
3)图像旋转可以用仿射变换来实现
4 主要用到的函数
getRotationMatrix2D()
warpAffine()
中心点坐标 角度 缩放
//!returns 2*3 affine transformation martrix for the planar rotation
CV_EXPORTS_W Mat getRotationMatrix2D(Point2f center,double angle,double scale);
flag 为插值方法 边界模式“
//warps the image using affine transformation
CV_EXPORTS_W void warpAffine(InputArray src,OutputArray dst,InputArray M,Size,dsize,int flags=INTER_LINEAR,int borderMode=BORDER_CONSTANT,
const Scalar &borderValue=Scalar());

Mat image=cv::imread("1.jpg");
Point2f center=Point2f(image.cols/2,image.rows/2);//旋转中心
double angle=45;//旋转角度
double scale=0.5;//缩放尺度
Mat rotatemat;
rotateMat=getRotationMatrix2D(center,angle,scale);//获得旋转矩阵
Mat rotateImg;
warpAffine(image,rotateImg,rotateMat,Size(1200,1800));
imshow("dst",rotateImg);
imwrite("rotate.jpg",rotateImg);
waitKey(0)

3)转置和镜像
用到的函数 transpose(),filp()
可以实现转置和镜像变换,以及90度 180度旋转
//!transposes the matrix
//行和列做翻转
CV_EXPORTS_W void transpose(InputArray src,OutputArray dst);
//!reverses the order of the rows ,columns or both in a matrix
CV_EXPORTS_W void filp(InputArray src,OutputArray dst,int filpCode);
filpCode=0,垂直翻转(沿X轴翻转)
filpCode>0 ,水平翻转(沿Y轴旋转)
filCode<0 水平垂直翻转(180度中心对称)
90度 要先做转置 ,再垂直翻转

重映射—–remap()
1)reamp()函数
重映射是指把一个图像中的一个位置的像素通过映射关系转换到另一个图像的指定位置,对于输入原图像f(x,y)目标图像g(x,y)映射关系为T,则满足下式;
g(x,y)=T(f(x,y));
CV_EXPORTS_W void remap(InputArray src,OutputArray dst,InputArray map1,InputArray map 2,int interpolation,int borderMode=BORDER_CONSTANT,const Scalar &borderValue=Scalar());
**map1:表示(x,y)点的坐标或x坐标,CV_16SC2,CV_32FC1,CV_32FC2类型
**map2:表示(x,y)点y坐标,如果map1为(x,y),map2可以不用,可以是CV_16UC1,CV_32FC1
interpolation:表示插值方法
borderMode:表示边界插值类型
borderValue:表示插值数值

//波浪效果  扭曲图像的变换
Mat img=imread("1.jpg");
imshow("src",img);
Mat dst;
Mat xMatImg(img.size(),CV_32FC1);
Mat yMatImg(img.size(),CV_32FC1);
int rows=img.rows;
int cols=img.cols;
for(int i=0;i<rows;i++)
{
  for(int j=0;j<cols;j++)
  {
   xMatImg.at<float>(i,j)=j;//列操作
   yMayImg.at<float>(i,j)=i+5*sin(j/10.0);//行操作
  }
  reamp(img,dst,xMatImg,yMatImg,CV_INTER_LINEAR);
  imshow("dst",dst);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值