简体   繁体   中英

Matlab: How to place image on a grid

I am learning matlab. I am trying to create a demo for showing effects of geometric transformations on a image like rotation, scaling, etc. I have created a grid like this:

I=imread('cameraman.tif');
x = linspace(-1,10);
y = linspace(0,1);
a=imrotate(I,30); % rotate image
figure  imshow(a)
grid on;

I want to place an image over the grid and perform geometric transformations:

1.rotate by 30 degrees.
2. Scale like newx=0.75x and newy=0.6y  (x and y are coordinates on grid)

But I'm unable to put the image on grid and rotate as well as scale it. Help me guys.

I would personally use imwarp to perform geometric transformations to your image. If you want to do a combined rotation and scaling, there isn't a way to do this in one combined operation. You would need to do this in two steps and you would need to create the proper transformation matrices for each operation. In MATLAB, the transformation matrix is actually the transpose of what is normally defined in computer graphics.

The transformation matrix for rotation looks like this:

T = [cos(theta) -sin(theta) 0;
     sin(theta) cos(theta)  0;
         0          0       1];

theta denotes the angle of rotation. This matrix will rotate your image counter-clockwise.

The transformation matrix for scaling each axis looks like this:

T = [sx 0 0;
     0 sy 0;
     0 0 1];

sx and sy are the scaling factors for each axis you want.

Now, all you have to do is make two transformation matrices, then apply one right after the other. You can do this with affine2d and you specify either of the transformation matrix above. This will create a transformation object that you can use with imwarp . After this, you would call imwarp twice: one for each transformation you want. As such, let's use the cameraman.tif image that's part of the MATLAB system path. Therefore, your code would look like this:

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');

This is what I get when you run the above code:

在此处输入图片说明

The above code performs each of the transformations you have specified above, and I have created a new figure that shows the original image and each transformation you specified.

Now, if you want to place this image within a certain range of x and y co-ordinates, you can certainly do that, but you specify two additional flags: XData and YData . For each of these fields, you specify the minimum and maximum values for the axes. In your case, it would be -1 to 10 for the x and 0 and 1 for the y. Bear in mind that after you transform your image, I don't recommend you apply the image to this grid as it will look very squished. However, if this is what you want, you would simply do:

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

This should get you started. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM