CPP和Opencv联合编程基础第一章第一节加载图像
一.伪代码
加载一张图片-> 准备一张图片,给出路径,如"D:\Microsoft.jpg"。
再C++中加载图片,要给图像准备一个对象。opencv中-> Mat 就是干这个事情。
加载图片之前做下确认,图片是否真实存在。
图片加载了,要放到窗口里面-> namedWindow
点击查看代码
enum WindowFlags {
//选用该模式可以修改窗口大小
WINDOW_NORMAL = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
//窗口大小由图片决定
WINDOW_AUTOSIZE = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.
WINDOW_OPENGL = 0x00001000, //!< window with opengl support.
WINDOW_FULLSCREEN = 1, //!< change the window to fullscreen.
WINDOW_FREERATIO = 0x00000100, //!< the image expends as much as it can (no ratio constraint).
WINDOW_KEEPRATIO = 0x00000000, //!< the ratio of the image is respected.
WINDOW_GUI_EXPANDED=0x00000000, //!< status bar and tool bar
WINDOW_GUI_NORMAL = 0x00000010, //!< old fashious way
};
选WINDOW_NORMAL加载模式,调整窗口大小->resizeWindow
显示图片->imshow
二.完整代码
新建一个控制台程序。
点击查看代码
#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat image;
image = imread("D:\\Microsoft.jpg");
//判断图片是否存在
if (image.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
//创建串口来显示图片
namedWindow("Display window", WINDOW_NORMAL);
//调整窗口大小
resizeWindow("Display window", cv::Size(1280, 960));
//显示图片
imshow("Display window", image);
//等待按键按下
waitKey(0);
std::cout << "Hello World!\n";
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下