将特定函数拟合到直方图MATLAB

时间:2019-05-16 15:23:32

标签: matlab histogram data-fitting

对于我的研究,我的目标是分析这样的图片:

enter image description here

创建域密度Cn与域半径R的直方图,并将直方图拟合为以下函数:

Function to Fit

通过更改拟合参数Cm,beta和R0

我有以下代码:

eclear,clc
close all
file=uigetfile('*.jpg');
Image_Raw=imread(file);

disp('Select Scale Length')
imshow(Image_Raw)
title('Select Scale Length (Pixel) 2 clicks')
[X,Y]=ginputc(2,'Color','r');
Pixel_Length=sqrt((Y(2)-Y(1))^2+(X(2)-X(1))^2);
close all
Actual_Length=input('Enter actual scale length (unit determines histogram x axis unit): ');



response1=input('How many regions do you want to analyze? ');
while ~(floor(response1)==response1)
    response1=input('Please only enter integers: ');
end

file2=input('What do you want to call your excel file?','s');

%Image_Eq=histeq(Image_Raw);
%figure
%imshow(Image_Eq)
level=0.7;
major_t=[];
minor_t=[];
area_t=[];
for i=1:response1

    disp('Select region of interest, then double click to finish')
    Image_Crop=imcrop(Image_Raw);
    close all
    Image_Bin=im2bw(Image_Crop,level);
    Image_Bin=imcomplement(Image_Bin);
    Image_Bin=imclearborder(Image_Bin);
    imshow(Image_Bin)
    fprintf('Current threshold is %.2f. \n',level)
    response=input('Enter new threshold (0.1-0.9) or type 0 if done ');
    close all
    while ~(response==0)
        level=response;
        Image_Bin=im2bw(Image_Crop,level);
            Image_Bin=imcomplement(Image_Bin);
    Image_Bin=imclearborder(Image_Bin);
        imshow(Image_Bin)
        response=input('Enter new threshold (0.1-0.9) or type 0 if done ');
        close all
    end

    properties = regionprops(Image_Bin, {'Area','MajorAxisLength', 'MinorAxisLength'});

    properties_table = struct2table(properties);
    Major=properties_table.MajorAxisLength.*(Actual_Length/Pixel_Length);
    Minor=properties_table.MinorAxisLength.*(Actual_Length/Pixel_Length);
    Area=properties_table.Area.*(Actual_Length^2/Pixel_Length^2);
    major_t=[major_t Major'];
    minor_t=[minor_t Minor'];
    area_t=[area_t Area'];
    xlswrite([file2 '.xlsx'],{'Major Axis Length','Minor Axis Length','Area'},['Region' num2str(i)],'A1')
    xlswrite([file2 '.xlsx'],Major,['Region' num2str(i)],'A2')
    xlswrite([file2 '.xlsx'],Minor,['Region' num2str(i)],'B2')
    xlswrite([file2 '.xlsx'],Area,['Region' num2str(i)],'C2')
end
xlswrite([file2 '.xlsx'],{'Major Axis Length','Minor Axis Length','Area'},'Total','A1')
xlswrite([file2 '.xlsx'],major_t./2','Total','A2')
xlswrite([file2 '.xlsx'],minor_t./2','Total','B2')
xlswrite([file2 '.xlsx'],area_t','Total','C2')

 histogram(major_t./2,'Normalization','probability','NumBins',round(length(major_t)^0.5))
    xlabel('Radius (unit)')
    ylabel('Probability')
    title('Edit Title')

这允许我从图片中选择区域并绘制直方图,但是由于我对Matlab相当陌生,所以我现在不知道如何使生成的直方图适合上述函数。我能找到的所有教程都使直方图适合已定义的函数,例如正态分布等。

任何帮助将不胜感激!

0 个答案:

没有答案