图像处理-Opencv入门(3)-图像的基本运算(2)-几何映射变换-平移映射变换

一、图像的几何变换

1.1 图像的几何变换

图像的几何变换包括图像的平移、旋转、缩小、放大和镜像映射。

1.2 功能

图像的几何变换改变图像的空间关系,但不改变图像的色彩属性。

1.3 数学关系

图像的几何变换一般定义为:

f(x,y) = g(u,v)

其中,g(u,v)为输入图像,f(x,y)为输出图像。坐标之间事平移的映射关系。

1.4 主代码

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

// 函数声明
void ImgMove(Mat& inputImag_1, Mat& outputImage, int dx, int dy);

int main()
{
    // 载入图像并显示
    Mat originImage_boy = imread("baby.png", 1);
    imshow("original_boy", originImage_boy);

    // 创建效果图
    Mat resultImage;
    resultImage.create(originImage_boy.rows, originImage_boy.cols, originImage_boy.type());

    // 记录时间
    double timeClock = static_cast<double>(getTickCount());

    // 图像平移变换, 可定义和调用不同的函数
    ImgMove(originImage_boy, resultImage, 30, 30);

    // 输出时间
    timeClock = ((double)getTickCount() - timeClock) / getTickCount();
    cout << "run time:" << timeClock << "seconds" << endl;

    imshow("result_Move",resultImage);
    waitKey(0);
    return 0;
}

2.1 图像平移变换

图像的平移变换就是在变换前后像素的水平和垂直的坐标的变换。

2.2 图像平移变换的数学关系-矩阵形式表示平移像素前后的关系

(1)平移后图像的大小变化

后向映射:计算平移后的像素在原图像中的坐标

2.2 编程实例-图像平移变换函数

// 使用动态地址计算图像的平移变换-变换后图像的大小变化
void ImgMove(Mat& inputImage_1, Mat& outputImage, int dx, int dy)
{
    outputImage = inputImage_1.clone();

    int rowsNum = outputImage.rows;
    int colsNum = outputImage.cols;

    for (int i = 0; i < rowsNum; i++)
    {
        for (int j = 0; j < colsNum; j++)
        {
        	// 计算像素在原图像中的坐标
            int x0 = i - dx;
            int y0 = j - dy;

            // 映射后的坐标在图像范围内
            if ((x0 >= 0) && (x0 < colsNum) && (y0 >= 0) && (y0 < rowsNum))
            {
            	outputImage.at<Vec3b>(i,j) = inputImage_1.at<Vec3b>(x0,y0);

            }
            else
            {
            	outputImage.at<Vec3b>(i,j) = 255;
            }
        }
    }
}

2.3 图像平移变换结果-图像大小变化

(2)图像平移变换后的大小不变

原理雷同

2.4 编程实例-图像平移变换-图像大小不变

// 使用动态地址计算图像的平移变换-变换后图像的大小不变
void ImgMove(Mat& inputImage_1, Mat& outputImage, int dx, int dy)
{
    int rowsNum = outputImage.rows + abs(dx);
    int colsNum = outputImage.cols + abs(dy);
    outputImage.create(rowsNum, colsNum, inputImage_1.type());

    for (int i = 0; i < rowsNum; i++)
    {
        for (int j = 0; j < colsNum; j++)
        {
        	// 计算像素在原图像中的坐标
            int x0 = i - dx;
            int y0 = j - dy;

            // 映射后的坐标在图像范围内
            if ((x0 >= 0) && (x0 < colsNum) && (y0 >= 0) && (y0 < rowsNum))
            {
            	outputImage.at<Vec3b>(i,j) = inputImage_1.at<Vec3b>(x0,y0);

            }
            else
            {
            	outputImage.at<Vec3b>(i,j) = 255;
            }
        }
    }
}

2.5 图像平移变换-图像大小不变结果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值