OPENCV学习笔记2_Mat 加载, 显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OPENCV学习笔记2_Mat 加载, 显示相关的知识,希望对你有一定的参考价值。

1.1 Introduction to related functions

   1.Imread() function

     Mat imread( const String& filename, int flags = IMREAD_COLOR );

  • filename:Name of file to be loaded
  • flags:载入标识,指定一个加载图像的颜色类型(参考enum ImreadModes)。自带缺省值1,如果在调用时忽略这个参数,就表示载入三通道的彩色图像。
enum ImreadModes {
       IMREAD_UNCHANGED     = -1, 
// !< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
// 不进行转化,比如保存为了16位的图片,读取出来仍然为16位
       IMREAD_GRAYSCALE     = 0,  
// !< If set, always convert image to the single channel gray scale image.
// 进行转化为灰度图,比如保存为了16位的图片,读取出来为8位,类型为CV_8UC1
       IMREAD_COLOR         = 1,  
// !< If set, always convert image to the 3 channel BGR color image.
// 进行转化为三通道图像
       IMREAD_ANYDEPTH      = 2,  
// 如果图像深度为16位则读出为16位,32位则读出为32位,其余的转化为8位。
       IMREAD_ANYCOLOR      = 4,  
// !< If set, the image is read in any possible color format.
/*
..............
*/
     };
/*
Mat image=imread("test.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);//载入最真实的图像
*/

2.  namedWindow() function

void namedWindow(const String& winname,int flags = WINDOW_AUTOSIZE);

·  flags,窗口的标识,可以填如下的值:

CV_WINDOW_AUTOSIZE, cannot change the window size manually(自适应窗口大小)

CV_WINDOW_NORMAL,   enables you to resize the window(没有限制)

WINDOW_OPENGL,     如果设置了这个值的话,窗口创建的时候便会支持OpenGL

namedWindow函数的作用是,通过指定的名字,创建一个可以作为图像和进度条的容器窗口。如果具有相同名称的窗口已经存在,则函数不做任何事情。

3.  imshow() function

void imshow(const String& winname, InputArray mat);

·  winname,填需要显示的窗口标识名称

·  matInputArray 类型,填需要显示的图像; typedef const _InputArray& InputArray;可查看_InputArray定义,一般遇到InputArray类型当作Mat类型即可

imshow函数用于在指定的窗口中显示图像。如果窗口是用CV_WINDOW_AUTOSIZE(默认值)标志创建的,那么显示图像原始大小。否则,将图像进行缩放以适合窗口(取决图像深度)。

·  如果载入的图像是8位无符号类型(8-bit unsigned),就显示图像本来的样子。

·  如果图像是16位无符号类型(16-bit unsigned)或32位整型(32-bit integer),便用像素值除以256。也就是说,值的范围是[0,255 x 256]映射到[0,255]

·  如果图像是32位浮点型(32-bit floating-point),像素值便要乘以255。也就是说,该值的范围是[0,1]映射到[0,255]

1.2 Mat Loading, displaying example

/*
    The opencv_core module that contains the core functionalities of the library, in particular, the basic data structures and arithmetic functions,包含了程序库的核心功能,特别是基本的数据结构和算法函数;
    The opencv_highgui module that contains the image and video reading and writing functions, along with other user interface functions,包含图像、视频读写函数和部分用户界面函数;
    The opencv_imgproc module that contains the main image processing functions
*/

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include<iostream>
using namespace std;

using namespace cv;

int main( int argc, char** argv ) {

    //Mat image;                         // creates an image of size 0 by 0
    Mat image = cv::imread("test.jpg");  // read an image from file, decode it, and allocate the memory

    if (!image.data)                     // Check for invalid input
    //if(image.empty())
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }
    cout << "size: " << image.size().height << " , " << image.size().width << endl;

    namedWindow("My Image", WINDOW_NORMAL);            // Create a window for display

    /*
    Several image transformations in OpenCV can be performed in-place,
    However,we can always create another matrix to hold the output result and that is what we will do:
    */
    Mat result;
    flip(image, result, 1);          // positive for horizontal,0 for vertical,negative for both

    namedWindow("Output Image");     // the result is displayed on another window
                                     // 如不介意窗口大小可变,可直接注释掉上一句,因imshow可以直接创建窗口
    imshow("Output Image", result);  // Show our image inside it

    imshow("My Image", image);       // specify the image to be shown on this special window

    /*
    Since it is a console window that will terminate at the end of the main function, we add an
    extra highgui method to wait for a user key before ending the program:
    */

    waitKey();                            // Wait for a keystroke in the window
    //waitKey(5000);
    //cv::imwrite("output.bmp", result);  // save the processed image on  disk
    return 1;
}

技术分享

 


以上是关于OPENCV学习笔记2_Mat 加载, 显示的主要内容,如果未能解决你的问题,请参考以下文章

OPENCV学习笔记2-8_Mat_类

opencv学习笔记opencv加载图像修改图像显示图像保存图像以及代码举例

OPENCV学习笔记1_Mat 创建

OPENCV学习笔记3_Mat 保存

[OpenCV学习笔记2][Mat数据类型和操作]

OPENCV学习笔记1-8_选取图像局部区域