CPP和Opencv联合编程基础第一章第一节加载图像
Posted Soft~何忠耀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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";
三.赶紧操练起来吧!
以上是关于CPP和Opencv联合编程基础第一章第一节加载图像的主要内容,如果未能解决你的问题,请参考以下文章