OpenCV && C++ 01 - Load Image from File and Display

前言

虽然之前已经在一定程度上接触、学习并应用了 OpenCV,但是依然想系统地对这些内容重新学习和整理。

Code

/*
作者:郑大峰
时间:2019年09月19日
环境:OpenCV 4.1.1 + VS2017
内容:Load Image from File and Display
*/

#include "pch.h" // 这个是预编译头文件,VS2017之前的版本为stdafx.h,详细请自行搜索
#include <iostream>
#include <opencv2/opencv.hpp> // 包含OpenCV库的头文件

using namespace std;
using namespace cv; // OpenCV中所有的类、函数和数据结构都在该命名空间下定义

int main()
{
    // 相对路径注意事项
    // 1)图片放在代码文件同级目录下,启动调试可正常读取图像。但是双击生成的可执行文件,则读取不到图像。
    // 2)复制图像至生成的可执行文件同级目录下,双击可执行文件,可正常读取图像
    string image_file = "claudia.png";

    // 如果同时添加Debug或者Release对应的依赖,这里会读取不到图像
    Mat img = imread(image_file, IMREAD_UNCHANGED);
    // If imread() function fails to load the image, the returned Mat object will be empty. 
    // If the Mat object is empty, image.empty() function will return true.
    if (img.empty())
    {
        cout << "Image is empty!";
        return -1;
    }

    namedWindow(image_file, WINDOW_AUTOSIZE); // Create a window
    imshow(image_file, img); // Show our image inside the created window.

    waitKey(0); // 一直等待按键输入。当按键被点击时,返回点击按键的ASCII码

    return 0;
}

Result

1370690-20190920153422751-2120990758.png

Explanation

Mat imread(const String& filename, int flags = IMREAD_COLOR)
This function loads an image from the specified file and return is as
a Mat object. If the function cannot read the file, it will return an
empty Mat object.

  1. filename - You have to give the relative or absolute path of an image file. If you are giving the relative path, it should be
    relative to your cpp file. jpeg, jpg, bmp, png, tiff and tif image
    file types are always supported. Other image file types are supported
    depending on your platform and installed codecs.
  2. flags - There are several possible values for the flag argument. In the above program, I did not pass any value to this
    argument such that default IMREAD_COLOR argument will be used.
  • IMREAD_UNCHANGED - The image will be loaded as it is. If you want to get the alpha channel in your input image (if it is available), you
    have to use this flag.
  • IMREAD_GRAYSCALE - The image will be load as a gray-scale image (i.e. - Single channel image, Black and white image)
  • IMREAD_COLOR - The image will be loaded as a BGR image (i.e. - 3 channel image, color image)

void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE)
This function creates a window which can be used to place images and track bars. If a window already exists with the given name, this function does nothing.

  1. winname - Name of the window. That name will display in the title bar of the newly created window. This name is also the identifier for this window and it will be used in the later OpenCV function calls to identify the window.
  2. flags - Determine the size of the window. In the above program, I did not pass any value to this argument such that default WINDOW_AUTOSIZE argument will be used.
  • WINDOW_AUTOSIZE - User cannot resize the window. Image will be displayed in its original size.
  • WINDOW_NORMAL- User can resize the window.

void imshow(const String& winname, InputArray mat)
This function shows the image in a window specified by winname. If the
window is created with WINDOW_AUTOSIZE flag, image will be displayed
in its original size. Otherwise image may be scaled to the size of the
window. If the window has not been created by calling to namedWindow()
function, this function will create a window with the WINDOW_AUTOSIZE
flag. This function call should be followed by waitKey(int)
function call in order to provide sufficient time to paint and display
the image in the window for the specified time duration in
milliseconds. If you do not call waitKey(int) function, the image will
not be displayed in the window.

  1. winname - Name of the window which created by namedWindow() function.
  2. mat - Mat object which holds the image

转载于:https://www.cnblogs.com/zdfffg/p/11551264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值