# Fit image gradient into exponential distribution function

1 view (last 30 days)
Hi,
I've developed a very simple code which computes the gradient of a filtered B&W image, compute the gradient, normalize it and then shows the histogram.
For instance :
I_filt = imgaussfilt(I,5.4);
N = normalize(Gmag,'range');
figure
histmag = histogram(N,100,'Normalization','count');
ylabel('Count');
xlim([0 1.5]);
It can be seen that the resulting histogram can be fitted with an exponential probability function, in order to be able to treshold it by limiting the cumulative function to 0.99 or some other treshold.
I'm interested also in viewing the curve of the exponential probability function.
The point is that I cannot understand what matlab tool I could use to do the fit, I tried using expfit and exppdf on N, but that does not seem to work.

Subhadeep Koley on 8 Nov 2019
Hi, Refer the code below which fits n degree polynomial to the histogram data. You can tweak the ‘n’ value to find the best fit.
% Perform gaussian filtering
I_filt = imgaussfilt(I,5.4);
% Rescaling the image
N = rescale(Gmag);
% Histogram count calculation
[counts,binLocations] = imhist(N, 100);
% Plot histogram
figure; bar(1:100, counts);
% nth order Polynomial fitting
n = 5;
c=polyfit(binLocations, counts,n);
histFit=polyval(c,binLocations);
% Plot the fit
hold on; plot(binLocations.*100,histFit,'LineWidth',3);
axis tight; legend({'Histogram','Fit'});
Hope this helps!