MATLAB Answers

Fit image gradient into exponential distribution function

1 view (last 30 days)
Francescodario Cuzzocrea
Francescodario Cuzzocrea on 5 Nov 2019
Answered: Subhadeep Koley on 8 Nov 2019
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 = imread(image.jpg);
I_filt = imgaussfilt(I,5.4);
[Gx, Gy] = imgradientxy(I_filt,'prewitt');
[Gmag, Gdir] = imgradient(Gx,Gy);
N = normalize(Gmag,'range');
figure
histmag = histogram(N,100,'Normalization','count');
xlabel('Gradient');
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.
untitled1.resized.png
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.
Thank you in advance !!

  0 Comments

Sign in to comment.

Answers (1)

Subhadeep Koley
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.
% Read your image
I = imread('cameraman.tif');
% Perform gaussian filtering
I_filt = imgaussfilt(I,5.4);
% Gradient calculation
[Gx, Gy] = imgradientxy(I_filt,'prewitt');
[Gmag, Gdir] = imgradient(Gx,Gy);
% Rescaling the image
N = rescale(Gmag);
% Histogram count calculation
[counts,binLocations] = imhist(N, 100);
% Plot histogram
figure; bar(1:100, counts);
xlabel('Gradient'); ylabel('Count');
% 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!

  0 Comments

Sign in to comment.

Sign in to answer this question.