【问题标题】:MATLAB image transformationMATLAB图像变换
【发布时间】:2012-11-13 18:46:48
【问题描述】:

以下代码描述了图像 I 的仿射变换,

T = [sx, 0, 0;...
      0, sy, 0;...
      0, 0, 1];
tform = maketform('affine', T);
[J,cdata,rdata] = imtransform(I,tform);

得到变换后的图像J后,我想在图像J上找到I(5,5)的合适像素值。

我该怎么办?

我的代码是

function test()
    sx = 0.5; sy = 1;
    theta = pi/4;
    Ts = [sx, 0, 0;...
    0, sy, 0;...
    0, 0, 1];
    Tr = [cos(theta) -sin(theta) 0;...
          sin(theta) cos(theta), 0; ...
          0, 0, 1];
    T = Ts*Tr;

    I = imread('image_0002.jpg');
    tform = maketform('affine', T);
    [J,xdata,ydata] = imtransform(I,tform);
    h = ydata(2) - ydata(1);
    w = xdata(2) - xdata(1);

    %%
    gridOx = meshgrid(1:50:size(I,2), 1:50:size(I,1));
    gridOy = meshgrid(1:50:size(I,1), 1:50:size(I,2))';
    go = [gridOy(:), gridOx(:), ones(length(gridOx(:)),1)];

    for i=1:size(go,1)
        I = makept(I, go(i,1), go(i,2));
    end
    imshow(I);

    %%
    gt = T*go';

    TL = T*[1,1,1]';
    BL = T*[size(I,1), 1, 1]';
    TR = T*[1, size(I,2), 1]';
    BR = T*[size(I,1), size(I,2), 1]';
    minr = min([TL(1), TR(1), BL(1), BR(1)]);
    minc = min([TL(2), TR(2), BL(2), BR(2)]);

    %%
    gt = int32(round(gt));
    r = gt(1,:) - minr+1;
    c = gt(2,:) - minc+1;

    figure,hold on;
    for i=1:length(r)
        J = makept(J, r(i), c(i));
        plot(gt(1, i), gt(2, i));
    end
    figure, imshow(J);

    close all;
end

function I = makept(I, r, c)
    mr = max(r-2,1);
    mc = max(c-2,1);
    maxr = min(r+2,size(I,1));
    maxc = min(c+2,size(I,2));

    I(mr:maxr, mc:maxc,:) = 0;
    I(mr:maxr, mc:maxc,1) = 255;
end

