在matlab中展开图像

时间:2017-08-17 22:46:14

标签: matlab image-processing

我有一项任务是创建一个新的双倍大小的空白图像,然后用原始图像的行和列填充每个第二行和每列。 这是我的解决方案,但它不起作用(在评论中有一些关于此任务的建议,并且有一个用于扩展的高斯过滤器):

A = im2double(imread('orange.png'));

gA = expand(A);

function g = expand(I)

% Input:
% I: the input image
% Output:
% g: the image after the expand operation
% Please follow the instructions to fill in the missing commands.    
% 1) Create the expanded image. 
% The new image should be twice the size of the original image.
% So, for an n x n image you will create an empty 2n x 2n image
% Fill every second row and column with the rows and columns of the original image
% i.e., 1st row of I -> 1st row of expanded image
%       2nd row of I -> 3rd row of expanded image
%       3rd row of I -> 5th row of expanded image, and so on


[height,width,coef] = size(I);
new = ones(height * 2,width * 2,coef);
for c=1:coef
    for m=1:height
        for n=1:width
            new(m * 2 - 1,n * 2 - 1,c) = I(m,n,c);
        end
    end
end


% 2) Create a Gaussian kernel of size 5x5 and 
% standard deviation equal to 1 (MATLAB command fspecial)
gas = fspecial('gaussian', [5 5], 1);
% 3) Convolve the input image with the filter kernel (MATLAB command imfilter)
% Tip: Use the default settings of imfilter
% Remember to multiply the output of the filtering with a factor of 4


g = imfilter(new,gas) .* 4;
end

如果在matlab中有一些本机功能或者如何执行此操作,您能给我一个建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

你的代码似乎是正确的,除了,如烧杯所说,有一些(高度* 2,宽度* 2,coef)而不是零(高度* 2,宽度* 2,coef);.

尽管如此,请尝试这一点。有时候,较长的代码会使得捕获小错误变得棘手。

function g = expand(I)

[height,width,coef] = size(I);
new = zeros(height * 2,width * 2,coef);
new(1:2:end,1:2:end,1:end) = I;    

gas = fspecial('gaussian', [5 5], 1);


g = imfilter(new,gas) .* 4;