How Can I Fit a Gaussian curve to the histogram of the image. Please Answer Urgent

129 views (last 30 days)
I have a thermal image containing some defects and rest are background. I have calculated signal-to-noise ration of each defect. Now, I wish to select an SNR threshold value at which the defect are detectable. I have read in some literature I found a few are doing some calculation on SNR and Gaussian to select the SNR threshold. The figure is given below, In this they are fitting a Gaussian curve to the histogram of the data which follows exact curve where the data is spread. Please suggest any way to do so.
  1 Comment
Rik
Rik on 1 May 2020
Edited: Rik on 1 May 2020
Your question is not urgent, the reason for that is explained here.
Do you mean you want to fit two Gaussians (one for the background, one for the other data)? It will help if you attach the actual data in a mat file, if you mention the release you're using and if you have the curve fitting toolbox.

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 1 May 2020
Edited: Ameer Hamza on 1 May 2020
If you have optimization toolbox, then you can try lsqcurvefit(). Follwoing code shows an example
x = linspace(-6, 6, 100);
y = normpdf(x) + rand(size(x))*0.03; % random datapoints
norm_fun = @(mu, sd, x) 1/(sqrt(2*pi)*sd)*exp(-(x-mu).^2/(2*sd^2));
[sol, f] = lsqcurvefit(@(p, x) norm_fun(p(1), p(2), x), rand(1,2), x, y);
mu = sol(1);
sd = sol(2);
stem(x, y, 'bo');
hold on
plot(x, norm_fun(mu, sd, x), 'b-', 'LineWidth', 2)
  4 Comments
Ameer Hamza
Ameer Hamza on 1 May 2020
Have you created the histogram of the image? Using histfit is the next step. First, you need to find out how to create the histogram. Check this: https://www.mathworks.com/help/images/ref/imhist.html. Is this what you are looking for?

Sign in to comment.

More Answers (2)

John
John on 26 Aug 2021
You can use the definition of probability and gaussian distribution, as follows.
a = 0.3;% standard deviation
b = 0.5; % mean
ny = 3000;
y = a.*randn(ny,1) + b; % data
mu = mean(y); % data mean
sd = std(y); % data std
nbins = round(ny/20);
hg = histogram(y, nbins);
hold on;
y_bin = hg.Values;
x_bin = (hg.BinEdges(1:end-1)+hg.BinEdges(2:end))/2;
y_pdf = 1/(2*pi*sd)*exp(-(x_bin-mu).^2/(2*sd^2));
area_hist = trapz(x_bin, y_bin);
area_pdf = trapz(x_bin, y_pdf);
h_fit = plot(x_bin,y_pdf*area_hist/area_pdf,'LineWidth',3);
legend(h_fit, sprintf('mu %.3f, std %.3f', mu, sd));
title(sprintf('Gaussian fit to histogram of %d observations with %d bins', length(y), nbins));
  2 Comments
Berna  Macin
Berna Macin on 30 Jan 2022
x_bin = (hg.BinEdges(1:end-1)+hg.BinEdges(2:end))/2;
I am wondering what this part means. Thank you in advance.
Image Analyst
Image Analyst on 30 Jan 2022
@Berna Macin the edges are the starting and stopping values for the bin. Most of the values that were put into the bin were not those exact values, but somewhere in between. So rather than plotting the count vs starting value, or the count vs stoppin values (which you get from edges), the code gives you the middle value of the bin and plots count vs. the middle bin value.

Sign in to comment.


Image Analyst
Image Analyst on 26 Aug 2021
I don't think you really need to fit a Gaussian like you asked and the others answered. I think your real question is how to find and characterize the thermal defect regions in the image.
So do you mean that you have a thermal image which is mostly uniform but there are some bright blobs on it? And you have computed the SNR for every pixel in the image? And you want to know what threshold to use to segment out the defect blobs? As the threshold gets above the mean background level, you will start to leave the noise and get fewer blobs. If you're maybe 2 or 3 standard deviations above the background mean, you may have only a few big blobs in the image, that represent defect areas (hot spots). You can use bwareafilt() to filter out blobs so that only big ones show up, like only get blobs with 100 or more pixels. Then you can count them, get their mean intensity, or whatever.
mask = SNRImage > someThreshold; % Get blobs having high SNR
mask = bwareafilt(mask, [100, inf]); % Keep only blobs bigger than 100 pixels.
% Get area and temperature from original temperature image, not SNR image.
props = regionprops(mask, temperatureImage, 'Area', 'MeanIntensity');
allAreas = [props.Area]
allMeanTemperatures = [props.MeanIntensity]
So there is no one "right" or best threshold. If your histogram is really a Gaussian there is really a continuum of gray levels and each one will segment out different pixels in the image. You just have to pick a threshold that works, like the mean of the image plus 2 standard deviations or however you want to define it -- no continuous Gaussian distribution curve is needed. Once you have that yoiu can find and characterize the thermal defects.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!