how to place two images on canvas by a reference vector to relate their relative distance, one image is fixed

Hi all,
I want to place two images together but with a reference vector to indicate their relative position, like the following image: circle point in image1' top left corner points to the star point of the image2, which gives a vector. I can't attach two points' coordinate, vector, and image 1 and 2 because system's capacity constrain. I'm confused by the matlab coordinate system, can anyone help me how to overlay them? Thank you so much!
image1
image2

4 Comments

Is your question about how to display multiple images in the same figure? Or how to have a figure that is always located at the same spot of a displayed image?
You might read up on image (specifically on placing images at a specific coordinate): https://www.mathworks.com/help/matlab/ref/image.html#bur_mqu-1, and of course imread (though assumably you already know that). If you need help displaying multiple images you can read this guide: https://www.mathworks.com/help/images/display-multiple-images.html
thanks for getting back, but the problem is not about how to show the image, it's about how to place two images on a canvas with some fixed position vector.
Follow up on what @John pointed out for image() -- the XData and YData inputs. You can specify the exact data coordinates to place the centers of the bottom left and top right pixels.

Sign in to comment.

Answers (2)

This code does a sloppy job of showing relative placement using the coordinate placement methods discussed in the comments. It doesn't account for scaling or disparate image sizes, but you can read more on the links in the comments to figure that out. Hopefully this gets you started in the right direction, assuming it addresses your actual question. Notice that the point in the plot moves with the coordinates inputted to the image(x, y, c) call to stay at the same "point" on the image.
pic1 = imread('ngc6543a.jpg');
pic2 = imread('peppers.png');
x1 = 10;
y1 = 15;
x2 = 0;
y2 = 50;
xp = [x1 + 250, x2 + 75];
yp = [y1 + 500, y2 + 31];
figure(1)
hold on
image(x1, y1, pic1);
image(x2, y2, pic2);
plot(xp, yp, 'or', 'MarkerSize', 15, 'LineWidth', 3)
plot(xp, yp, 'r', 'MarkerSize', 15, 'LineWidth', 3)
figure(2)
x2 = 300;
y2 = 50;
xp = [x1 + 250, x2 + 75];
yp = [y1 + 500, y2 + 31];
hold on
image(x1, y1, pic1);
image(x2, y2, pic2);
plot(xp, yp, 'or', 'MarkerSize', 15, 'LineWidth', 3)
plot(xp, yp, 'r', 'MarkerSize', 15, 'LineWidth', 3)

3 Comments

Hi, thanks for the help. I tried your method and image() function, the result is not what I want:
In fact, I'm doing image stitiching and I'm trying to overlay the third image (which is image1 above) to the stitched image (which is image2 above), I wish to show both images, not simply stack the third one onto the previous one. The desired output is something like this:
I used goodnote from my ipad to get this image but it's still not what I wanted because they are simply stacked, not overlayed---I want to show information from both two images.
And the star point is the top left corner on the reference image, other later images will need to form a vector so that I can know their relative distance. Do you have any idea about it? Thanks@John@Walter Roberson@William Rose
I'm just trying to understand this. I would have thought that "stacking" and "overlaying" would be the same thing in terms of displaying images, unless you're talking about alpha channels? I'm guessing you're also trying to scale and reposition the images on the screen? If so, you can set the scale of the images using image, just by passing two vectors rather than scalar values for the X-Y coordinates. This allows you to set the size and position of your images.
From the documentation:
"image(x,y,C) specifies the image location. Use x and y to specify the locations of the corners corresponding to C(1,1) and C(m,n). To specify both corners, set x and y as two-element vectors. To specify the first corner and let image determine the other, set x and y as scalar values. The image is stretched and oriented as applicable."
Is that what you're looking for? If not, can you explain what you mean by stacking?
This article might also be helpful if you're looking for ways to expose or suppress certain areas of your 3 images:

Sign in to comment.

You write: "I'm confused by the matlab coordinate system":
X increases as one goes to the right, from the top left corner. Y increases as one goes down, from the top left corner.
Like @John, I don;t understand exactly what you want. The warped image in the top figure is not the simply a warped verison of image 2.
Please share the files containining image1 and image2. Also please share the commands you used to display them, since the commands are probably relevant to what you are trying to do.
I see that you have attached 3 mat files. Collectively, they contain the following information:
endPt=[1008,57];
starPt_image2=[735,1];
Warp2topleft=[2,510];
endPt is the (x,y) location of the star in figure 1.
Warp2topleft is the (x,y) location of the open circle in figure 1.

3 Comments

Y increases as one goes down, from the top left corner.
That only applies if the axes YDir property is set to reverse which is something that happens automatically if you use image or imshow and hold is not on.
Otherwise, the normal condition, Y increases as you go up from the bottom left corner.
Hi thank you for the help!! I reuploaded image1 and image2 and a desired output image. And my code to produce image1 is:
figure;
mesh(warpedImage2);
view([0 -90.000])
hold on;
quiver(Warp2topleft(1), Warp2topleft(2), ...
Warp2RefVec(1), Warp2RefVec(2), 0, 'Color', 'r', 'LineWidth', 2);
plot(startPt(1), startPt(2), 'ro', ...
'MarkerSize', 10, 'LineWidth', 2);
plot(endPt(1), endPt(2), 'r*', ...
'MarkerSize', 10, 'LineWidth', 2);
hold off
code for image2:
figure
mesh(stitchedImg1_shifted);
view([0 -90.000])
hold on
plot(starPt_image2(1), starPt_image2(2), 'r*', ...
'MarkerSize', 10, 'LineWidth', 2);
hold off
And for question from John: "Is your question about how to display multiple images in the same figure? Or how to have a figure that is always located at the same spot of a displayed image?" My question is: I want to have just one figure, but overlay two images, and the relative distance from image1 to image2 is always fixed, this translation distance is given by a vector, Warp2RefVec.
Sorry that some of my expression may not be clear, please help as you can, thank you so much!

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 7 Feb 2023

Edited:

on 8 Feb 2023

Community Treasure Hunt

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

Start Hunting!