图像处理学习笔记之图像的几何变换(2)平移变换

图像的平移变换就是将图像中的所有像素点按照给定的平移量进行水平(x方向)或垂直(y方向)移动。平移变换分为两种类型:图像大小变化和图像大小不变。第一种类型保证图像平移的完整信息,第二种图像的原始信息部分可能丢失。

若点A0(x0,y0)进行平移后,移动到了A(x,y)位置,其中x方向上移动了Δxy方向上平移了Δy,那么点A的坐标为:


正变换用矩阵表示为:


同样,点A0的坐标为:


反变换使用矩阵表示为:


4*4的图像矩阵沿x轴向右平移一个单位,沿y轴向下平移一个单位,若移动后图像的大小不变,多余部分填充为白色时可表示为:


4*4的图像矩阵沿x轴向右平移一个单位,沿y轴向下平移一个单位,若移动后图像的大小变化,多余部分填充为白色时可表示为:


图像平移操作代码如下:

//平移图像,图像大小不变
void ImageTranslation(const Mat& src, Mat& dstImage, int Xoffset, int Yoffset)
{
    dstImage.create(src.size(), src.type());
    int rowNum = src.rows;
    int colNum = src.cols;
    for (int i = 0; i < rowNum; i++)
    {
        for(int j = 0; j < colNum; j++)
        {
            int x = j - Xoffset;
            int y = i - Yoffset;
            if (x > 0 && x < colNum && y > 0 && y < rowNum)
            {
                dstImage.at<Vec3b>(i, j) = src.at<Vec3b>(y, x);
            }
        }
    }
}

//平移图像,图像大小改变
void ImageTranslation2(const Mat& src, Mat& dstImage, int Xoffset, int Yoffset)
{
    int nRowNum = src.rows + abs(Yoffset);
    int nColNum = src.cols + abs(Xoffset);
    dstImage.create(nRowNum, nColNum, src.type());
    for (int i = 0; i < nRowNum; i++)
    {
        int y = i - Yoffset;
        for (int j = 0; j < nColNum; j++)
        {
            int x = j - Xoffset;
            if (x > 0 && x < src.cols && y > 0 && y < src.rows)
            {
                dstImage.at<Vec3b>(i, j) = src.at<Vec3b>(y, x);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/linshanxian/article/details/56835513
今日推荐