zoukankan      html  css  js  c++  java
  • OpenCV && C++ 01

    前言

    虽然之前已经在一定程度上接触、学习并应用了 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

    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
  • 相关阅读:
    Anaconda和Miniconda的安装
    并联谐振电路频率阻抗及计算曲线带宽案例摘要
    TEM波
    Android Studio实现简单考试应用程序
    android studio实现简单考试应用程序
    Android Studio的简易计算器界面设计
    时谐变换
    简要理解什么是趋肤效应
    c盘清理
    如何把Visual Studio完全安装在其他磁盘
  • 原文地址:https://www.cnblogs.com/zdfffg/p/11551264.html
Copyright © 2011-2022 走看看