图像金字塔绘制

发布时间 2023-10-12 15:42:11作者: WWHf

图像金字塔绘制

untitled

代码:

clc
clear all
close all
%% 初始化参数
dog = imread("kobi.png");
% 灰度化
dogGray = rgb2gray(dog);
[W , H ] =size(dogGray);
% 高斯图像金字塔
dogPyramid =Pyramid(dogGray,W,H,4);
%% 绘图
figure;
%取第一张图片参数,作为金字塔底层大小
[FirstW ,FirstH] = size(cell2mat(dogPyramid(1)));
%上一张图参数
LastW = 0;LastH = 0;
%偏移量
deltaH = 0;deltaW = 0;
for i=1:4
    im = cell2mat(dogPyramid(i));
    im=uint8(im);
    im = flipud(im);  
    [W,H] = size(im);
    if i == 1
        [Y,Z]=meshgrid(1:H,1 :W);
    else
        deltaH = deltaH + LastH/4;
        deltaW = deltaW + LastW/4;
        [Y,Z]=meshgrid(deltaH:FirstH - deltaH - 1,deltaW :FirstW - deltaW - 1);
    end
    X=ones(W,H)*(4 - i)*5;
    LastW = W;
    LastH = H;
    surf(X,Y,Z,im,'FaceColor','texture','EdgeColor','none');
    colormap(gray);
     hold on;
end


function out=Pyramid(img_filter , w , h , p)
gausPyramid=cell(1,p);
% 5*5高斯核,sigma=1
kernel=fspecial('gaussian',5,1);
% 高斯核乘以4(亮度守恒)
kernel1=4.*kernel;
gausPyramid(1)=mat2cell(img_filter,w,h);
for i=2:p        
    %% 高斯金字塔
    % 滤波
    img_filter1=filter2(kernel,img_filter,'same');
    % 下采样:去掉偶数行、列
    img_filter1(2:2:end,:)=[];
    img_filter1(:,2:2:end)=[];    
    [m,n]=size(img_filter1);
    gausPyramid(i)=mat2cell(img_filter1,m,n);
    img_filter=img_filter1;
end
out = gausPyramid;
end