Matlab:如何在网格上放置图像

Matlab:如何在网格上放置图像,matlab,image-processing,Matlab,Image Processing,我正在学习matlab。我正在尝试创建一个演示,用于显示图像上几何变换的效果,如旋转、缩放等。 我创建了一个网格,如下所示: I=imread('cameraman.tif'); x = linspace(-1,10); y = linspace(0,1); a=imrotate(I,30); % rotate image figure imshow(a) grid on; T = [cos(theta) -sin(theta) 0; sin(theta) cos(theta)

我正在学习matlab。我正在尝试创建一个演示,用于显示图像上几何变换的效果,如旋转、缩放等。 我创建了一个网格,如下所示:

I=imread('cameraman.tif');
x = linspace(-1,10);
y = linspace(0,1);
a=imrotate(I,30); % rotate image
figure  imshow(a)
grid on;
T = [cos(theta) -sin(theta) 0;
     sin(theta) cos(theta)  0;
         0          0       1];
T = [sx 0 0;
     0 sy 0;
     0 0 1];
theta = pi/6; %// 30 degrees in radians
Trotate = [cos(theta) -sin(theta) 0;
          sin(theta) cos(theta)  0;
             0          0       1];
sx = 0.75;
sy = 0.6;
Tscale = [sx 0 0;
     0 sy 0;
     0 0 1];

%// Make objects
objRotate = affine2d(Trotate);
objScale = affine2d(Tscale);

%// Read in image
im = imread('cameraman.tif');

%// Rotate the image
out = imwarp(im, objRotate);

%// Take the rotated image and scale it
out2 = imwarp(out, objScale);

%// Show the images
figure;
subplot(1,3,1);
imshow(im); title('Original Image');
subplot(1,3,2);
imshow(out); title('Rotated Image');
subplot(1,3,3);
imshow(out2); title('Scaled Image');
我想在网格上放置图像并执行几何变换:

1.rotate by 30 degrees.
2. Scale like newx=0.75x and newy=0.6y  (x and y are coordinates on grid)
但我无法将图像放在网格上旋转和缩放。帮帮我,伙计们

我个人会用它对你的图像进行几何变换。如果要进行组合旋转和缩放,则无法在一个组合操作中完成此操作。您需要分两步完成此操作,并且需要为每个操作创建适当的变换矩阵。在MATLAB中,变换矩阵实际上是计算机图形学中通常定义的转置

旋转的变换矩阵如下所示:

I=imread('cameraman.tif');
x = linspace(-1,10);
y = linspace(0,1);
a=imrotate(I,30); % rotate image
figure  imshow(a)
grid on;
T = [cos(theta) -sin(theta) 0;
     sin(theta) cos(theta)  0;
         0          0       1];
T = [sx 0 0;
     0 sy 0;
     0 0 1];
theta = pi/6; %// 30 degrees in radians
Trotate = [cos(theta) -sin(theta) 0;
          sin(theta) cos(theta)  0;
             0          0       1];
sx = 0.75;
sy = 0.6;
Tscale = [sx 0 0;
     0 sy 0;
     0 0 1];

%// Make objects
objRotate = affine2d(Trotate);
objScale = affine2d(Tscale);

%// Read in image
im = imread('cameraman.tif');

%// Rotate the image
out = imwarp(im, objRotate);

%// Take the rotated image and scale it
out2 = imwarp(out, objScale);

%// Show the images
figure;
subplot(1,3,1);
imshow(im); title('Original Image');
subplot(1,3,2);
imshow(out); title('Rotated Image');
subplot(1,3,3);
imshow(out2); title('Scaled Image');
θ表示旋转角度。此矩阵将逆时针旋转图像

缩放每个轴的变换矩阵如下所示:

I=imread('cameraman.tif');
x = linspace(-1,10);
y = linspace(0,1);
a=imrotate(I,30); % rotate image
figure  imshow(a)
grid on;
T = [cos(theta) -sin(theta) 0;
     sin(theta) cos(theta)  0;
         0          0       1];
T = [sx 0 0;
     0 sy 0;
     0 0 1];
theta = pi/6; %// 30 degrees in radians
Trotate = [cos(theta) -sin(theta) 0;
          sin(theta) cos(theta)  0;
             0          0       1];
sx = 0.75;
sy = 0.6;
Tscale = [sx 0 0;
     0 sy 0;
     0 0 1];

%// Make objects
objRotate = affine2d(Trotate);
objScale = affine2d(Tscale);

%// Read in image
im = imread('cameraman.tif');

%// Rotate the image
out = imwarp(im, objRotate);

%// Take the rotated image and scale it
out2 = imwarp(out, objScale);

%// Show the images
figure;
subplot(1,3,1);
imshow(im); title('Original Image');
subplot(1,3,2);
imshow(out); title('Rotated Image');
subplot(1,3,3);
imshow(out2); title('Scaled Image');
sx
sy
是您想要的每个轴的比例因子

现在,你要做的就是做两个变换矩阵,然后一个接一个地应用。您可以使用并指定上述任一变换矩阵来执行此操作。这将创建一个可与
imwarp
一起使用的变换对象。在此之后,您将调用
imwarp
两次:每个转换一次。因此,让我们使用
cameraman.tif
图像,这是MATLAB系统路径的一部分。因此,您的代码如下所示:

I=imread('cameraman.tif');
x = linspace(-1,10);
y = linspace(0,1);
a=imrotate(I,30); % rotate image
figure  imshow(a)
grid on;
T = [cos(theta) -sin(theta) 0;
     sin(theta) cos(theta)  0;
         0          0       1];
T = [sx 0 0;
     0 sy 0;
     0 0 1];
theta = pi/6; %// 30 degrees in radians
Trotate = [cos(theta) -sin(theta) 0;
          sin(theta) cos(theta)  0;
             0          0       1];
sx = 0.75;
sy = 0.6;
Tscale = [sx 0 0;
     0 sy 0;
     0 0 1];

%// Make objects
objRotate = affine2d(Trotate);
objScale = affine2d(Tscale);

%// Read in image
im = imread('cameraman.tif');

%// Rotate the image
out = imwarp(im, objRotate);

%// Take the rotated image and scale it
out2 = imwarp(out, objScale);

%// Show the images
figure;
subplot(1,3,1);
imshow(im); title('Original Image');
subplot(1,3,2);
imshow(out); title('Rotated Image');
subplot(1,3,3);
imshow(out2); title('Scaled Image');
这是运行上述代码时得到的结果:

上面的代码执行您在上面指定的每个转换,我创建了一个新的图形,显示原始图像和您指定的每个转换

现在,如果您想将此图像放置在
x
y
坐标的特定范围内,您当然可以这样做,但需要指定两个附加标志:
XData
YData
。对于每个字段,可以指定轴的最小值和最大值。在您的情况下,
x
为-1到10,y为0和1。请记住,在您变换图像后,我不建议您将图像应用于此网格,因为它看起来会被压扁。但是,如果这是您想要的,您只需执行以下操作:

imshow(out2, 'XData', [-1 10], 'YData', [0 1]);


这应该让你开始。祝你好运

谢谢,这是一个很好的开端。我也实现了仿射变换。你能给我一些关于实现透视转换的想法吗?因为它的矩阵中没有标识列。(如果我错了,请纠正我)@nikhilk-当然!只需使用:class-。创建对象的方法与
affine2d
相同。我引用的链接中有一些关于如何进行投影变换的示例。