【OpenCV】加载并显示图片

来源:互联网 发布:如何建立图片数据库 编辑:程序博客网 时间:2024/06/01 07:14

翻译自OpenCV Tutorials,原地址在这里。

目标

这里将指导你学会如何:
  • 加载一张图片(使用 imread)
  • 创建一个已命名的OpenCV窗口(使用nameWindow)
  • 在OpenCV窗口中显示图片(使用imshow)

源码

你可以从这里下载源码。

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace cv;using namespace std;int main( int argc, char** argv ){    if( argc != 2)    {     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;     return -1;    }    Mat image;    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file    if(! image.data )                              // Check for invalid input    {        cout <<  "Could not open or find the image" << endl ;        return -1;    }    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.    imshow( "Display window", image );                   // Show our image inside it.    waitKey(0);                                          // Wait for a keystroke in the window    return 0;}

解释

在OpenCV 2(现在是3了)我们有多个模块,每一个都对应于不同的图像处理的领域和方法。你可能已经在在这个指南的的结构中看到这一点了。在你用它们之前,你需要先包含它们的头文件,当它们独立的模块被声明。(Before you use any of them you first need to include the header files where the content of each individual module is declared.

一般来讲,你会用到:
  • core部分,这里定义了这个库的基础内容。
  • highgui模块,这里包含了输入输出操作函数。
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: rgb(51, 51, 51); line-height: 15.6000003814697px; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: rgb(170, 204, 238); border-bottom-color: rgb(170, 204, 238); background-color: rgb(224, 245, 255);"><span class="cp" style="color: rgb(0, 112, 32);">#include <opencv2/core/core.hpp></span><span class="cp" style="color: rgb(0, 112, 32);">#include <opencv2/highgui/highgui.hpp></span><span class="cp" style="color: rgb(0, 112, 32);">#include <iostream></span>

我们也包含iostream去简化命令行输入输出。为避免和其他库的数据结构和函数名称相互矛盾, OpenCV有其自己的命名空间:cv。为避免每次添加cv::前缀,你可以对整个文件插入命名空间,方法如下:
<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: rgb(51, 51, 51); line-height: 15.6000003814697px; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: rgb(170, 204, 238); border-bottom-color: rgb(170, 204, 238); background-color: rgb(224, 245, 255);"><span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">using</span> <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">namespace</span> <span class="n">cv</span><span class="p">;</span><span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">using</span> <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">namespace</span> <span class="n">std</span><span class="p">;</span>

这对STL库同样正确(用来控制输入输出)。现在,让我们分析main函数。我们初步假设我们从命令行得到了可以使用的名称参数。

<pre style="overflow-x: auto; overflow-y: hidden; padding: 5px; color: rgb(51, 51, 51); line-height: 15.6000003814697px; border-top-width: 1px; border-bottom-width: 1px; border-style: solid none; border-top-color: rgb(170, 204, 238); border-bottom-color: rgb(170, 204, 238); background-color: rgb(224, 245, 255);">   <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">if</span><span class="p">(</span> <span class="n">argc</span> <span class="o" style="color: rgb(102, 102, 102);">!=</span> <span class="mi" style="color: rgb(32, 128, 80);">2</span><span class="p">)</span>    <span class="p">{</span>     <span class="n">cout</span> <span class="o" style="color: rgb(102, 102, 102);"><<</span><span class="s" style="color: rgb(64, 112, 160);">" Usage: display_image ImageToLoadAndDisplay"</span> <span class="o" style="color: rgb(102, 102, 102);"><<</span> <span class="n">endl</span><span class="p">;</span>     <span class="k" style="color: rgb(0, 112, 32); font-weight: bold;">return</span> <span class="o" style="color: rgb(102, 102, 102);">-</span><span class="mi" style="color: rgb(32, 128, 80);">1</span><span class="p">;</span>    <span class="p">}</span>

然后创建一个Mat对象去存储被加载图片的数据。
 Mat image;
现在,我们可以调用imread函数去加载名称被第一个参数(argv[1])限定的图片。第二个参数限定了我们想要的图片格式。
它可以是:
  • CV_LOAD_IMAGE_UNCHANGED(<0)按照图片本身的样子加载图片(如果alpha通道存在的话,包括alpha通道)
  • CV_LAOD_IMAGE_GRAYSCALE(0)仅加载图片的一个通道(loads the image as an intensity one,加载灰度图)
  • CV_LOAD_IMAGE_COLOR (>0) 使用RGB格式加载图片
如果第二个参数没有被显示给出,默认被设为CV_LOAD_IMAGE_COLOR

注: OpenCV提供支持的图片格式有Windows bitmap (bmp), portable image formats (pbm, pgm, ppm)和Sun raster (sr, ras). 在插件的帮助下 (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) 你也可以加载以下格式图片,如:JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - codenamed in the CMake as Jasper), TIFF files (tiff, tif)以及portable network graphics (png). 除此之外, OpenEXR也是支持的.

检查图像数据被正确加载之后,我们希望去展示我们的图片,所以我们使用nameWindow函数创建一个OpenCV窗口。这些将被OpenCV自动管理只要你创建他们。你需要给指定他们的名字和他们如何处理图像的尺寸变化(For this you need to specify its name and how it should handle the change of the image it contains from a size point of view.)它可以是:

  • WINDOW_AUTOSIZE是除Qt平台之外唯一被支持的参数。它将按照图片的尺寸显示。并不允许调节尺寸。
  • WINDOW_NORMAL允许你在Qt平台调节尺寸。图片将依据窗口尺寸自动调节。如果你想保持图片比例,你可以使用 | 操作符(WINDOW_KEEPRATIO) 或 自由比例(WINDOW_FREERATIO)。
    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
如果第二个参数不被指定,将是WINDOW_AUTOSIZE,这也意味着不改变图片尺寸。

最后,我们用imshow函数在OpenCV窗口中显示图片。使用如下操作指定要被哪个窗口显示哪张图片:
imshow( "Display window", image );                   // Show our image inside it.
因为我们想让我们的窗口一直显示指导我们按下一个按键,(否则程序将很快结束otherwise the program would end far too quickly,毫秒级的?),我们使用 waitKey函数,它的唯一参数就是等用户输入多久时间(毫秒级)。零意味着永远(Zero means to wait forever.)。

    waitKey(0);                                          // Wait for a keystroke in the window

结果

  • 编译你代码给出一个图片路径作为参数并执行。如果你是在Windows执行将有一个exe拓展名。当然假设你的图片文件也在你的项目文件。

./DisplayImage HappyFish.jpg
  • 你将得到一个很棒的窗口,正如下面展示的。

0 0