【问题讨论】:

    标签: image-processing computer-vision matlab


    【解决方案1】:

    您可以像转换图像一样转换像素坐标。

    appropriatePixel = J(T*[5;5;1]);
    

    编辑

    您发布的代码已经很接近了。永远记住,对于图像,图像的原点在左上角,并且图像尺寸的顺序是列-行,这与矩阵不同。您在某些地方考虑到了这一点,但并非在所有地方都考虑到了这一点。

    下面的代码应该可以解决问题。 gt = T*go 改为使用 T 的转置,mincminr 互换。

    function airplane()
           close all
           sx = 0.5; sy = 1;
           theta = pi/4;
           Ts = [sx, 0, 0;...
           0, sy, 0;...
           0, 0, 1];
           Tr = [cos(theta) -sin(theta) 0;...
               sin(theta) cos(theta), 0; ...
               0, 0, 1];
           T = Ts*Tr;
    
           I = imread('airplane2.png');
           tform = maketform('affine', T);
           [J,xdata,ydata] = imtransform(I,tform);
    
            %% 
           gridOx = meshgrid(1:50:size(I,2), 1:50:size(I,1));
           gridOy = meshgrid(1:50:size(I,1), 1:50:size(I,2))';
           go = [gridOx(:), gridOy(:), ones(length(gridOx(:)),1)];
    
           figure; imshow(I); hold on;
           scatter(go(:,1), go(:,2), 25, 'rs', 'filled');
    
           %%
           gt = T'*go'; 
    
           TL = T*[1,1,1]';
           BL = T*[size(I,1), 1, 1]';
           TR = T*[1, size(I,2), 1]';
           BR = T*[size(I,1), size(I,2), 1]';
           minc = min([TL(1), TR(1), BL(1), BR(1)]);
           minr = min([TL(2), TR(2), BL(2), BR(2)]);
    
    
           %%
           gt = int32(round(gt));
           r = gt(1,:) - minr+1;
           c = gt(2,:) - minc+1;
    
           figure; imshow(J);hold on;
           scatter(c, r, 25, 'rs', 'filled');
    
    end
    

    结果:

    【讨论】:

    • no.. 如果我变换像素坐标,变换像素可能是 [-5.5, -20]。
    • 对。基于这个例子,我只考虑(正)缩放变换。那么在这种情况下,首先对图像的所有四个角进行此变换,取最小值 x 和 y 并从上面减去,然后加一个,这样原点就回到了 1,1。
    • 你在测试什么sxsy
    • 非常感谢..我找到了解决这个问题的另一种方法..我添加了答案...我们也需要对其进行缩放..
    • 当转换后的图像很大时,matlab 会重新缩放它.. 所以我们需要乘以一个缩放因子... as NPtImage(:,1) = NPt(:,1) - xdata(1) + 1; NPtImage(:,2) = NPt(:,2) - ydata(1) + 1;我们现在需要这样做... gt = int32(round(gt)); r = gt(1,:) - minr+1; c = gt(2,:) - minc+1;.. 无论如何,非常感谢您的回答和努力.. 非常感谢.. NPtImage(:,1) = NPtImage(:,1)*scalex; NPtImage(:,2) = NPtImage(:,2)*scaley;
    【解决方案2】:

    我找到了另一种方法.. 使用 'tformfwd'.. 我们需要应用缩放,并在 http://blogs.mathworks.com/steve/2006/02/14/spatial-transformations-maketform-tformfwd-and-tforminv/ 中进行了解释

             function [new_im, grn, gcn] = transformImage(I, gr, gc, TM)
                  [new_im xdata ydata] = imtransform(I, TM);
                  w = xdata(2)-xdata(1) +1;
                  h = ydata(2)-ydata(1)+1;
                  scalex = size(new_im,2)/w;
                  scaley = size(new_im,1)/h;
    
                  % cartesian coordinates
                  go = [gc(:), gr(:)];
    
                  %% cartesian coordinates
                  NPt = tformfwd(TM, go);
    
                  % convert to image coordinates
                  NPtImage(:,1) = NPt(:,1) - xdata(1) + 1;
                  NPtImage(:,2) = NPt(:,2) - ydata(1) + 1;
                  NPtImage(:,1) = NPtImage(:,1)*scalex;
                  NPtImage(:,2) = NPtImage(:,2)*scaley;
                  NPtImage = round(NPtImage);
    
                  grn = NPtImage(:,2);
                  gcn = NPtImage(:,1);
                  grn = reshape(grn, size(gr,1), size(gr,2));
                  gcn = reshape(gcn, size(gc,1), size(gc,2));
    
                  showimgs = false;
                  if showimgs
                      I = uint8(I);
                      new_im = uint8(new_im);
    
                      figure, imshow(I), hold on;
                      for i=1:size(go,1)
                          plot(go(i,1), go(i,2), 'r*');
                      end
    
                      figure, imshow(new_im, 'XData', xdata, 'YData', ydata);
                      hold on
                      axis on
                      for i=1:size(go,1)
                          plot(NPt(i,1), NPt(i,2), 'r*')
                      end
    
                      tmpim = new_im;
                      for i=1:size(NPtImage,1)
                          tmpim = makept(tmpim, NPtImage(i,2), NPtImage(i,1), 'b');
                      end
                      figure, imshow(tmpim);
                  end
    
                  end
    
    
                  function I = makept(I, r, c, color)
                  mr = max(r-2,1);
                  mc = max(c-2,1);
                  maxr = min(r+2,size(I,1));
                  maxc = min(c+2,size(I,2));
    
                  I(mr:maxr, mc:maxc,:) = 0;
    
                  if strcmp(color, 'r')
                      I(mr:maxr, mc:maxc,1) = 255;
                  else
                      I(mr:maxr, mc:maxc,3) = 255;
                  end
                  end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多