【opencv】教程代码 —ImgProc (9) 图像金字塔

9. Pyramids.cpp

c2c3ab997c2e30eedace736abb450547.png

721970a2701c185f71faf07f49d22f5c.jpeg

c24a9b9f759d34a8f7af35ef63588363.png

/**
 * @file Pyramids.cpp
 * @brief 本样例代码展示了图像金字塔(pyrDown和pyrUp)的使用
 * @author OpenCV团队
 */


// 导入需要的头文件
#include "iostream"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"


// 使用命名空间
using namespace std;
using namespace cv;


// 定义窗口名称
const char* window_name = "Pyramids Demo";


/**
 * @function main
 */
int main( int argc, char** argv )
{
    /// 通用提示
    cout << "\n Zoom In-Out demo \n "
            "------------------  \n"
            " * [i] -> 放大   \n"    // i代表放大
            " * [o] -> 缩小  \n"    // o代表缩小
            " * [ESC] -> 关闭程序 \n" << endl; // ESC退出程序


    //![load]
    // 定义图像文件名,如果命令行参数大于2则使用参数作为文件名,否则默认为 "chicky_512.png"
    const char* filename = argc >=2 ? argv[1] : "chicky_512.png";


    // 加载图像
    Mat src = imread( samples::findFile( filename ) );


    // 检查图像是否成功加载
    if(src.empty()){
        printf(" 打开图像出错\n");
        printf(" 程序参数: [image_name -- 默认 chicky_512.png] \n");
        return EXIT_FAILURE;  // 如果图像加载失败,则返回错误代码
    }
    //![load]


    //![loop]
    // 循环处理图像
    for(;;)
    {
        //![show_image]
        // 显示图像
        imshow( window_name, src );
        //![show_image]
        // 等待用户按键输入
        char c = (char)waitKey(0);


        // 如果用户按了ESC键,退出循环
        if( c == 27 )
        { break; }
        //![pyrup]
        // 如果用户按了'i',则放大图像
        else if( c == 'i' )
        { 
            pyrUp( src, src, Size( src.cols*2, src.rows*2 ) );
            printf( "** Zoom In: Image x 2 \n" );
        }
        //![pyrup]
        //![pyrdown]
        // 如果用户按了'o',则缩小图像
        else if( c == 'o' )
        { 
            pyrDown( src, src, Size( src.cols/2, src.rows/2 ) );
            printf( "** Zoom Out: Image / 2 \n" );
        }
        //![pyrdown]
    }
    //![loop]


    // 返回成功代码
    return EXIT_SUCCESS;
}

该代码是一个完整的图像处理应用程序,主要演示了如何通过OpenCV库中的图像金字塔函数(pyrUp, pyrDown)来放大和缩小图像。用户可以通过命令行参数传入需要处理的图片名称,并通过键盘输入'i'或'o'来实现图片的放大和缩小,从而直观地观察图像金字塔的处理效果。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 07:22:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 07:22:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 07:22:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 07:22:05       20 阅读

热门阅读

  1. 浅聊openGauss逻辑架构

    2024-03-28 07:22:05       21 阅读
  2. SBA架构5G核心网

    2024-03-28 07:22:05       20 阅读
  3. Mysql实用SQL例子

    2024-03-28 07:22:05       17 阅读
  4. 深入理解RabbitMQ:配置与应用场景详解

    2024-03-28 07:22:05       21 阅读
  5. [C语言]带连接数统计功能的多进程TCP服务器

    2024-03-28 07:22:05       18 阅读
  6. Speech Dispatcher required for SpeechSynthesis API @FreeBSD

    2024-03-28 07:22:05       22 阅读
  7. Kotlin by关键字

    2024-03-28 07:22:05       20 阅读
  8. Kotlin非常用关键字使用记录

    2024-03-28 07:22:05       24 阅读
  9. kafka安装并测试

    2024-03-28 07:22:05       23 阅读