核心代码:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char ** argv)
{
Mat src = imread("F:/OpenCV/material/lena.jpg",IMREAD_ANYCOLOR);
if (src.empty()) { printf("图像加载失败!"); return -1; }
namedWindow("input", WINDOW_FREERATIO);//自适应图像大小,窗口可以修改,当屏蔽时图像过大会显示不完整
imshow("input", src);
waitKey(0);
destroyAllWindows();
return 0;
}
1、读取图像函数imread()
CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );
@功能:从文件加载图像
函数imread从指定的文件加载一个图像并返回它。 如果图像不能
读取(由于缺少文件、权限不当、不支持或无效的格式)的函数
返回一个空矩阵(Mat::data==NULL)。 支持的文件格式有限制。
@ filename:要加载的文件名。
@ flags :ImreadModes值的标志
IMREAD_UNCHANGED–原图
IMREAD_GRAYSCALE–灰度图像
IMREAD_ANYCOLOR–图像以任何可能的颜色格式读取
返回值为Mat矩阵,src.empty()为真时代表加载失败
//! Imread flags
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};
2、创建窗口函数namedWindow()
CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
@功能:创建一个窗口
函数名为namedWindow,它创建了一个窗口,可以用作图像的占位符
trackbars。 创建的窗口通过它们的名称来引用。如果已经存在同名的窗口,则该函数不执行任何操作。
@winname:winname窗口标题中的窗口名称,该窗口可以用作窗口标识符。
@flags :参考WindowFlags。WINDOW_FREERATIO 窗口大小可以修改,自动缩放适应,避免大图显示不全
//! Flags for cv::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
};
3、显示图像imshow()
CV_EXPORTS_W void imshow(const String& winname, InputArray mat);
@功能:显示指定窗口中的图像。
@imshow功能用于在指定窗口中显示图像。 如果窗口是用 windows::WINDOW_AUTOSIZE标志,图像显示其原始大小,但它仍然受屏幕分辨率的限制。 否则,图像将被缩放以适应窗口。 该函数可以根据深度对图像进行缩放。如果在此函数之前没有创建窗口,则假设使用cv::WINDOW_AUTOSIZE创建窗口。如果你需要显示一个比屏幕分辨率大的图像,你需要在imshow之前调用namedWindow(“”, WINDOW_NORMAL)。 这个函数后面应该跟着cv::waitKey函数,用于显示指定的图像
毫秒。 否则,它将不会显示图像。 例如,**waitKey(0)**将显示窗口无限到任意按键(适合图像显示)。 **waitKey(25)**将显示一个帧
25毫秒后,显示将自动关闭。 (如果你把它放在循环中读取视频,它会逐帧显示视频)
按Ctrl+C将复制图像到剪贴板。
按Ctrl+S将显示一个对话框来保存图像。
@param winname窗口名。
@param mat将要显示的图像。
4、等待按键输入waitKey()
CV_EXPORTS_W int waitKeyEx(int delay = 0);
@功能:等待按下的键。
函数waitKey无限地等待一个键事件或等待延迟毫秒,当它是正的。 由于操作系统有最小的线程切换时间,因此
函数不会精确地等待ms,它至少会等待ms,这取决于其他是什么运行在你的电脑上。 它返回按下的键的代码,如果没有键,则返回-1
在指定的时间过去之前按下。
@delay:毫秒
5、销毁所有窗口destroyAllWindows()
CV_EXPORTS_W void destroyAllWindows();
@功能:破坏所有的HighGUI窗口。
销毁指定窗口函数:CV_EXPORTS_W void destroyWindow(const String& winname);
废话:
嘿嘿,开启opencv的第一天,滴答,打卡成功!!!!!!!!!!!!!!!!

388

被折叠的 条评论
为什么被折叠?



