Load and Display an Image - 加载并显示图像

OpenCV 4.2.0 - Modules
https://docs.opencv.org/4.2.0/index.html

OpenCV 4.2.0 - Tutorials
https://docs.opencv.org/4.2.0/d9/df8/tutorial_root.html

0. Load and Display an Image

https://docs.opencv.org/4.2.0/db/deb/tutorial_display_image.html
OpenCV modules -> OpenCV Tutorials -> Introduction to OpenCV -> Load and Display an Image

1. Goal

Load an image (using cv::imread) - 加载图像
Create a named OpenCV window (using cv::namedWindow)
Display an image in an OpenCV window (using cv::imshow) - 在 OpenCV 窗口中显示图像

2. Source Code

//============================================================================
// Name        : using namespace cv;
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>using namespace cv;
using namespace std;int main(int argc, char** argv)
{String imageName("./images/person.jpg"); // by defaultif (argc > 1){imageName = argv[1];}Mat image;image = imread(samples::findFile(imageName), IMREAD_COLOR); // Read the fileif (image.empty())                      // Check for invalid input{cout << "Could not open or find the image" << std::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 windowreturn 0;
}
21:23:09 **** Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cppBuilding target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage"  ./src/DisplayImage.o   -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage21:23:10 Build Finished (took 1s.632ms)

3. Explanation

In OpenCV 2 we have multiple modules. Each one takes care of a different area or approach towards image processing. You could already observe this in the structure of the user guide of these tutorials itself. Before you use any of them you first need to include the header files where the content of each individual module is declared.
在 OpenCV 2 中,我们有多个模块。每个模块负责图像处理的不同区域或方法。您可能已经在这些教程本身的用户指南的结构中看到了这一点。在使用其中的任何一个之前,首先需要包括声明每个单独模块的内容的头文件。

You’ll almost always end up using the:

  • core section, as here are defined the basic building blocks of the library - 如此处所定义,是库的基本构建块
  • highgui module, as this contains the functions for input and output operations - 因为它包含输入和输出操作的功能
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

We also include the iostream to facilitate console line output and input. To avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: cv. To avoid the need appending prior each of these the cv:: keyword you can import the namespace in the whole file by using the lines:
我们还包括 iostream,以方便控制台行的输出和输入。为了避免数据结构和函数名称与其他库冲突,OpenCV 有自己的命名空间:cv。为了避免在每个 cv:: 关键字之前附加前缀,您可以使用以下行将名称空间导入整个文件:

using namespace cv;
using namespace std;

This is true for the STL library too (used for console I/O). Now, let’s analyze the main function. We start up assuring that we acquire a valid image name argument from the command line. Otherwise take a picture by default: person.jpg.
STL 库也是如此 (用于控制台 I/O)。现在,让我们分析主要功能。我们开始确保从命令行获取有效的图像名称参数。否则默认情况下获取图像:person.jpg

    String imageName( "person.jpg" ); // by defaultif( argc > 1){imageName = argv[1];
}

Then create a Mat object that will store the data of the loaded image.
然后创建一个 Mat 对象,该对象将存储加载的图像的数据。

Mat image;

Now we call the cv::imread function which loads the image name specified by the first argument (argv[1]). The second argument specifies the format in what we want the image. This may be:
现在我们调用 cv::imread 函数,该函数加载第一个参数 (argv[1]) 指定的图像名称。第二个参数指定所需图像的格式。这可能是:

  • IMREAD_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
  • IMREAD_GRAYSCALE (0) loads the image as an intensity one
  • IMREAD_COLOR (>0) loads the image in the RGB format
image = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Read the file
intensity [ɪnˈtensəti]:n. 强度,强烈,亮度,紧张

