Center a Plot on top of an image in Matlab

Asked

Viewed 625 times

1

I’m crowding over an image, however with the code below the Plot happens at the beginning of the image at 0.0. I would like it to be done from the centre of the image in question. Excerpt from the code:

image=imread('imagem.jpg');
imshow(image);
hold on;
plot(X,Y,'k-','linewidth',2)
hold off;

1 answer

0


You can move the location where the image will be drawn by setting XData and YData in imshow. To center, you define that the image position (by default is [0.0]) is [-width/2, -height/2].

Example

% carrega a imagem
image=imread('imagem.jpg');

% calcula a metade da altura e largura
image_sz = size(image) * 0.5;

% desenha a imagem deslocada
imshow(image, 'XData', [-image_sz(1) image_sz(1)],'YData', [-image_sz(2) image_sz(2)]);
hold on;

Demontraction

To the following image:

Imagem de Exemplo

And that function:

t = 0:0.0001:2*pi;
x = 50 * cos(t);
y = 50 * sin(t);
plot(x,y) 

Centering the image, you have:

Resultado


To center the function to the image you can simply move the function elements to the center of the image. For example:

For that function:

t = 0:0.0001:2*pi;
x = 50 * cos(t);
y = 50 * sin(t);

Scroll using:

x = x + image_sz(1);
y = y + image_sz(2);

Which results in:

Outro resultado.

  • Plot is the one that needs to be centered not the image.

  • So just shift the elements of the function. But, I think shift the image more efficiently.

  • I considered it right because it solved otherwise, but in my case this is not possible I had to move the function as commented.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.