网站链接: element-ui dtcms
当前位置: 首页 > 技术博文  > 技术博文

OpenCV——图像金字塔

2021/6/26 23:49:16 人评论

#Fu Xianjun.All Rights Reserved. 来了来了,今天的学习开始了! 我们的目标: 能够理解高斯金字塔与拉普拉斯金字塔的处理过程 能够使用相关函数进行高斯金字塔可逆性分析 能够使用相关函数进行拉普拉斯金字塔无损恢复图像 能够掌握ROI的…

#@Fu Xianjun.All Rights Reserved.

来了来了,今天的学习开始了!

我们的目标:

能够理解高斯金字塔与拉普拉斯金字塔的处理过程

能够使用相关函数进行高斯金字塔可逆性分析

能够使用相关函数进行拉普拉斯金字塔无损恢复图像

能够掌握ROI的应用处理

能够掌握泛洪填充算法并使用相关函数进行处理

高斯金字塔

import cv2 
import numpy as np

#高斯金字塔
def pyramid_demo(image,level):
    temp = image.copy()
    pyramid_images = []
    for i in range(level):
        dst = cv2.pyrDown(temp)
        pyramid_images.append(dst)
        cv2.imshow("pyramid_down_"+str(i), dst)
        temp = dst.copy()
    return pyramid_images[level-1]

src = cv2.imread("lena.jpg")
cv2.imshow("input image", src)
pyramid_demo(src,4)
cv2.waitKey(0)
cv2.destroyAllWindows()

拉普拉斯金字塔

# 拉普拉斯金字塔构建
G0 = cv2.imread("lena.bmp")
cv2.imshow("input image",G0)
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
G4=cv2.pyrDown(G3)
L0 = cv2.subtract(G0,cv2.pyrUp(G1))
L1 = cv2.subtract(G1,cv2.pyrUp(G2))
L2 = cv2.subtract(G2,cv2.pyrUp(G3))
L3 = cv2.subtract(G3,cv2.pyrUp(G4))
cv2.imshow("G1",G1)
cv2.imshow("G2",G2)
cv2.imshow("G3",G3)
cv2.imshow("G4",G4)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 使用拉普拉斯金字塔恢复高分辨图片
l3=cv2.pyrUp(G4)
l2=cv2.pyrUp(l3)
l1=cv2.pyrUp(l2)
l0=cv2.pyrUp(l1)
G00=L0+cv2.pyrUp(G1)
cv2.imshow("l0",l0)
cv2.imshow("G00",G00)
cv2.imshow("input image",G0)
cv2.waitKey(0)
cv2.destroyAllWindows()

金字塔的应用

import cv2
import numpy as np
A = cv2.imread('apple.png')
A = cv2.resize(A,(256,256),interpolation=cv2.INTER_CUBIC) 
B = cv2.imread('orange.png')
B = cv2.resize(B,(256,256),interpolation=cv2.INTER_CUBIC)
# 生成高斯金字塔
G = A.copy()
gpA = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpA.append(G)
    
G = B.copy()
gpB = [G]
for i in range(5):
    G = cv2.pyrDown(G)
    gpB.append(G)
# 产生Laplacian金字塔
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)

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)
# 合并
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)
# 重新构建图像
ls_ = LS[0]
for i in range(1,6):
    ls_ = cv2.pyrUp(ls_)
    ls_ = cv2.add(ls_, LS[i])
# 连接
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imshow("apple",A)
cv2.imshow("orange",B)
cv2.imshow("LS",ls_)
cv2.imshow("Real",real)
cv2.waitKey()
cv2.destroyAllWindows()

ROI在了解

import cv2
src=cv2.imread("lena.jpg")
cv2.imshow("first_image", src)
face = src[100:200, 100:200]    #选择200:300行、200:400列区域作为截取对象
gray = cv2.cvtColor(face, cv2.COLOR_RGB2GRAY)  #生成的的灰度图是单通道图像
backface = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)  #将单通道图像转换为三通道RGB灰度图,因为只有三通道的backface才可以赋给三通道的src
src[100:200, 100:200] = backface
cv2.imshow("face", src)
cv2.waitKey(0)
cv2.destroyAllWindows()

import cv2
lena=cv2.imread("lena.jpg")
src=lena.copy()
h,w=src.shape[:2]
h1=h//40
for i in range(0,44,2):
    roi=src[i*h1:(i+1)*h1,0:w]
    gray = cv2.cvtColor(roi, cv2.COLOR_RGB2GRAY)
    backface = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) 
    src[i*h1:(i+1)*h1,0:w] = backface
cv2.imshow("inputface", lena)
cv2.imshow("face", src)
cv2.waitKey(0)
cv2.destroyAllWindows()

泛洪填充

import cv2 as cv
import numpy as np
# 彩色图像填充
def fill_color_demo(src,seedx,seedy):
    img_copy = src.copy()
    h, w, ch = src.shape
    mask = np.zeros([h+2, w+2], np.uint8)
    cv.floodFill(img_copy, mask, (seedx, seedy), (0, 255, 0),(50, 50, 50), (100, 100, 100), cv.FLOODFILL_FIXED_RANGE)
    cv.imshow("color_demo", img_copy)

src = cv.imread('AM.png')
fill_color_demo(src,140,140)
cv.waitKey()
cv.destroyAllWindows()

我们下次见!

相关资讯

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?