随笔 - 46  文章 - 39  trackbacks - 0
<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(2)

随笔分类

随笔档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜

目标
学习:1、使用imread载入图像。
         2、使用namedWindow创建命名OpenCV窗口。
         3、使用imshow在OpenCV窗口中显示图像。

源码
 1 #include <opencv2/core/core.hpp>
 2 #include <opencv2/highgui/highgui.hpp>
 3 #include <iostream>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 
 8 int main(int argc, char ** argv)
 9 {
10     if (2 != argc)
11     {
12         cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
13         return -1;
14     }
15 
16     Mat image;
17     image = imread(argv[1], CV_LOAD_IMAGE_COLOR);          // Read the file
18 
19     if (!image.data)                                                                 // Check for invalid input
20     {
21         cout << "Could not open or find the image" << std::endl;
22         return -1;
23     }
24 
25     namedWindow("Display window", WINDOW_AUTOSIZE);      // Create a window for display
26     imshow("Display window", image);                                     // Show our image inside it.
27 
28     waitKey(0);                                                                       // wait for a keystroke in the window
29     return 0;
30 }

说明
在使用OpenCV 2 的功能之前,几乎总是要包含
1、core 部分,定义库的基本构建块
2、highgui模块,包含输入输出操作函数。

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

还需要include<iostream>这样更容易在console上输出输入。为了避免数据结构和函数名称与其他库冲突,OpenCV有自己的命名空间cv。当然为了避免在每个关键字前都加cv::keyword,可以在头部导入该命名空间。
using namespace cv;
using namespace std;

需要在命令行输入有效的图像名称。
if (2 != argc)
{
      cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
      return -1;
}
然后创建Mat对象用于存储载入的图像数据。
Mat image;

调用imread函数载入图像(图像名称为argv[1]指定的)。第二个参数指定图像格式。
1、CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is(including the alpha channel if present)
2、CV_LOAD_IMAGE_GRAYSCALE (0) loads the image as an intensity one
3、CV_LOAD_IMAGE_COLOR (>0) loads the image in the BGR format

image = imread(argv[1], CV_LOAD_IMAGE_COLOR);      // Read the file
如果第二个参数未指定,那么默认为CV_LOAD_IMAGE_COLOR

为了检查图像是否正常载入,我们用namedWindow函数创建一个OpenCV窗口来显示图像。需要指定窗口名称和大小。
第二个参数默认为:WINDOW_AUTOSIZE
1、WINDOW_AUTOSIZE    只支持QT平台。
2、WINDOW_NORMAL       QT上支持窗口调整大小。

最后在创建的窗口中显示图像
imshow("Display window", image);   
                             
     

结果
编译执行程序。
./DisplayImage d:\apple.jpg
posted on 2016-07-11 07:58 canaan 阅读(922) 评论(0)  编辑 收藏 引用 所属分类: OPenCV学习

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理