# How to plot histogram of difference of two images using MATLAB

i would like to calculate difference of two images. Then, histogram of difference of two images. So I can plot a histogram but I am not sure whether it is true or not? Also, I am not sure about what should be second parameter of built-in histogramfunction.

My two images are original_result and output_image. And difference of two image is image_of_dif. my result:

image_of_dif = double(original_result) - double(output_image);
figure; histogram(image_of_dif(:), 511);


Let us suppose that images are coded on uint8, hence integers in $$[0,\ldots,255]$$. For the difference image, values can range from $$0-255$$ or $$255-0$$, the maximum span. So I would keep the negative differences by computing the histogram over all $$511=255-(-255)+1$$ integers in $$[-255,\ldots,255]$$.

• i changed second parameter of histogram to 511. is it okay now? – beyblade41 Dec 17 '19 at 13:51
• This looks ok to me. This way, you get an histogram of all possible values in the difference. – Laurent Duval Dec 20 '19 at 19:24

In your second line, casting with uint8() will change a negative value to 0. Avoid casting or change to another type that supports negative values.

• i changed my code. is it ok now? – beyblade41 Dec 17 '19 at 8:26