MATLAB: How to make an image comparison slider in Matlab

image processingslider

I'd like to create an image comparison slider in Martlab similar to this (https://www.w3schools.com/howto/howto_js_image_comparison.asp) however I have no idea how can I implement that. The slider show display two images interactively by sliding the slider over the image. I'd greatly appreaciate if somone can help me with this. Thanks in advance.

Best Answer

I patched together this example using a figure callback function. You can try it and modify according to your requirement
im_left = im2double(imread('pears.png')); % opening two example images
im_right = im2double(imread('peacock.jpg'));
im_right = imresize(im_right, size(im_left, [1 2])); % making the dimensions equal
f = figure();
ax = axes('Units', 'pixels');
imshow(im_left);
hold(ax);
im_handle = imshow(im_right);
im_handle.AlphaData = zeros(size(im_left, [1 2]));
axes_pos = cumsum(ax.Position([1 3]));
f.WindowButtonMotionFcn = {@cb, size(im_left,2), im_handle, axes_pos};
function cb(obj, ~, im_width, im_handle, axes_pos)
x_pos = obj.CurrentPoint(1);
if x_pos > axes_pos(1) && x_pos < axes_pos(2)
left_cols = floor(floor(x_pos - axes_pos(1))/diff(axes_pos) * im_width);
im_handle.AlphaData(:, 1:left_cols) = 0;
im_handle.AlphaData(:, left_cols+1:end) = 1;
end
end
Related Question