MATLAB: How to convert two rgb images into grayscale? and then subtract

subtraction of images

I have two images in jpg format(one as reference image and other is captured image) . I want to convert them to grayscale and want to subtract them. How to do this?
Thanks in Advance.

Best Answer

img1 = imread('Image1.jpg');
img2 = imread('Image2.jpg');
D = rgb2gray(img1) - rgb2gray(img2)