在matlab中调整图像大小而不使用内置的imresize()函数

时间:2018-04-12 02:53:26

标签: matlab image-resizing

我在matlab中编写自己的脚本/函数而不使用内置命令," imresize"但我得到3个输出图像而不是单个图像。我也在这里分享我的代码。请有人发现我的错误。

    %zoomin out an imagge

originalImage = imread('imggerm1.jpg');
[origImRows, origImColumns] = size(originalImage);

newImage = zeros(origImRows/2, origImColumns/2);
newImRow = 1; newImColumn = 1;

for row = 1:2:origImRows
    for column = 1:2:origImColumns
        newImage(newImRow, newImColumn)=originalImage(row, column);
        newImColumn = newImColumn+1;
    end
    newImRow = newImRow+1;
    newImColumn = 1;
end

figure; imshow(originalImage);
figure; imshow(newImage/255);

Output Image

1 个答案:

答案 0 :(得分:1)

这是因为您最初读取彩色图像,其中每个像素由3个数字编码。尝试输入size(originalImage),你会看到这个数组是3维的(最后一个维度的大小是3)。

在您的代码中包含以下行:

[origImRows, origImColumns] = size(originalImage);

产生您不期望的结果:origImColumns似乎是3倍大。

您的代码很容易修复。下面我略微改变了3行:#4,#6和#11:

    %zoomin out an imagge

originalImage = imread('1.jpg');
[origImRows, origImColumns,~] = size(originalImage);

newImage = zeros(origImRows/2, origImColumns/2,3);
newImRow = 1; newImColumn = 1;

for row = 1:2:origImRows
    for column = 1:2:origImColumns
        newImage(newImRow, newImColumn,:)=originalImage(row, column,:);
        newImColumn = newImColumn+1;
    end
    newImRow = newImRow+1;
    newImColumn = 1;
end

figure; imshow(originalImage);
figure; imshow(newImage/255);