Search code examples
imagematlablinematlab-figuresubplot

Subplot two images with a vertical separator


I'm trying to create a figure with an image on the left (original image) and an image on the right (warped image) and a vertical line separating them, like this:

enter image description here

I've tried this by creating axes, without ticks and labels. Then drawing a line from bottom to top and applying hold on and finally subplot the two images.

My code:

origImage = imread('F-original.png');
tform = affine2d([1 0 0; .5 1 0; 0 0 1]);
warpedImage = imwarp(origImage, tform, 'interp', 'bilinear');

axes('Position', [0 0 1 1], 'XTick', NaN, 'YTick', NaN);
line([1/2 1/2], [0 1], 'Color', 'k')
axes(gca)
hold on

subplot(1, 2, 1)
imshow(origImage)

subplot(1, 2, 2)
imshow(warpedImage)

But what actually happens is: the line flashes for a split second, but then disappears and all that can be seen are the subplots.

How to make this work?


Solution

  • To achieve that result you should use an annotation, which is a graphical object on the figure level (i.e. not confined to a specific axes, so doesn't require hold on etc.).

    Here's an example:

    function q54617073
    % Prepare images:
    origImage = imread('ngc6543a.jpg');
    tform = affine2d([1 0 0; .5 1 0; 0 0 1]);
    warpedImage = imwarp(origImage, tform, 'interp', 'bilinear');
    % Create a figure with a white background:
    figure('Color','w');
    % Plot the two images:
    subplot(1, 2, 1); imshow(origImage);
    subplot(1, 2, 2); imshow(warpedImage);
    % Add the Line
    annotation('line', [0.52 0.52], [0.2 0.8], 'Color', 'r', 'LineWidth', 3);
    

    Resulting in:

    enter image description here