请给出C++使用OpenCV横向拼接图片的代码
时间: 2024-05-01 19:20:46 浏览: 109
以下是使用OpenCV进行横向拼接图片的C代码示例:
```c
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
Mat img1 = imread("image1.jpg");
Mat img2 = imread("image2.jpg");
if (img1.empty() || img2.empty()) {
cout << "Could not open or find the image!" << endl;
return -1;
}
Mat result;
hconcat(img1, img2, result);
imshow("Result", result);
waitKey(0);
return 0;
}
```
在此示例中,我们首先使用 `imread()` 函数加载两个图像 `img1` 和 `img2`。然后,我们检查这些图像是否已成功加载,如果其中任一图像未成功加载,则显示错误消息并退出程序。
接下来,我们使用 OpenCV 的 `hconcat()` 函数将这两个图像横向拼接成一个新的图像 `result`。最后,我们使用 `imshow()` 函数显示结果图像,并使用 `waitKey()` 函数等待用户按下任意键以关闭窗口。
注意,此示例假定图像文件 `image1.jpg` 和 `image2.jpg` 位于程序运行的当前工作目录中。如果这些图像文件位于其他位置,请相应地更改文件路径。
阅读全文
相关推荐















