-1

我编写了一个读取图像并scaling在 x 轴方向上执行 2 个单位的代码。Scaling matrix由从文本文件中读取的值填充。

Scaling Matrix好像

2 0 0 
0 1 0 
0 0 1

原始图像

在此处输入图像描述

转换后的图像(在 X 方向缩放 2 个单位)

在此处输入图像描述

代码

file = importdata('transform_c.txt');
fileData = file.data;
image = imread('mecca06.pgm');
[row, col] = size(image);
scalingMatrix = zeros(3,3);

scalingMatrix(1,1) = fileData(2);
scalingMatrix(1,2) = fileData(3);
scalingMatrix(1,3) = fileData(4);
scalingMatrix(2,1) = fileData(5);
scalingMatrix(2,2) = fileData(6);
scalingMatrix(2,3) = fileData(7);
scalingMatrix(3,1) = fileData(8);
scalingMatrix(3,2) = fileData(9);
scalingMatrix(3,3) = fileData(10);

m1Inverse = inv(scalingMatrix);

outputImage = applyTransformation(image, row, col, m1Inverse);
figure
imshow(outputImage);

function outImage = applyTransformation(image, row, col, m1Inverse)
    points = zeros(3,1);
    for i=1:row
        for j=1:col
            points(1,1) = i;
            points(2,1) = j;
            points(3,1) = 1;
            m2 = m1Inverse * points;

            x = m2(1,1);
            y = m2(2,1);
            xlb = floor(x);
            ylb = floor(y);

           if(xlb <= 0)
             xlb = 1;
           end

           if(xlb > row)
             xlb = row;
           end
           if(ylb <= 0)
             ylb = 1;
           end

           if(ylb > col)
             ylb = col;
           end

           xub = xlb+1;
           yub = ylb+1;

           if(xub <= 0)
              xub = 1;
           end

          if(xub > row)
             xub = row;
          end

          if(yub <= 0)
             yub = 1;
          end

          if(yub > col)
             yub = col;
          end

          exub = xub-x;
          eyub = yub-y;
          exlb = x-xlb;
          eylb = y-ylb;
          outImage(i,j) = (exub*eyub*image(xlb,ylb))+(exlb*eyub*image(xub,ylb))+(exub*eylb*image(xlb,yub))+(exlb*eylb*image(xub,yub));
        end
    end
end

我的问题是如何修改上面的代码以获得未裁剪的图像?

我想得到以下图像

在此处输入图像描述

4

1 回答 1

1

尝试查看此代码是否产生您需要的结果:

img = imread('aSxLS.png');

scale = [
  2 0 0 
  0 1 0 
  0 0 1
];

tform = maketform('affine',inv(scale));
img_tform = imtransform(img,tform,'bilinear');

figure();
imshow(img);

figure();
imshow(img_tform);
于 2017-11-19T16:36:50.280 回答