【OpenCV-Python】教程:3-8 图像金字塔

OpenCV Python 图像金字塔

【目标】

  • 学习图像金字塔
  • cv2.pyrUp(), cv2.pyrDown()

【原理】

在某些情况下,需要处理不同分辨率的图像。我们需要创建不同分辨率的图像,最高分辨率的在最下面,最低分辨率的在最上面,看起来像金字塔。

高斯金字塔中的最高级别(低分辨率)是通过移除低级别(高分辨率)图像中的连续行和列而形成的。较高级别的每个像素由高斯权重的基础级别中的5个像素贡献而成。 M ∗ N M*N MN 的图像变成了 ( M / 2 ) ∗ ( N / 2 ) (M/2)*(N/2) (M/2)(N/2)

【代码】

  • pyrDown

【OpenCV-Python】教程:3-8 图像金字塔_第1张图片

import cv2

img = cv2.imread('messi5.jpg', 1)
lower1 = cv2.pyrDown(img)
lower2 = cv2.pyrDown(lower1)
lower3 = cv2.pyrDown(lower2)
lower4 = cv2.pyrDown(lower3)

cv2.imshow('src', img)
cv2.imshow('down1', lower1)
cv2.imshow('down2', lower2)
cv2.imshow('down3', lower3)
cv2.imshow('down4', lower4)

cv2.waitKey(0)
cv2.destroyAllWindows()
  • pyrUp

【OpenCV-Python】教程:3-8 图像金字塔_第2张图片

import cv2

img = cv2.imread('lena-small.jpg', 1)
big1 = cv2.pyrUp(img)
big2 = cv2.pyrUp(big1)

cv2.imshow('src', img)
cv2.imshow('up1', big1)
cv2.imshow('up2', big2)

cv2.waitKey(0)
cv2.destroyAllWindows()
  • 图像混合

import cv2
import numpy as np,sys
A = cv2.imread('apple.jpg')
B = cv2.imread('orange.jpg')
# generate Gaussian pyramid for A
G = A.copy()
gpA = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpA.append(G)
# generate Gaussian pyramid for B
G = B.copy()
gpB = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpB.append(G)
# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i-1],GE)
    lpA.append(L)
# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in range(5,0,-1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i-1],GE)
    lpB.append(L)
# Now add left and right halves of images in each level
LS = []
for la,lb in zip(lpA,lpB):
    rows,cols,dpt = la.shape
    ls = np.hstack((la[:,0:cols//2], lb[:,cols//2:]))
    LS.append(ls)
# now reconstruct
ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])
# image with direct connecting each half
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
# cv2.imwrite('Pyramid_blending2.jpg',ls_)
# cv2.imwrite('Direct_blending.jpg',real)

cv2.imshow("apple", A)
cv2.imshow("orange", B)
cv2.imshow("Pyramid_blending", ls_)
cv2.imshow("Direct_blending", real)

cv2.waitKey(0)
cv2.destroyAllWindows()

【接口】

  • pyrDown
void cv::pyrDown	(	InputArray 	src,
OutputArray 	dst,
const Size & 	dstsize = Size(),
int 	borderType = BORDER_DEFAULT 
);
cv2.pyrDown(	src[, dst[, dstsize[, borderType]]]	) ->	dst

图像模糊后下采样。
默认情况下,输出图像的尺寸是通过输入图像来计算的。但是任何情况下,得满足 ∣ d s t . w i d t h ∗ 2 − s r c . w i d t h ∣ ≤ 2 |dst.width*2-src.width|≤2 dst.width2src.width2, ∣ d s t . h e i g h t ∗ 2 − s r c . h e i g h t ∣ ≤ 2 |dst.height*2-src.height|≤2 dst.height2src.height2
在下采样前需要先进行高斯模糊,

1 256 [ 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 ] \frac{1}{256}\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} 25611464141624164624362464162416414641

  • src: 输入图像
  • dst: 输出图像
  • dstsize: 输出图像的尺寸
  • borderType: 扩边类型,不支持 BORDER_CONSTANT
  • pyrUp
void cv::pyrUp	(	InputArray 	src,
OutputArray 	dst,
const Size & 	dstsize = Size(),
int 	borderType = BORDER_DEFAULT 
);
cv2.pyrUp(	src[, dst[, dstsize[, borderType]]]	) ->	dst

图像上采样后高斯模糊

  • src: 输入图像
  • dst: 输出图像
  • dstsize: 输出图像的尺寸
  • borderType: 扩边类型,不支持 BORDER_CONSTANT

【参考】

  1. OpenCV官方文档

你可能感兴趣的:(#,OpenCV,#,Python,opencv,python,图像金字塔)