Can not shift image relative to plot with Imtranslate

4 views (last 30 days)
I try to overlay a plot and an loaded image to show resemblance. As the axis of my plot are position data from an experiment they are not centered around [0,0]. Therefore the image and the plot do not overlap. I tried to shift the image with imtranslate to overlay it with the plot, however the image is not translated but instead of the image there is a black box starting at [1 ,1]
gscatterplot, centered around [-30 , -84]
gscatterplot, centered around [-30 , -84] and image loaded by image=imread('overlay_test.png') and shown with imshow(image), also the plot axis was flipped by including the image.
gscatterplot, centered around [-30 , -84] and image shifted by [-30, -84] with imtranslate(image,[xcenter ycenter])
It seems I cant translate the image relative to the plot, just relative to an image axis. How can make the image translate relative to the plot axis? I would prefer not to shift the data to 0,0 as this is one of multiple subplots showing the same image section.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2021
and shown with imshow(image)
I do not recommend imshow() for this purpose: it does too many things to the axes that are not desirable in this kind of context.
Instead of using imtranslate() what you should be doing for your RGB image is
img = imread('overlay_test.png');
[nrow, ncol, ~] = size(img);
xloc = xcenter + [-1 1]/2 * ncol;
yloc = ycenter + [-1 1]/2 * nrow;
image(xloc, yloc, img)
This assumes that 1 pixel = 1 data unit; if not then you should
xloc = xcenter + [-1 1]/2 * ncol / pixels_per_x_dataunit;
yloc = ycenter + [-1 1]/2 * nrow / pixels_per_y_dataunit;
  2 Comments
tirbu ungel
tirbu ungel on 20 Sep 2021
Thanks for your input. This already works much better, I am now able to position and scale the image.
Do you have an idea why the plot could be mirrored horizontally. This is surprising to me because i set xlim and ylim last in the subplot.
Here is the code:
x=[-51, -9];
y=[-101 -66];
.
.
.
img = imread('overlay_test.png');
img = imrotate(img,10);
[nrow, ncol, ~] = size(img);
pixels_per_x_dataunit=20;
pixels_per_y_dataunit=20;
xloc = xcenter + [-1 1]/2 * ncol / pixels_per_x_dataunit;
yloc = ycenter + [-1 1]/2 * nrow / pixels_per_y_dataunit;
image(xloc, yloc, img);
hold on
c=gscatter(trace(:,4),trace(:,6),idx, [],[], [5 10 10 10 10 10 10 10 10 10],'doleg','off');
xlim(x)
ylim(y)
Also when I rotate the image with imrotate, there is again the black box in the shape of the original image. I checked the documentation for image() but could not find a parameter to change rotation angle when inserting. Is there a way to color this box white or is there a different function to rotate avoiding this behavior?
Walter Roberson
Walter Roberson on 20 Sep 2021
If "hold" is not on, then when you use image(), it will set the axis YDir to "reverse", which has the effect of making location (1,1,:) of the image array be in the upper left corner, and the last row of the image array be shown in the bottom left corner -- so row numbers (y values) increase down the axes instead of y values increasing up the axes.
To avoid that box in the rotated image, you should change the approach a little. Before displaying the image, do
ax = gca;
hgt = hgtransform(ax);
C = [ncol, nrow, 0]/2;
hgt.Matrix = makehgtform('translate', C, 'zrotate', deg2rad(10), 'translate', -C);
image(hgt, xloc, yloc, img)
hold on
gscatter stuff
Though as long as you are going to use an hgtransform, you can use that to scale and translate the image and then you should not need to pass in the location parameters. I am not sure at the moment but I think it would go like
ax = gca;
hgt = hgtransform(ax);
C = [ncol, nrow, 0]/2;
hgt.Matrix = makehgtform('translate', [xcenter, ycenter, 0], 'zrotate', deg2rad(10), 'scale', 1/pixels_per_x_dataunit, 'translate', -C);
image(hgt, img)

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!