OpenCV读取和显示图片

时间:2022-06-10
本文章向大家介绍OpenCV读取和显示图片,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

代码示例:

#include "stdafx.h"
#include <include_opencv320_gpu.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main( )
{
    Mat srcImage;
    Mat tortImage;
    const char *srcImageName = "D:\image_retrieval\sln\SiftPractice\PicLib\1.jpg";
    const char *tortImageName = "D:\image_retrieval\sln\SiftPractice\PicLib\2.jpg";
    srcImage = imread( srcImageName );
    tortImage = imread( tortImageName );

    namedWindow( "原图窗口" , CV_WINDOW_NORMAL );//CV_WINDOW_NORMAL参数表示用户可以改变窗口大小
    imshow( "原图窗口" , srcImage );    
    namedWindow( "侵权图窗口" , CV_WINDOW_NORMAL );
    imshow( "侵权图窗口" , tortImage );
    waitKey( 0 );

    getchar( );
    return 0;
}

运行效果:

运行效果

代码讲解:

头文件:

#include "stdafx.h" 不多说,直接看下stdafx.h里面的内容:

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

// TODO:  在此处引用程序需要的其他头文件

接下来还要三个头文件:

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

在三个是我在开发OpenCV程序时常用包含库,其中include_opencv320_gpu.h是自己配的,可以在百度网盘https://pan.baidu.com/s/1ceviu6下载。

命名空间:

using namespace std; 这个C++开发的同学应该很熟悉了吧,标准命名空间,包含C++标准程序库中的所有标识符。

using namespace cv; 这个是OpenCV实现的命名空间,包含Mat等标识符。 如果不写using namespace cv;,那在使用Mat类时就要这样声明:cv::Mat

主程序:

Mat srcImage;
Mat tortImage;

以上两行代码声明了srcImagetortImage两个类,分别用来存储原始图片和侵权图片。

const char *srcImageName = "D:\image_retrieval\sln\SiftPractice\PicLib\1.jpg";
const char *tortImageName = "D:\image_retrieval\sln\SiftPractice\PicLib\2.jpg";

以上两行代码声明了两个字符串指针变量:srcImageNamesrcImageName,指向两张图片所在的文件全路径。 注意: windows直接复制下来的文件全路径是:D:image_retrievalslnSiftPracticePicLib1.jpg 在编程使用时要使用两个反斜杠:D:\image_retrieval\sln\SiftPractice\PicLib\1.jpg

srcImage = imread( srcImageName );
tortImage = imread( tortImageName );

以上两行代码实现了将图片读取到类中。 注意,imread()函数的原型是imread( const String& filename, int flags = IMREAD_COLOR )flags默认值为1,表示将图像转换到彩色再返回。 具体参数讲解详见:http://blog.csdn.net/poem_qianmo/article/details/20537737

namedWindow( "原图窗口" , CV_WINDOW_NORMAL )

上面这行代码定义了一个窗口,第一个参数"原图窗口"指定了该窗口的名称,同时也是该窗口的标识符。第二个参数代表该窗口可以被用户改变大小。 nameWindow()原型为: void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE)

  • WINDOW_NORMAL设置了这个值,用户便可以改变窗口的大小(没有限制)
  • WINDOW_AUTOSIZE如果设置了这个值,窗口大小会自动调整以适应所显示的图像,并且不能手动改变窗口大小。
  • WINDOW_OPENGL 如果设置了这个值的话,窗口创建的时候便会支持OpenGL。
imshow( "原图窗口" , srcImage );    

上面这行代码将在使"原图窗口"这个窗口加载srcImage这个类,并显示该类中的图片。 注意: 如果不在后面写waitKey( 0 );你将会看到窗口显示全灰,没有正常显示图片。

waitKey( 0 );

上面这行代码不能忽视,waitKay()原型为int waitKey(int delay = 0),其中delay的单位是毫秒,表示显示图片的时间,如果delay=0表示一直显示图片。 官方对waitKey()的说明:

/** @brief Waits for a pressed key.

The function waitKey waits for a key event infinitely (when f$texttt{delay}leq 0f$ ) or for delay
milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the
function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is
running on your computer at that time. It returns the code of the pressed key or -1 if no key was
pressed before the specified time had elapsed.

@note

This function is the only method in HighGUI that can fetch and handle events, so it needs to be
called periodically for normal event processing unless HighGUI is used within an environment that
takes care of event processing.

@note

The function only works if there is at least one HighGUI window created and the window is active.
If there are several HighGUI windows, any of them can be active.

@param delay Delay in milliseconds. 0 is the special value that means "forever".
 */

最后

getchar( );

上面这行代码用于等待用户输入字符,当程序调用getchar()时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar()才开始从stdio流中每次读入一个字符

好,关于OpenCV读取和显示图片就先写这么多,文中如有不当之处欢迎留言。 我们下次再见。