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 
#include 
#include 

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;


你可能感兴趣的:(图像的滤波方式)