c++ opencv编写打开图片程序
时间: 2024-02-27 18:58:46 浏览: 67
下面是一个使用OpenCV库打开图片的示例程序:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// 检查命令行参数
if (argc != 2)
{
cout << "Usage: " << argv[0] << " <Image_Path>" << endl;
return -1;
}
// 加载图片
Mat image = imread(argv[1], IMREAD_COLOR);
// 检查是否成功加载
if (image.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
// 创建窗口并显示图片
namedWindow("Display window", WINDOW_NORMAL);
imshow("Display window", image);
// 等待用户按下任意按键
waitKey(0);
return 0;
}
```
在命令行中输入以下命令编译并运行程序:
```
g++ main.cpp -o main `pkg-config --cflags --libs opencv4`
./main path_to_image
```
其中 `path_to_image` 是要打开的图片文件路径。
阅读全文
相关推荐


















