opencv-037-图像金字塔

知识点

图像金字塔是对一张输入图像先模糊再下采样为原来大小的1/4(宽高缩小一半)、不断重复模糊与下采样的过程就得到了不同分辨率的输出图像,叠加在一起就形成了图像金字塔、所以图像金字塔是图像的空间多分辨率存在形式。这里的模糊是指高斯模糊,所以这个方式生成的金字塔图像又称为高斯金字塔图像。高斯金字塔图像有两个基本操作:

  • reduce 是从原图生成高斯金字塔图像、生成一系列低分辨图像

  • expand 是从高斯金字塔图像反向生成高分辨率图像

规则:

  1. 图像金字塔在redude过程或者expand过程中必须是逐层
  2. reduce过程中每一层都是前一层的1/4

API

reduce 操作 pyrDown
expand操作 pyrUp

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void pyramid_up(Mat &image, vector<Mat> &pyramid_images, int level);
void pyramid_down(vector<Mat> &pyramid_images);

int main() {
Mat src = imread("../images/test1.jpg");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

vector<Mat> p_images;
pyramid_up(src, p_images, 2);
pyramid_down(p_images);

waitKey(0);
return 0;
}

void pyramid_down(vector<Mat> &pyramid_images) {
for (int i = pyramid_images.size() - 1; i > -1; --i) {
Mat dst;
pyrUp(pyramid_images[i], dst);
imshow(format("pyramid_down_%d", i), dst);
}
}

void pyramid_up(Mat &image, vector<Mat> &pyramid_images, int level) {
Mat temp = image.clone();
Mat dst;
for (int i = 0; i < level; ++i) {
pyrDown(temp, dst);
imshow(format("pyramid_up_%d", i), dst);
temp = dst.clone();
pyramid_images.push_back(temp);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import cv2 as cv


def pyramid_down(pyramid_images):
level = len(pyramid_images)
print("level = ",level)
for i in range(level-1, -1, -1):
expand = cv.pyrUp(pyramid_images[i])
cv.imshow("pyramid_down_"+str(i), expand)


def pyramid_up(image, level=3):
temp = image.copy()
# cv.imshow("input", image)
pyramid_images = []
for i in range(level):
dst = cv.pyrDown(temp)
pyramid_images.append(dst)
# cv.imshow("pyramid_up_" + str(i), dst)
temp = dst.copy()
return pyramid_images


src = cv.imread("D:/images/master.jpg")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)
# pyramid_up(src)
pyramid_down(pyramid_up(src))

cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github