1. 程式人生 > >opencv(7)-影象金字塔+影象梯度

opencv(7)-影象金字塔+影象梯度

一、影象金字塔:高斯金字塔+拉普拉斯金字塔

第一步:

第二步:

上面兩步得到高斯金字塔,由高斯金字塔能得到拉普拉斯金字塔

拉普拉斯金字塔時,影象大小必須是2的n次方*2的n次方,不然會報錯

高斯金字塔程式碼

# -*- coding=GBK -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

def pyramid_demo(image):
    level = 3 #金字塔的層數
    temp = image.copy()
    pyramid_image = []
    for i in range(level):
        dst = cv2.pyrDown(temp)
        pyramid_image.append(dst)
        cv2.imshow('dst'+str(i),dst)
        temp = dst.copy()
    return pyramid_image

img = cv2.imread('dog.jpg',1)
cv2.imshow('src',img)
pyramid_demo(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

拉普拉斯金字塔程式碼:

# -*- coding=GBK -*-
import cv2
import numpy as np
import matplotlib.pyplot as plt

def pyramid_demo(image):
    level = 3 #金字塔的層數
    temp = image.copy()
    pyramid_image = []
    for i in range(level):
        dst = cv2.pyrDown(temp)
        pyramid_image.append(dst)
        cv2.imshow('dst'+str(i),dst)
        temp = dst.copy()
    return pyramid_image

# 拉普拉斯金字塔
def laplian_image(image):
    pyramid_images = pyramid_demo(image)
    level = len(pyramid_images)
    for i in range(level-1,-1,-1):
        if(i-1)<0:
            expand = cv2.pyrUp(pyramid_images[i],dstsize=image.shape[:2])
            lpls = cv2.subtract(image,expand)
            cv2.imshow('lpls_'+str(i),lpls)
        else:
            expand = cv2.pyrUp(pyramid_images[i],dstsize=pyramid_images[i-1].shape[:2])
            lpls = cv2.subtract(pyramid_images[i-1],expand)
            cv2.imshow('lpls_'+str(i),lpls)
            
img = cv2.imread('dog.jpg',1)
cv2.imshow('src',img)
laplian_image(img)
cv2.waitKey(0)
cv2.destroyAllWindows()