Can not shift image relative to plot with Imtranslate
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Walter Roberson
2021년 9월 20일
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
Walter Roberson
2021년 9월 20일
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)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!