pyrDown和pyrUp函数

pyrDown函数:

函数的作用:

对图像进行滤波然后进行下采样

函数调用形式:

void pyrDown(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT )

参数详解:


InputArray src:表示输入图像

 OutputArray dst:表示输出图像

const Size& dstsize=Size():表示输出图像的大小

 int borderType=BORDER_DEFAULT:表示图像边界的处理方式


函数的操作过程:、

  • 1、与高斯内核卷积:

    \frac{1}{16} \begin{bmatrix} 1 & 4 & 6 & 4 & 1  \\ 4 & 16 & 24 & 16 & 4  \\ 6 & 24 & 36 & 24 & 6  \\ 4 & 16 & 24 & 16 & 4  \\ 1 & 4 & 6 & 4 & 1 \end{bmatrix}

  • 2、将所有偶数行和列去除。


pyrUp函数

函数功能:

对图像进行高斯滤波,然后进行上采样;

函数调用形式:

void pyrUp(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT )


参数跟上面函数一样;



opencv代码:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

using namespace cv;

/// 全局变量
Mat src, dst, tmp;
char* window_name = "Pyramids Demo";


/**
 * @函数 main
 */
int main( int argc, char** argv )
{
  /// 指示说明
  printf( "\n Zoom In-Out demo  \n " );
  printf( "------------------ \n" );
  printf( " * [u] -> Zoom in  \n" );
  printf( " * [d] -> Zoom out \n" );
  printf( " * [ESC] -> Close program \n \n" );

  /// 测试图像 - 尺寸必须能被 2^{n} 整除
  src = imread( "../images/chicky_512.jpg" );
  if( !src.data )
    { printf(" No data! -- Exiting the program \n");
      return -1; }

  tmp = src;
  dst = tmp;

  /// 创建显示窗口
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );
  imshow( window_name, dst );

  /// 循环
  while( true )
  {
    int c;
    c = waitKey(10);

    if( (char)c == 27 )
      { break; }
    if( (char)c == 'u' )
      { pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
        printf( "** Zoom In: Image x 2 \n" );
      }
    else if( (char)c == 'd' )
     { pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
       printf( "** Zoom Out: Image / 2 \n" );
     }

    imshow( window_name, dst );
    tmp = dst;
  }
  return 0;
}


代码理解:

  • 装载图像(此处路径由程序设定,用户无需将图像路径当作参数输入)

    /// 测试图像 - 尺寸必须能被 2^{n} 整除
    src = imread( "../images/chicky_512.jpg" );
    if( !src.data )
      { printf(" No data! -- Exiting the program \n");
        return -1; }
    
  • 创建两个Mat实例, 一个用来储存操作结果(dst), 另一个用来存储零时结果(tmp)。

    Mat src, dst, tmp;
    /* ... */
    tmp = src;
    dst = tmp;
    
  • 创建窗口显示结果

    namedWindow( window_name, CV_WINDOW_AUTOSIZE );
    imshow( window_name, dst );
    
  • 执行无限循环,等待用户输入。

    while( true )
    {
      int c;
      c = waitKey(10);
    
      if( (char)c == 27 )
        { break; }
      if( (char)c == 'u' )
        { pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ) );
          printf( "** Zoom In: Image x 2 \n" );
        }
      else if( (char)c == 'd' )
       { pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ) );
         printf( "** Zoom Out: Image / 2 \n" );
       }
    
      imshow( window_name, dst );
      tmp = dst;
    }
    

    如果用户按 ESC 键程序退出。 此外,它还提供两个选项:

    • 向上采样 (按 ‘u’)

      pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 )
      

      函数 pyrUp 接受了3个参数:

      • tmp: 当前图像, 初始化为原图像 src 。
      • dst: 目的图像( 显示图像,为输入图像的两倍)
      • Size( tmp.cols*2, tmp.rows*2 ) : 目的图像大小, 既然我们是向上采样, pyrUp 期待一个两倍于输入图像( tmp )的大小。
    • 向下采样(按 ‘d’)

      pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 )
      

      类似于 pyrUp, 函数 pyrDown 也接受了3个参数:

      • tmp: 当前图像, 初始化为原图像 src 。
      • dst: 目的图像( 显示图像,为输入图像的一半)
      • Size( tmp.cols/2, tmp.rows/2 ) :目的图像大小, 既然我们是向下采样, pyrDown 期待一个一半于输入图像( tmp)的大小。
    • 注意输入图像的大小(在两个方向)必须是2的冥,否则,将会显示错误。

    • 最后,将输入图像 tmp 更新为当前显示图像, 这样后续操作将作用于更新后的图像。

      tmp = dst;


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这三个函数都是OpenCV中常用的图像缩放函数,但它们的实现方式不同,适用于不同的场景。 1. pyrUp函数是图像金字塔中的上采样函数,用于将输入图像的尺寸增大一倍。该函数的调用格式为: ```c++ pyrUp(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT ) ``` 其中,src表示输入图像,dst表示输出图像,dstsize表示输出图像的大小,borderType表示边界处理方式,默认为BORDER_DEFAULT。pyrUp函数的实现是通过低通滤波和插值算法来实现的,可用于图像的放大和配准等领域。 2. pyrDown函数是图像金字塔中的下采样函数,用于将输入图像的尺寸缩小一倍。该函数的调用格式为: ```c++ pyrDown(InputArray src, OutputArray dst, const Size& dstsize=Size(), int borderType=BORDER_DEFAULT ) ``` 其中,src表示输入图像,dst表示输出图像,dstsize表示输出图像的大小,borderType表示边界处理方式,默认为BORDER_DEFAULT。pyrDown函数的实现是通过高斯滤波和降采样算法来实现的,可用于图像的缩小和图像特征提取等领域。 3. resize函数是OpenCV中常用的图像缩放函数,可以将图像的尺寸缩小或放大到指定的大小。该函数的调用格式为: ```c++ resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR ) ``` 其中,src表示输入图像,dst表示输出图像,dsize表示输出图像的大小,fx和fy表示在水平和竖直方向上的缩放系数,interpolation表示插值算法,默认为INTER_LINEAR。resize函数的实现是通过插值算法来实现的,可用于图像的缩放和图像配准等领域。和pyrUp函数pyrDown函数不同,resize函数可以将图像缩小或放大到任意指定的大小,而不仅仅是缩小或放大一倍。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值