视频学习链接为:https://www.bilibili.com/video/BV1i54y1m7tw?p=2.
知识点:
读取图像 – imread,im表示image
显示图像 – imshow
OpenCV的API手册链接: http://www.opencv.org.cn/opencvdoc/2.3.2/html/modules/highgui/doc/user_interface.html?highlight=namedwindow#cv.NamedWindow.
1、当你发现如果要显示的是一张.png格式的图片的话(或者图像很大时),显示不全,又应该怎么办?
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char** argv)
{
Mat picture = imread("test.jpeg"); //图片test.jpeg必须和test.cpp文件放在一个文件夹下!!!
//从"class cv:: Mat"可以看出Mat是定义在cv这个类中的一个public的静态成员,
//此外,对于静态成员的使用,类内必须声明,类外必须初始化,用“::”(作用域操作符)来指明所属的类。
//通过测试,我们会发现如果要显示的是一张.png格式的图片的话(图像很大时),可能显示不全。
//那应该怎么办呢?
//这个时候,我需要另外一个函数 -- namedWindow(含义是创建一个窗口)
// void namedWindow(const string& winname, int flags)
//该函数两个参数,第一个是给这个窗口起一个名称,
//第二个是Flags of the window. Currently the only supported(默认支持) flag is WINDOW_AUTOSIZE .
//If this is set, the window size is automatically adjusted to fit the displayed image ,
//and you cannot change the window size manually.
//我们要将参数改为 WINDOW_FREERATIO ,这样就可以手动调整了。
namedWindow("输入窗口",WINDOW_FREERATIO);
//使用Mat(matrix -- 矩阵)这种类型来读取通过指定路径的图像信息并存放到变量picture中。
//图像从本质上来说都是二维的数组(矩阵)
imshow("输入窗口", picture);//需要注意第一个参数表示显示在哪个窗口上
//imshow有两个参数,第一个参数是窗口的名称,第二个是Mat定义的变量。
//当我们不通过namedWindow创建窗口时,程序会采用默认窗口,且这个窗口的参数是WINDOW_AUTOSIZE
waitKey(0);
//waitKey可以理解为延时函数,单位为ms
//当程序执行到这条语句时会被阻塞,
//0表示一直堵塞,1000表示延时1000ms
destroyAllWindows();
//将所有显示窗口全部销毁
return 0;
}
2、当你想要显示一张灰度图像应该怎么办呢?
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char** argv)
{
//使用Mat(matrix -- 矩阵)这种类型来读取通过指定路径的图像信息并存放到变量picture中。
//图像从本质上来说都是二维的数组(矩阵)
Mat picture = imread("test.jpeg",IMREAD_GRAYSCALE);
// C++: Mat imread(const string& filename, int flags=1 )
/* 参数为枚举类型:
IMREAD_COLOR = 1 -- 默认
IMREAD_GRAYSCALE = 0 -- 灰度图像
*/
/*
filename – Name of file to be loaded.
Flags specifying the color type of a loaded image:
>0 Return a 3-channel color image
=0 Return a grayscale image
flag默认值为1(当该参数不设置的时候),默认为彩色
*/
//我们要将参数改为 WINDOW_FREERATIO ,这样就可以手动调整了。
namedWindow("输入窗口",WINDOW_FREERATIO);
imshow("输入窗口", picture);//需要注意第一个参数表示显示在哪个窗口上
//imshow有两个参数,第一个参数是窗口的名称,第二个是Mat定义的变量。
//当我们不通过namedWindow创建窗口时,程序会采用默认窗口,且这个窗口的参数是WINDOW_AUTOSIZE
waitKey(0);
//waitKey可以理解为延时函数,单位为ms
//当程序执行到这条语句时会被阻塞,
//0表示一直堵塞,1000表示延时1000ms
destroyAllWindows();
//将所有显示窗口全部销毁
return 0;
}
3、当要显示一张特殊颜色格式的图像怎么办?
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char** argv)
{
//使用Mat(matrix -- 矩阵)这种类型来读取通过指定路径的图像信息并存放到变量picture中。
//图像从本质上来说都是二维的数组(矩阵)
Mat picture = imread("test.jpeg", IMREAD_COLOR);
//1、在当前目录找不到该图像文件
//2、如果我通过修改后缀名得到的图片文件,也会报错
if (picture.empty())
{
cout << "could not Load image." << endl;
system("pause");
return -1;
}
// C++: Mat imread(const string& filename, int flags=1 )
/* 参数为枚举类型:
IMREAD_COLOR = 1 -- 默认
IMREAD_GRAYSCALE = 0 -- 灰度图像
IMREAD_UNCHANGED = -1 -- 图片有透明通道时使用
IMREAD_ANYCOLOR = 4 -- 图片是浮点数据或者其他特殊的数据,比如其他颜色格式
IMREAD_ANYDEPTH = 2 -- 任意深度的图片,比如是8、16、32位像素深度
*/
/*
filename – Name of file to be loaded.
Flags specifying the color type of a loaded image:
>0 Return a 3-channel color image
=0 Return a grayscale image
<0 Return the loaded image as is.(同样加载,适合于有透明通道的图片)
flag默认值为1(当该参数不设置的时候),默认为彩色,也就是 IMREAD_COLOR
*/
//我们要将参数改为 WINDOW_FREERATIO ,这样就可以手动调整了。
namedWindow("输入窗口",WINDOW_FREERATIO);
imshow("输入窗口", picture);//需要注意第一个参数表示显示在哪个窗口上
//imshow有两个参数,第一个参数是窗口的名称,第二个是Mat定义的变量。
//当我们不通过namedWindow创建窗口时,程序会采用默认窗口,且这个窗口的参数是WINDOW_AUTOSIZE
waitKey(0);
//waitKey可以理解为延时函数,单位为ms
//当程序执行到这条语句时会被阻塞,
//0表示一直堵塞,1000表示延时1000ms
destroyAllWindows();
//将所有显示窗口全部销毁
system("pause");
return 0;
}
4、关于imshow函数还需要指出一点:
If the image is 8-bit unsigned, it is displayed as is.
如果图像为8位无符号,则显示为原样。
If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255256] is mapped to [0,255].
如果图像是16位无符号或32位整数,则像素被256除。即将[0,255256]的取值范围映射为[0,255]。
If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
如果图像是32位浮点数,则像素值乘以255。即将取值范围[0,1]映射为[0,255]。
也就是说通过imshow只能显示用8位表示一个像素分量的图像。
比如说,一幅彩色图像的每个像素用R,G,B三个分量表示,那么如果想通过imshow函数正常显示出来,那这三个分量就必须为8位无符号表示。