OpenCV offers support for the image formats Windows bitmap (bmp), portable image formats (pbm, pgm, ppm) and Sun raster (sr, ras). With help of plugins (you need to specify to use them if you build yourself the library, nevertheless in the packages we ship present by default) you may also load image formats like JPEG (jpeg, jpg, jpe), JPEG 2000 (jp2 - codenamed in the CMake as Jasper), TIFF files (tiff, tif) and portable network graphics (png). Furthermore, OpenEXR is also a possibility.
OpenCV 支持 Windows 位图 (bmp),可移植图像格式 (pbm, pgm, ppm) 和 Sun 栅格 (sr, ras) 图像格式。借助插件 (如果您自己构建库,则需要指定使用它们,但是默认情况下在我们提供的软件包中),您还可以加载图像格式,例如 JPEG (jpeg, jpg, jpe),JPEG 2000 (jp2 - 在 CMake 中代号为 Jasper),TIFF 文件 (tiff, tif) 和便携式网络图形 (png)。此外,OpenEXR 也有可能。

After checking that the image data was loaded correctly, we want to display our image, so we create an OpenCV window using the cv::namedWindow function. These are automatically managed by OpenCV once you create them. 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. It may be:
在检查了图像数据是否正确加载之后,我们要显示图像,因此我们使用 cv::namedWindow 函数创建一个 OpenCV 窗口。创建它们后,这些文件将由 OpenCV 自动管理。为此,您需要指定其名称以及从大小的角度来看应如何处理其中包含的图像的更改。可能是:

  • WINDOW_AUTOSIZE is the only supported one if you do not use the Qt backend. In this case the window size will take up the size of the image it shows. No resize permitted!
    如果您不使用 Qt 后端,则仅支持 WINDOW_AUTOSIZE。在这种情况下,窗口大小将占据其显示图像的大小。不允许调整大小!

  • WINDOW_NORMAL on Qt you may use this to allow window resize. The image will resize itself according to the current window size. By using the | operator you also need to specify if you would like the image to keep its aspect ratio (WINDOW_KEEPRATIO) or not (WINDOW_FREERATIO).
    您可以在 Qt 上使用 WINDOW_NORMAL 来调整窗口大小。图像将根据当前窗口大小调整其大小。通过使用 | 运算符,还需要指定是否要保持图像的宽高比(WINDOW_KEEPRATIO) or not (WINDOW_FREERATIO)。

namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.

Finally, to update the content of the OpenCV window with a new image use the cv::imshow function. Specify the OpenCV window name to update and the image to use during this operation:
最后,要使用新图像更新 OpenCV 窗口的内容,请使用 cv::imshow 函数。指定要更新的 OpenCV 窗口名称以及此操作期间要使用的图像:

    imshow( "Display window", image );                // Show our image inside it.

Because we want our window to be displayed until the user presses a key (otherwise the program would end far too quickly), we use the cv::waitKey function whose only parameter is just how long should it wait for a user input (measured in milliseconds). Zero means to wait forever.
因为我们希望在用户按下某个键之前一直显示窗口 (否则程序会以太快的速度结束),所以我们使用 cv::waitKey 函数,其唯一的参数就是等待用户输入的时间 (以毫秒)。零意味着永远等待。

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

4. Result

  • Compile your code and then run the executable giving an image path as argument. If you’re on Windows the executable will of course contain an exe extension too. Of course assure the image file is near your program file.
    编译代码,然后运行给出图像路径作为参数的可执行文件。如果您使用的是 Windows,则可执行文件当然也会包含 exe 扩展名。当然,请确保图像文件位于程序文件附近。
./DisplayImage ../images/person.jpg
  • You should get a nice window as the one shown below:

