Matlab impyramid问题

mad*_*mad 4 matlab image image-processing computer-vision

我用matlab在impyramid中遇到了问题.我正在尝试保存二进制图像的一个缩小版本,以及这个二进制图像的两个缩小版本.在matlab中这很简单,如下面的代码所示:

scale1_2= impyramid(compressed_image, 'reduce');
scale1_4= impyramid(scale1_2, 'reduce');
Run Code Online (Sandbox Code Playgroud)

因此,尺寸为810x1080的图像以405x540和203x270像素保存.我面临的问题是当我尝试将这两个图像扩展回来时具有与之前相同的尺寸.

scaled_result1_2=impyramid(scale1_2,'expand');
scaled_result1_4=impyramid(impyramid(scale1_4,'expand'), 'expand');
Run Code Online (Sandbox Code Playgroud)

因此,预计scaled_result1_2和scaled_result1_4再次是810x1080图像,但不是:

>>size(scaled_result1_2)
     809        1079
>>size(scaled_result1_4)
     809        1077
Run Code Online (Sandbox Code Playgroud)

我需要这两个图像再次具有相同的810x1080像素,但是impyramid无法做到这一点.如果我用imresize调整这些图像的大小,它会通过放大和模糊图像来执行图像金字塔分解吗?我应该使用哪种方法(插值)来得到类似的结果?

ray*_*ica 5

如果您实际打开impyramid并查看源代码,则归结为imresize通话.具体来说,这是当您expand在调用impyramid时使用时A定义为图像时发生的情况:

M = size(A,1);
N = size(A,2);
scaleFactor = 2;
outputSize = 2*[M N] - 1;
kernel = makePiecewiseConstantFunction( ...
    [1.25   0.75    0.25   -0.25   -0.75   -1.25   -Inf], ...
    [0.0    0.125   0.5     0.75    0.5    0.125    0.0]);
kernelWidth = 3;

B = imresize(A, scaleFactor, {kernel, kernelWidth}, ...
    'OutputSize', outputSize, 'Antialiasing', false);
Run Code Online (Sandbox Code Playgroud)

如您所见,outputSize定义为图像尺寸减去1的两倍,这就是每个尺寸偏离1个像素的原因.该函数makePiecewiseConstantFunction是一个定义的本地函数impyramid.我会让你打开它,亲眼看看.在调用上面的代码之前,请确保已定义.

因此,只需删除1的减法即可实现您想要的效果.

因此,请调用上面的代码,但更改outputSize为:

outputSize = 2*[M N];
Run Code Online (Sandbox Code Playgroud)

但是,如果你想要冒险,你可以自己修改这个源代码以获取一个标志,如果你设置它true,它将不会减1并false执行减法.因此,您可以修改标题impyramid来执行此操作:

function B = impyramid(A, direction, padding)
Run Code Online (Sandbox Code Playgroud)

然后,在完成任何计算之前的开始,您可以这样做:

if nargin == 2
    padding = false;
end
Run Code Online (Sandbox Code Playgroud)

这允许您在impyramid没有第三个参数的情况下调用,默认情况下不会填充.

一旦完成,在声明的expand部分中if,您可以:

else
    scaleFactor = 2;
    outputSize = 2*[M N];
    if ~padding %// Change
        outputSize = outputSize - 1;
    end
    kernel = makePiecewiseConstantFunction( ...
        [1.25   0.75    0.25   -0.25   -0.75   -1.25   -Inf], ...
        [0.0    0.125   0.5     0.75    0.5    0.125    0.0]);
    kernelWidth = 3;
end
Run Code Online (Sandbox Code Playgroud)

然后,嵌套if语句将检查您是否要允许输出图像的大小2M x 2N2M - 1 x 2N - 1.因此,当您完成修改代码后,您可以执行以下操作:

scaled_result1_2 = impyramid(scale1_2, 'expand', true);
scaled_result1_4 = impyramid(impyramid(scale1_4,'expand', true), 'expand', true);
Run Code Online (Sandbox Code Playgroud)