⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calcarea.m

📁 这个GUI程序让你测量面积和图像中路径的长度
💻 M
📖 第 1 页 / 共 3 页
字号:




function calibDistance_Callback(hObject, eventdata, handles)
% hObject    handle to calibDistance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calibDistance as text
%        str2double(get(hObject,'String')) returns contents of calibDistance as a double


% --- Executes during object creation, after setting all properties.
function calibDistance_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calibDistance (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in calibrateScale.
function calibrateScale_Callback(hObject, eventdata, handles)
    temp = get(handles.calibDistance,'string');
    dist = str2double( temp );
    if isempty(dist) || size(handles.scaleCalibP,1) < 2
        errorstring = {'Number entered is invalid or Missing points' ; 'You mut click on two points and enter the distance between it'};
        errordlg(errorstring,'ERROR','on')
        return
    end
    x = handles.scaleCalibP(:,1);
    y = handles.scaleCalibP(:,2);
    dx = x(2)-x(1);
    dy = y(2)-y(1);
    A = dist / sqrt( dx^2 + dy^2 );
    temp = get(handles.totalArea,'userdata');
    oldA = temp(1);
    temp(1) = A;
    temp(2) = temp(2) * (A/oldA)^2;
    temp(3) = temp(3) * A/oldA;
    set(handles.totalArea,'userdata',temp);
    hIm = findobj(gca,'type','image');
    im = handles.im;
    [r c ans] = size(im);
    x = (0:c-1) * A;
    y = (0:r-1) * A;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%
    hold off;
    image(x,y,im/max(im(:)),'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
    grid on
    hold on;
%     axis image;
    handles.hScaleCalibP = [];
    handles.scaleCalibP = [];
    handles = resetPoints(handles);
    guidata(hObject,handles);
% hObject    handle to calibrateScale (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in calcEnclosedArea.
function calcEnclosedArea_Callback(hObject, eventdata, handles)
    % --- Executes on button press in calc.
% function calc_Callback(hObject, eventdata, handles)
    f = findobj(handles.axes1,'tag','tempPatch');
    delete(f);
    
    temp = get(handles.totalArea,'userdata');
    A = temp(1);
    patchHandles = [];
    triangleCenters = [];
    points = handles.areaPoints;
    Npoints = size(points,1);
    if Npoints < 3
        message = 'can''t create an area from less then three points :(';
        title = 'error';
        msgbox(message,title,'error')
        return
    end

    %% Building the initial connectionsMatrix (a one symbolize a line between
    %% the two points)
    connectionsMat = or( [zeros(Npoints,1) eye(Npoints,Npoints-1)], [zeros(1,Npoints) ; eye(Npoints-1,Npoints)] );
    connectionsMat(1,Npoints) = 1; connectionsMat(Npoints,1) = 1;
    connectionsMat = int8(connectionsMat);
    
    cross = lineCrossExistingLines(1,Npoints,connectionsMat,handles.areaPoints);
    if cross
        message = 'The last line cross an existing line, correct the shape         (first & last points are connected)';
        title = 'error';
        msgbox(message,title,'error')
        return
    end
                            
    temp = get(handles.areaCalcMethode,'string');
    methode = temp{get(handles.areaCalcMethode,'value')};
    
    switch methode
        case 'Monte-Carlo'
            patchArea = calcAreaMonteCarlo(points,connectionsMat)*A^2;
            if patchArea
                hPatch = patch(A*points(:,1),A*points(:,2), 'r','facealpha',0.51,'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))',...
                    'userdata',patchArea,'tag',['patch ' num2str(handles.run)] );
                handles.hAreaPatches = [handles.hAreaPatches hPatch];
                x = mean(A*points(:,1));    y = mean(A*points(:,2));
                h = text(x,y,num2str(patchArea),'color','k','tag',['patch ' num2str(handles.run)],... 
                    'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
                handles.hAreaPatches = [handles.hAreaPatches h];
            end
        case 'Triangulation'
            triangles = 0;
            addedTriangle = false;
            ind=1;
            pointWorthChecking = ones(Npoints,1);
            while triangles < Npoints-3 && sum(pointWorthChecking)
                % While didn't close all possible triangles (minus one) & there
                % are still points worth checking
                connections = find( connectionsMat(:,ind) );
                for n=1:length(connections)-1
                    for m =n+1:length(connections)
                        N = connections(n);
                        M = connections(m);                
                        if  ~connectionsMat( N, M) %the two are not connected yet
                            fit4triangle = testIfCanCloseATriangle( N,M,ind,connectionsMat,points,triangleCenters);
                            if fit4triangle
                                triangles = triangles+1;
                                pointWorthChecking(N) = 1;
                                pointWorthChecking(M) = 1;
                                h =  patch(A*points([M,N,ind],1),A*points([M,N,ind],2), 'r','tag','tempPatch','facealpha',0.51,...
                                    'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))'); 
                                patchHandles = [patchHandles h];
                                drawnow;
                                addedTriangle = true;
                                triangleCM = [ mean( points([M,N,ind],1) ) mean(points([M,N,ind],2) ) ];
                                triangleCenters = [triangleCenters ; triangleCM];
                                if ~connectionsMat( M,N )
                                    connectionsMat(M,N) = 2;
                                end
                                if ~connectionsMat(N,M)  
                                    connectionsMat(N,M) = 2;
                                end
                            end % if fit
                        end % if n~=curr & m ~=curr

                    end %for m
                end %for n

                pointWorthChecking(ind) = 0;

                loop = 0;
                while ~pointWorthChecking(ind) && loop < Npoints
                    loop = loop+1;
                    ind = ind+1;
                    ind = mod(ind-1,Npoints)+1;
                end

            end % while

            delete(patchHandles);   %removing temporary patches

            patchArea = 0;
            for ind =1 : Npoints
               b = find(connectionsMat(ind,:));
               a = ind;
               f = find(b > a);
                N = length(f);
                b = b(f);
                if N > 1
                    for n=1:N-1
                        for m =n+1:N
                            if connectionsMat(b(n),b(m))
                                trianglePoints = points([ind ,b(m) ,b(n)],:)*A;
                                area = triangleArea( trianglePoints,1);
                                patchArea = patchArea + area;
                                h = patch(trianglePoints(:,1),trianglePoints(:,2), 'r','facealpha',0.51,'EdgeColor','none',...
                                    'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))', 'userdata',area,...
                                    'tag',['patch ' num2str(handles.run)]);
                                handles.hAreaPatches = [handles.hAreaPatches h];
                            end
                        end %for m
                    end % for n
                end %if N >1
            end %for ind
            x = mean(A*points(:,1));
            y = mean(A*points(:,2));
            h = text(x,y,num2str(patchArea),'color','k','tag',['patch ' num2str(handles.run)],...
                'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
            handles.hAreaPatches = [handles.hAreaPatches h];
            
        case 'Matlab (polyArea)'
            temp = get(handles.totalArea,'userdata');
            A = temp(1);
            patchArea = polyarea(points(:,1),points(:,2))*A^2;
            hPatch = patch(A*points(:,1),A*points(:,2), 'r','facealpha',0.51,'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))',...
                'userdata',patchArea,'tag',['patch ' num2str(handles.run)] );
            handles.hAreaPatches = [handles.hAreaPatches hPatch];
            x = mean(A*points(:,1));
            y = mean(A*points(:,2));
            h = text(x,y,num2str(patchArea),'color','k','tag',['patch ' num2str(handles.run)],... 
                'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
            handles.hAreaPatches = [handles.hAreaPatches h];

end % switch methode

    temp = get(handles.totalArea,'userdata');
    totalArea = temp(2);
    totalArea = totalArea + patchArea;
    temp(2) = totalArea;
    set(handles.totalArea,'userdata',temp);    
    
    updateTitle(handles);
    
    handles.areaPoints = [];
    delete(handles.hAreaPoints);
    handles.hAreaPoints = [];
    delete(handles.hAreaLines);
    handles.hAreaLines = [];
    handles.run = handles.run +1;
    
    guidata(hObject,handles);       
    
    % neads to calculate the total area acording to all the triangles.
                

% hObject    handle to calcEnclosedArea (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on selection change in areaCalcMethode.
function areaCalcMethode_Callback(hObject, eventdata, handles)
% hObject    handle to areaCalcMethode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns areaCalcMethode contents as cell array
%        contents{get(hObject,'Value')} returns selected item from areaCalcMethode


% --- Executes during object creation, after setting all properties.
function areaCalcMethode_CreateFcn(hObject, eventdata, handles)
% hObject    handle to areaCalcMethode (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


function handles = resetPoints(handles)
    % Area measurment data
    handles.areaPoints = [];
    handles.hAreaPoints = [];
    handles.hAreaLines = [];
    handles.hAreaPatches = [];

    % scale calibration data
    handles.scaleCalibP = [];
    handles.hScaleCalibP = [];

    % length measures data
    handles.lengthPoints = [];
    handles.lengthLines = [];
    handles.hLengthPointsPlot = [];
    handles.hLengthData = [];
    
    temp = get(handles.totalArea,'userdata');
    temp(2) = 0;    temp(3) = 0;
    set(handles.totalArea,'userData',temp);
    mode_Callback(handles.mode, [], handles)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -