OpenCV Filters - buildPyramid

Constructs the Gaussian pyramid for an image.

C++: void buildPyramid(InputArray src, OutputArrayOfArrays dst, int maxlevel, int borderType=BORDER_DEFAULT )

  • src – Source image. Check pyrDown() for the list of supported types.
  • dst – Destination vector of maxlevel+1 images of the same type as src . dst[0] will be the same as src . dst[1] is the next pyramid layer, a smoothed and down-sized src , and so on.
  • maxlevel – 0-based index of the last (the smallest) pyramid layer. It must be non-negative.
  • borderType – Pixel extrapolation method (BORDER_CONSTANT don’t supported). See borderInterpolate() for details.

The function constructs a vector of images and builds the Gaussian pyramid by recursively applying pyrDown() to the previously built pyramid layers, starting from dst[0]==src.

Reference: OpenCV Documentation - buildPyramid

This is a sample code (C++) with images for opencv build Pyramid filter.

sstring imgFileName = "lena.jpg";

cv::Mat src = cv::imread(imgFileName);
if (!{
    cout << "Unable to open file" << endl;
    return 1;

int maxVal = 4;
vector dstVect;
cv::buildPyramid(src, dstVect, maxVal);

cv::imshow("Source", src);

string imgName;
for (int i = 0; i < maxVal + 1; i++){
    stringstream ss;
    ss << "Filtered " << i;
    imgName = ss.str();

    cv::imshow(imgName, dstVect[i]);

    ss << ".jpg";
    imgName = ss.str();
    cv::imwrite(imgName, dstVect[i]);

return 0;

Filtered Image Source Image
Image size: 512 x 512

Image size: 256 x 256

Image size: 128 x 128

Image size: 64 x 64

Image size: 32 x 32

Download complete Visual Studio project.

No comments:

Post a Comment