OpenCV中pyrUp()和pyrDown()函数使用

cmakelists:

cmake_minimum_required(VERSION 3.7)
project(pyrUp_and_pyrDown)

set(CMAKE_CXX_STANDARD 11)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIRS})

set(SOURCE_FILES main.cpp)
add_executable(pyrUp_and_pyrDown ${SOURCE_FILES})
target_link_libraries(pyrUp_and_pyrDown ${OpenCV_LIBS})

程序:

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int main()
{
    Mat src_image = imread("ubuntu.png");
    Mat tmp_image, dst_image1, dst_image2;

    tmp_image = src_image;

    imshow("原始图片", src_image);

    //看一下函数原型
    //参数:源图像,目标图像
    //这里的const Size& dstsize = Size()是有默认值的,不像resize()没有默认值。默认值是放大或缩小一倍
    //第三个边界类型一般不管它,用默认值就好
/*
    CV_EXPORTS_W void pyrUp( InputArray src, OutputArray dst,
                             const Size& dstsize = Size(),
                             int borderType = BORDER_DEFAULT );
*/
/*
    CV_EXPORTS_W void pyrDown( InputArray src, OutputArray dst,
                               const Size& dstsize = Size(),
                               int borderType = BORDER_DEFAULT );
*/
    pyrUp(tmp_image, dst_image1, Size(tmp_image.cols*2, tmp_image.rows*2));
    pyrDown(tmp_image, dst_image2, Size(tmp_image.cols/2, tmp_image.rows/2));

    imshow("pyrUp放大图像", dst_image1);
    imshow("pyrDown缩小图像", dst_image2);

    waitKey(0);

    return 0;
}

这里注意一下,pyrup和pyrdown函数只能固定对图像进行长宽二倍或半倍的缩小和放大,不能是其他大小,也就是pyrDown()中的Size(tmp_image.cols/2, tmp_image.rows/2)必须是这个大小,换成其他的会报错,同理pyrUp()也是一样,必须是*2。如果想多次放缩,可以用循环多次调用。
例如像这样:

int main()
{
    Mat src_image=imread("open2.JPG");
    Mat src_image_gray;
    cvtColor( src_image, src_image_gray, CV_BGR2GRAY );
    //imshow("原始图:", src_image);

    Mat tmp_image, imagel;
    tmp_image = src_image_gray;

    for (int i = 0; i < 2; ++i)
    {
        pyrDown(tmp_image, imagel, Size(tmp_image.cols/2, tmp_image.rows/2));
        tmp_image=imagel;
    }
    cout<"*"<"降低分辨率后:", imagel);

    std::vector keypoints_1;
    Ptr orb = ORB::create();
    orb->detect(imagel,keypoints_1);
    cout<1), DrawMatchesFlags::DEFAULT);
    imshow("关键点图:", imagel_with_keypoints);

    waitKey(0);
    return 0;
}

你可能感兴趣的:(OpenCV库使用,opencv)