MATLAB: How the difference(difference of adjacent pixel values) histogram of an image can be plot ?I want to generate a difference image and histogram of thedifference image.I have write a code,but getting an error.Please help.

difference histogramImage Processing Toolbox

clc;
clear all;
close all hidden;
[file,path]=uigetfile('*.bmp;*.jpg','Pick an Image File');
if isequal(file,0) isequal(path,0) warndlg('User Pressed Cancel'); else Host_im=imread(file);
[r c p] = size(Host_im);
if p==3
Host_im = rgb2gray(Host_im);
end
figure(1);
imshow(Host_im);
title('Input Image');
end
X = inputdlg('Enter Value from 0 to 5');
L = str2num(X{1});
P = 2^L;
H = Host_im;
figure(2);
imhist(Host_im);
title('Histogram of Input Image(Fused Image)');
xlabel('Pixel value');
ylabel('No of pixels');
%%%%%%%%Histogram Shifting
location1 = find(Host_im<P);
location2 = find(Host_im>(255-P));
H(location1)=H(location1)+P;
H(location2)=H(location2)-P;
figure(3);
imshow(H,[]);
title('Image of Histogram Shifted Image');
figure(4);
imhist(H);
title('Histogram of Histogram Shifted Image');
xlabel('Pixel value');
ylabel('No of pixels');
%%%%%%To find pixel difference
Host_im = double(Host_im);
Row = r;
Col = c;
k =1;
for i = 1:Row
im_val = Host_im(i,:);
if mod(i,2)==0 %%%%%%%%%%checkreminder
d(k:i*Col,:) = im_val(end:-1:1)';
else
d(k:i*Col,:) = im_val(1:1:end)';
end
k = i*Col + 1;
end
Diff(1) = d(1);
Diff(2:length(d)) = abs(d(1:length(d)-1) - d(2:length(d)));
x = d; len = length(Diff); Row = r; Col =c; k =1; for i = 1:Row if mod(i,2)==0 star_p = Col; mid_p = -1; end_p = 1; else star_p = 1; mid_p = 1; end_p = Col; end for j = star_p:mid_p:end_p nw(i,j) = Diff(k); k = k+1; end end location14 = find(nw<P); location15 = find(nw>(255-P));
nw(location14)=nw(location14)+P; nw(location15)=nw(location15)-P;
figure(35);
imshow(nw,[]);
title('Image of difference Histogram Shifted Image');
figure(45);
imhist(nw);
[counts x]=imhist(nw)
title('Histogram of difference Histogram Shifted Image');
xlabel('Pixel value');
ylabel('No of pixels');

Best Answer

To get the difference image, you can do that all in one line with a single call to conv2(). Do you want the average of the difference in all 8 directions, or just the different in one directions? Either way, just make up your kernel for the situation you want and call conv2(). Then call hist() or histc() to get its histogram.