image-processing - MATLAB 图像变换

标签 image-processing computer-vision matlab

下面的代码描述了图像的仿射变换,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)的合适像素值。

我该怎么办?

Enter image description here

Enter image description here

我的代码是

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

最佳答案

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

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

结果:
itsaplane

关于image-processing - MATLAB 图像变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13366771/

相关文章:

c++ - 关闭 GTK 窗口

java - 如何使用图像在 Java 中实现人脸识别?

python - 成功的 cv2.calibrateCamera 后如何获得良好的 cv2.stereoCalibrate

opencv - 如何确保所有图像的方向相同?

matlab - matlab中的k最近邻分类器

matlab - 当用线条覆盖时,图像逐渐消失

matlab - 如何在matlab中平均多个图像?

image-processing - 如何从 3D 图像中去除小物体?

python - 我尝试在矩阵单元格上执行加法操作时发生溢出警告

matlab - 如何填充matlab函数 "scatter"生成的图像内的空白区域