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. };
所以默认载入三通道的彩色图像,其他参数用途在注释中也已明确。
flags > 0 : 返回一个3通道的彩色图像
flags = 0 : 返回灰度图像
flags < 0 : 返回包含Alpha通道的加载图像
Mat image0=imread("1.jpg", 2 | 4); 载入无损的源图像 Mat image1=imread("1.jpg", 0); 载入灰度图 Mat image2=imread("1.jpg", 199); 载入3通道的彩色图像
函数原型:void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);
第一个参数,填写被用作窗口的标识符的窗口名称
第二个参数,窗口的标识,其enum定义如下:
1 2 3 4 5 6 7 8 9 10 11
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 };
第二个参数:指定窗口里每次鼠标事件发生的时候,被调用的函数指针。 这个函数的原型大概是void Foo(int event, int x, int y, int flags, void *param);其中event变量enum值的定义见后文,x和y是鼠标指针在图像坐标系中的坐标值,flag是的enum值的定义见后文。param是用户定义的传递到setMouseCallback函数调用的参数。
第三个参数:用户定义的传递到回调函数的参数,默认值为0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//! Mouse Events see cv::MouseCallback enum MouseEventTypes { EVENT_MOUSEMOVE = 0, //!< indicates that the mouse pointer has moved over the window. EVENT_LBUTTONDOWN = 1, //!< indicates that the left mouse button is pressed. EVENT_RBUTTONDOWN = 2, //!< indicates that the right mouse button is pressed. EVENT_MBUTTONDOWN = 3, //!< indicates that the middle mouse button is pressed. EVENT_LBUTTONUP = 4, //!< indicates that left mouse button is released. EVENT_RBUTTONUP = 5, //!< indicates that right mouse button is released. EVENT_MBUTTONUP = 6, //!< indicates that middle mouse button is released. EVENT_LBUTTONDBLCLK = 7, //!< indicates that left mouse button is double clicked. EVENT_RBUTTONDBLCLK = 8, //!< indicates that right mouse button is double clicked. EVENT_MBUTTONDBLCLK = 9, //!< indicates that middle mouse button is double clicked. EVENT_MOUSEWHEEL = 10,//!< positive and negative values mean forward and backward scrolling, respectively. EVENT_MOUSEHWHEEL = 11//!< positive and negative values mean right and left scrolling, respectively. };
1 2 3 4 5 6 7 8 9
//! Mouse Event Flags see cv::MouseCallback enum MouseEventFlags { EVENT_FLAG_LBUTTON = 1, //!< indicates that the left mouse button is down. EVENT_FLAG_RBUTTON = 2, //!< indicates that the right mouse button is down. EVENT_FLAG_MBUTTON = 4, //!< indicates that the middle mouse button is down. EVENT_FLAG_CTRLKEY = 8, //!< indicates that CTRL Key is pressed. EVENT_FLAG_SHIFTKEY = 16,//!< indicates that SHIFT Key is pressed. EVENT_FLAG_ALTKEY = 32//!< indicates that ALT Key is pressed. };
//-----------------------------------【全局函数声明部分】------------------------------------ // 描述:全局函数的声明 //------------------------------------------------------------------------------------------------ voidon_MouseHandle(int event, int x, int y, int flags, void* param); voidDrawRectangle(cv::Mat& img, cv::Rect box); voidShowHelpText();
//【3】程序主循环,当进行绘制的标识符为真时,进行绘制 while (1) { srcImage.copyTo(tempImage);//拷贝源图到临时变量 if (g_bDrawingBox) DrawRectangle(tempImage, g_rectangle);//当进行绘制的标识符为真,则进行绘制 imshow(WINDOW_NAME, tempImage); if (waitKey(10) == 27) break;//按下ESC键,程序退出 } return0; }
//--------------------------------【on_MouseHandle( )函数】----------------------------- // 描述:鼠标回调函数,根据不同的鼠标事件进行不同的操作 //----------------------------------------------------------------------------------------------- voidon_MouseHandle(int event, int x, int y, int flags, void* param) {
Mat& image = *(cv::Mat*) param; switch (event) { //鼠标移动消息 case EVENT_MOUSEMOVE: { if (g_bDrawingBox)//如果是否进行绘制的标识符为真,则记录下长和宽到RECT型变量中 { g_rectangle.width = x - g_rectangle.x; g_rectangle.height = y - g_rectangle.y; } } break;
//左键按下消息 case EVENT_LBUTTONDOWN: { g_bDrawingBox = true; g_rectangle = Rect(x, y, 0, 0);//记录起始点 } break;
//左键抬起消息 case EVENT_LBUTTONUP: { g_bDrawingBox = false;//置标识符为false //对宽和高小于0的处理 if (g_rectangle.width < 0) { g_rectangle.x += g_rectangle.width; g_rectangle.width *= -1; }