【OpenCV_03】图像载入,显示和输出 Read & Display Image

图像的载入,显示和输出


这篇博客我回谈一谈,如何通过采用OpenCV的库函数将图像载入,显示以及输出。首先,打开C++的IDE编程环境新建一个工程,(必须把OpenCV的环境设置好在IDE下)


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

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
     Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); 
  //read the image data in the file "MyPic.JPG" and store it in 'img'

     if (img.empty()) //check whether the image is loaded or not
     {
          cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
          return -1;
     }

     namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); 
      //create a window with the name "MyWindow"
     imshow("MyWindow", img); 
     //display the image which is stored in the 'img' in the "MyWindow" window

     waitKey(0); //wait infinite time for a keypress

     destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"

     return 0;
}
//



在运行程序之前,在C++源文件文件夹里放一个叫  (MyPic.JPG) 文件名的图片,否则就需要设置函数imread()的第一个参数为图片的绝对路径。 

代码解释
  • #include "stdafx.h"
因为是在 Visual Studio下开发,所以需要加上。
  • #include "opencv2/highgui/highgui.hpp"
imread()namedWindow()imshow() and  waitKey() 的定义需要包含以上头文件。 上面定义的 Mat类的对象, 在头文件 "opencv2/core/core.hpp"已经声明。 It is declared in "opencv2/core/core.hpp" header file. 但是代码中为什么没有?因为在
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值