如何使用OpenCV库进行图像平移操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计863个文字,预计阅读时间需要4分钟。
本文以OpenCV实现图像平移的具体代码为例,供大家参考。图像平移指的是将图像沿水平或垂直方向进行移动。平移变换公式如下:
对于原始图像中的点 (x, y),平移后的点 (x', y') 的坐标计算公式为:x'=x + dxy'=y + dy
其中,dx 和 dy 分别表示沿 x 轴和 y 轴的平移量。
以下是一个简单的图像平移代码示例:
pythonimport cv2import numpy as np
读取图像image=cv2.imread('example.jpg')
设置平移量dx=50dy=30
创建平移矩阵M=np.float32([[1, 0, dx], [0, 1, dy]])
应用平移变换translated_image=cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
显示结果cv2.imshow('Original Image', image)cv2.imshow('Translated Image', translated_image)cv2.waitKey(0)cv2.destroyAllWindows()
在上述代码中,我们首先读取了一个名为 example.jpg 的图像文件。然后,我们设置了沿 x 轴和 y 轴的平移量,并创建了一个平移矩阵。接着,我们使用 `cv2.warpAffine` 函数将平移矩阵应用于原始图像,得到平移后的图像。最后,我们使用 `cv2.imshow` 函数显示原始图像和平移后的图像。
本文实例为大家分享了opencv实现图像平移的具体代码,供大家参考,具体内容如下
图像平移指的是沿水平方向或垂直方向进行图像的移动。
平移变换公式:
对于原始图像而言,正变换矩阵:
对于目标图像而言,逆变换矩阵:
代码:
#include<opencv2/imgproc.hpp> #include<opencv2/highgui.hpp> #include<opencv2/core.hpp> #include<iostream> #include<stdlib.h> using namespace std; using namespace cv; Mat imgTranslation1(Mat& src, int xOffset, int yOffset); Mat imgTranslation2(Mat& src, int xOffset, int yOffset); int main() { Mat src = imread("C:\\Users\\H\\Desktop\\niao.bmp"); if (src.empty()) { cout << "请检查图像是否存在..." << endl; return -1; } pyrDown(src, src); cout << "原图尺寸\trows:" << src.rows << "\tcols: " << src.cols << endl; int xOffset = 50, yOffset = 80; Mat dst1 = imgTranslation1(src, xOffset, yOffset); imshow("dst1", dst1); cout << "平移不改变尺寸\trows: " << dst1.rows << "\tcols: " << dst1.cols << endl; Mat dst2 = imgTranslation2(src, xOffset, yOffset); imshow("dst2", dst2); cout << "平移改变尺寸\trows: " << dst2.rows << "\tcols: " << dst2.cols << endl; waitKey(0); system("pause"); return 0; } 图像的平移 ,大小不变 Mat imgTranslation1(Mat& src, int xOffset, int yOffset) { int nrows = src.rows; int ncols = src.cols; Mat dst(src.size(), src.type()); for (int i = 0; i < nrows; i++) { for (int j = 0; j < ncols; j++) { 映射变换 int x = j - xOffset; int y = i - yOffset; 边界判断 if (x >= 0 && y >= 0 && x < ncols && y < nrows) { dst.at<Vec3b>(i, j) = src.ptr<Vec3b>(y)[x]; } } } return dst; } //图像平移大小改变 Mat imgTranslation2(Mat& src, int xOffset, int yOffset) { int nrows = src.rows + abs(yOffset); int ncols = src.cols + abs(xOffset); Mat dst(nrows, ncols, src.type()); for (int i = 0; i < nrows; i++) { for (int j = 0; j < ncols; j++) { int x = j - xOffset; int y = i - yOffset; if (x >= 0 && y >= 0 && x < ncols && y < nrows) { dst.at<Vec3b>(i, j) = src.ptr<Vec3b>(y)[x]; } } } return dst; }
结果展示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计863个文字,预计阅读时间需要4分钟。
本文以OpenCV实现图像平移的具体代码为例,供大家参考。图像平移指的是将图像沿水平或垂直方向进行移动。平移变换公式如下:
对于原始图像中的点 (x, y),平移后的点 (x', y') 的坐标计算公式为:x'=x + dxy'=y + dy
其中,dx 和 dy 分别表示沿 x 轴和 y 轴的平移量。
以下是一个简单的图像平移代码示例:
pythonimport cv2import numpy as np
读取图像image=cv2.imread('example.jpg')
设置平移量dx=50dy=30
创建平移矩阵M=np.float32([[1, 0, dx], [0, 1, dy]])
应用平移变换translated_image=cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
显示结果cv2.imshow('Original Image', image)cv2.imshow('Translated Image', translated_image)cv2.waitKey(0)cv2.destroyAllWindows()
在上述代码中,我们首先读取了一个名为 example.jpg 的图像文件。然后,我们设置了沿 x 轴和 y 轴的平移量,并创建了一个平移矩阵。接着,我们使用 `cv2.warpAffine` 函数将平移矩阵应用于原始图像,得到平移后的图像。最后,我们使用 `cv2.imshow` 函数显示原始图像和平移后的图像。
本文实例为大家分享了opencv实现图像平移的具体代码,供大家参考,具体内容如下
图像平移指的是沿水平方向或垂直方向进行图像的移动。
平移变换公式:
对于原始图像而言,正变换矩阵:
对于目标图像而言,逆变换矩阵:
代码:
#include<opencv2/imgproc.hpp> #include<opencv2/highgui.hpp> #include<opencv2/core.hpp> #include<iostream> #include<stdlib.h> using namespace std; using namespace cv; Mat imgTranslation1(Mat& src, int xOffset, int yOffset); Mat imgTranslation2(Mat& src, int xOffset, int yOffset); int main() { Mat src = imread("C:\\Users\\H\\Desktop\\niao.bmp"); if (src.empty()) { cout << "请检查图像是否存在..." << endl; return -1; } pyrDown(src, src); cout << "原图尺寸\trows:" << src.rows << "\tcols: " << src.cols << endl; int xOffset = 50, yOffset = 80; Mat dst1 = imgTranslation1(src, xOffset, yOffset); imshow("dst1", dst1); cout << "平移不改变尺寸\trows: " << dst1.rows << "\tcols: " << dst1.cols << endl; Mat dst2 = imgTranslation2(src, xOffset, yOffset); imshow("dst2", dst2); cout << "平移改变尺寸\trows: " << dst2.rows << "\tcols: " << dst2.cols << endl; waitKey(0); system("pause"); return 0; } 图像的平移 ,大小不变 Mat imgTranslation1(Mat& src, int xOffset, int yOffset) { int nrows = src.rows; int ncols = src.cols; Mat dst(src.size(), src.type()); for (int i = 0; i < nrows; i++) { for (int j = 0; j < ncols; j++) { 映射变换 int x = j - xOffset; int y = i - yOffset; 边界判断 if (x >= 0 && y >= 0 && x < ncols && y < nrows) { dst.at<Vec3b>(i, j) = src.ptr<Vec3b>(y)[x]; } } } return dst; } //图像平移大小改变 Mat imgTranslation2(Mat& src, int xOffset, int yOffset) { int nrows = src.rows + abs(yOffset); int ncols = src.cols + abs(xOffset); Mat dst(nrows, ncols, src.type()); for (int i = 0; i < nrows; i++) { for (int j = 0; j < ncols; j++) { int x = j - xOffset; int y = i - yOffset; if (x >= 0 && y >= 0 && x < ncols && y < nrows) { dst.at<Vec3b>(i, j) = src.ptr<Vec3b>(y)[x]; } } } return dst; }
结果展示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

