#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys
#图像金字塔
def pyramid_demo(img):
level = 3
temp = img.copy()
pyramid_img = []
for i in range(level):
dst = cv.pyrDown(temp)
pyramid_img.append(dst)
cv.namedWindow("pyramid_down"+str(i),cv.WINDOW_NORMAL)
cv.imshow("pyramid_down"+str(i),dst)
temp = dst.copy()
return pyramid_img
#图像金字塔
def lapalian_demo(img):
pyramid_imgs = pyramid_demo(img)
level = len(pyramid_imgs)
for i in range(level-1,-1,-1):
if (i-1)<0:
expand =cv.pyrUp(pyramid_imgs[i],dstsize=img.shape[:2])
lpls = cv.subtract(img,expand)
else:
expand =cv.pyrUp(pyramid_imgs[i],dstsize=pyramid_imgs[i-1].shape[:2])
lpls = cv.subtract(pyramid_imgs[i-1],expand)
cv.namedWindow("lapalian_up"+str(i),cv.WINDOW_NORMAL)
cv.imshow("lapalian_up"+str(i),lpls)
def img_test():
img = cv.imread('E:/chenopencvblogimg/lena22.jpg')
#判断是否读取成功
if img is None:
print("Could not read the image,may be path error")
return
cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
cv.imshow("origin Pic",img)
lapalian_demo(img)
#让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
cv.waitKey(0)
#销毁窗口
cv.destroyAllWindows()
if __name__ == '__main__':
sys.exit(img_test() or 0)
OpenCV__Python图像金字塔_教程18
最新推荐文章于 2023-08-09 15:58:40 发布