Load and Display an Image - 加载并显示图像相关推荐

  1. Load 方法 暨 程序的加载顺序

    前言 众所周知,App 的入口是 main 函数,而在此之前,我们了解到的是系统会自动调用 load 方法.而且是先调用父类的,再是自己的,最后才是分类的.而为什么是这样呢,不清楚. 下面所有的 lo ...

  2. 关于ie中easyui form组件load事件无法多次加载数据

    1.废话不多说,既然找到这了就是要解决问题的: 在easyui中form load事件是这样用的的 load data 加载记录来填充表单. data 参数可以是一个字符串或者对象类型,字符串作为一个 ...

  3. jsp ajax加载html页面,Ajax中的load()方法实现指定区域加载或刷新html与jsp

    一.Jquery中的Ajax Jquery对Ajax操作进行了封装,在Jquery中: 最底层的是$.ajsx(), 第二层是load().$.get().$.post() 第三层是$.getJSON ...

  4. swift懒加载(lazy load)VS OC懒加载

    为什么80%的码农都做不了架构师?>>>    懒加载可以让你不用关心变量的创建时机,等到真正使用的时候才去创建并且能保证在使用的时候已经初始化完毕,在一定程度上可以提高性能. OC ...

  5. Qt Load and Save PCL/PLY 加载和保存点云

    Qt可以跟VTK和PCL等其他库联合使用,十分强大,下面的代码展示了如何使用Qt联合PCL库来加载和保存PCL/PLY格式的点云: 通过按钮加载点云: void QMainWindow::on_pb_ ...

  6. html5 canvas 图像预览,html5-canvas 加载并显示图像

    示例 加载图像并将其放置在画布上 var image = new Image();  // 请参阅有关创建图像的注释 image.src = "imageURL"; image.o ...

  7. 利用 JQuery的load函数动态加载页面

    利用JQuery的load函数动态加载页面 JQuery有好多Ajax函数,其中load是用来动态加载一个页面的内容到指定的dom元素上. 我们来做个例子: 做一个上下(左右)结构的页面,其中下左部分 ...

  8. Hibernate深入之get()与load()懒加载

    前面讲过get和load,一个是直接加载,并不是直接去数据库查询,如果缓存里有,首先到缓存里找,缓存分为一级缓存和二级缓存,首先到一级缓存中查找: 如果在session关闭之后进行获取类的名字之类的属 ...

  9. 跳转,location.href,window.open(),load加载页面,iframe加载页面,兼容相关

    跳转页面,兼容ios: window.location.href = ""; 跳转页面,ios无法跳转: window.open(); load加载页面: $("#&qu ...

最新文章

  1. docker load 出错 open /var/lib/docker/tmp/docker-import-837327978/bin/json: no such file or directory
  2. shop++商品搜索出现乱码的解决方法
  3. webapp检测安卓app是否安装并launch
  4. 【机器学习PAI实践二】人口普查统计
  5. kernel 3.10内核源码分析--内核栈及堆栈切换
  6. tp5 iis7 404 解决方案
  7. toAppendStream doesn‘t support consuming update changes which is produced by node GroupAggregate
  8. 《转》Ubuntu 12.04常用的快捷键
  9. 公文写作与计算机应用,德阳市事业单位《计算机应用和公文写作》真题.doc
  10. netfilter和iptables的实现机制
  11. JPA的主键生成策略
  12. 关于git的cherry-pick命令
  13. 【渝粤教育】广东开放大学 财会法规和职业道德 形成性考核 (26)
  14. AI头发笔刷_这么棒的AI插件,一定要偷偷藏好了不让总监知道……
  15. 微信小程序人脸识别功能(wx.faceDetect)、带扫脸动画、人脸图片获取(upng.js)及位置展示
  16. python compare_ssim_Python 之 计算psnr和ssim值
  17. java前后端分离(增删查改)
  18. 51单片机系列——定时/计数器
  19. 在线客服——各第三方的收费标准及服务提供
  20. Java编程思想(六)

热门文章

  1. 如何清空c盘只剩系统_使用win10不久,C盘只剩下500MB?这样操作,我清理出了30G空间!...
  2. PHP 使用FPDF 处理中文遇到的坑
  3. CARLA 笔记(02)— Ubuntu 安装 CARLA(服务端、客户端、安装 miniconda、创建虚拟环境、更换 pip 源、生成交通流、人工控制车辆按键)
  4. 易基因|病毒抗性:全基因组DNA甲基化揭示草鱼年龄相关病毒易感性的表观遗传机制
  5. Goland嗖嗖的: 快捷键,自动生成代码等效率小技巧
  6. 干货!CDN内容分发网络实战技巧
  7. AppsFlyer的测试
  8. Android开发学习之WindowManager实现弹窗
  9. 表情识别------CNN训练fer2013数据集
  10. python计算线性相关系数_Python+pandas计算数据相关系数(person、Kendall、spearman)