📄 calcarea.m
字号:
function varargout = calcEnclosedArea(varargin)
% calcArea M-file for calcArea.fig
% calcArea, by itself, creates a new calcArea or raises the existing
% singleton*.
%
% H = calcArea returns the handle to a new calcArea or the handle to
% the existing singleton*.
%
% calcArea('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in calcArea.M with the given input arguments.
%
% calcArea('Property','Value',...) creates a new calcArea or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before calcArea_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to calcArea_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help calcArea
% Last Modified by GUIDE v2.5 03-Dec-2006 21:02:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @calcArea_OpeningFcn, ...
'gui_OutputFcn', @calcArea_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before calcArea is made visible.
function calcArea_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to calcArea (see VARARGIN)
% Choose default command line output for calcArea
handles.output = hObject;
handles.im = [];
% handles = resetPoints(handles);
handles.run = 0;
% a function handle for distance between two points
dist = @(a,b) sqrt( sum( (a-b).^2 ) );
%% Update handles structure
guidata(hObject, handles);
reset_Callback(hObject, eventdata, handles)
% UIWAIT makes calcArea wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = calcArea_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in loadImage.
function loadImage_Callback(hObject, eventdata, handles)
[fName folder ] = uigetfile('*.jpg;*.tif,*.bmp');
if fName
cd(folder);
im = double( imread(fName) );
handles.im = im;
reset_Callback(hObject, eventdata, handles);
guidata(hObject, handles);
end
% hObject handle to loadImage (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 reset.
function reset_Callback(hObject, eventdata, handles)
temp = [1 0 0];
set(handles.totalArea,'userdata',temp);
A = temp(1);
handles = resetPoints(handles);
hold off;
im = handles.im;
[r c ans] = size(im);
%%%%
%% in the future will support an option not to reset the scale
%%%%
x = (0:c-1) * A;
y = (0:r-1) * A;
if ~isempty(im)
image(x,y,im/max(im(:)),'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
else
image([],'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
end
grid on
hold on;
guidata(handles.figure1, handles);
% hObject handle to reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
temp = get(handles.totalArea,'userdata');
A = temp(1);
temp = get(gca,'CurrentPoint');
x = temp(1,1)/A; y=temp(1,2)/A;
mouseButton = get(gcf,'SelectionType');
switch mouseButton
case 'normal'
% adding a point
switch get(handles.mode,'value')
case 1 % calibrate scale
hPlot = plot(x*A,y*A,'b+','markersize',10,'Linewidth',2,'tag','point');
if size(handles.scaleCalibP,1) > 1
handles.scaleCalibP(2,:) = [x y];
if ishandle(handles.hScaleCalibP(2))
delete(handles.hScaleCalibP(2));
end
handles.hScaleCalibP(2) = (hPlot);
else
handles.scaleCalibP = [handles.scaleCalibP ; x y];
handles.hScaleCalibP = [handles.hScaleCalibP hPlot];
end
guidata(hObject,handles);
case 2 % measure area
handles.areaPoints = [handles.areaPoints ; x y];
hPlot = plot(x*A,y*A,'r.','markersize',14,'tag','point');
handles.hAreaPoints = [handles.hAreaPoints ; hPlot];
n = length(handles.hAreaPoints);
if n > 1
%% this point creates a line with the previous point
xx = handles.areaPoints([n-1 n],1); yy = handles.areaPoints([n-1 n],2);
%% verifying that the new line doesn't cross any existing line
connectionsMat = [zeros(n,1) eye(n,n-1)];
cross = lineCrossExistingLines(n-1,n,connectionsMat,handles.areaPoints);
if cross
message = 'Line entered croses an existing line, point disregarded';
title = 'error';
msgbox(message,title,'error')
handles.areaPoints = handles.areaPoints(1:end-1,:);
delete(handles.hAreaPoints(end));
handles.hAreaPoints = handles.hAreaPoints(1:end-1,:);
else
hLine = plot(A*xx,A*yy, 'r', 'linewidth',2, 'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
handles.hAreaLines = [handles.hAreaLines hLine];
end
end
guidata(hObject,handles);
case 3 % measure length
handles.lengthPoints = [handles.lengthPoints ; x y];
hPlot = plot(x*A,y*A,'g.','markersize',14,'tag','point');
handles.hLengthPointsPlot = [handles.hLengthPointsPlot ; hPlot];
if size(handles.lengthPoints,1) > 1
points = A*handles.lengthPoints([end-1 end],:);
hLine = plot(points(:,1), points(:,2), 'g', 'linewidth',2, 'ButtonDownFcn','calcArea(''axes1_ButtonDownFcn'',gca,[],guidata(gcbo))');
handles.lengthLines = [handles.lengthLines hLine];
dist = sqrt( sum((diff(points).^2)) );
orientation = angle( diff(points) * [1 ; i] );
h = text(mean( points(:,1) ), mean( points(:,2) ) , num2str(dist), 'Rotation', orientation);
handles.hLengthData = [handles.hLengthData h];
temp = get(handles.totalArea,'userdata');
temp(3) = temp(3) + dist;
set(handles.totalArea,'userdata',temp);
updateTitle(handles);
end
guidata(hObject,handles);
end
case 'alt'
switch get(handles.mode,'value')
case 1 % calibrate scale
if ~isempty(handles.hScaleCalibP)
if ishandle(handles.hScaleCalibP(end))
delete(handles.hScaleCalibP(end));
end
handles.hScaleCalibP = handles.hScaleCalibP(1:end-1);
handles.scaleCalibP = handles.scaleCalibP(1:end-1,:);
end
guidata(hObject,handles);
case 2 % measure area
h = gco;
if strcmp( get(h,'type'), 'patch') || strcmp( get(h,'type'), 'text')
button = questdlg('Remove entire shape?','','Yes','No','Yes');
if strcmp( button, 'Yes')
tag = get(h,'tag');
h = findobj(handles.axes1,'tag',tag);
end
removeHandles= zeros(size(h));
for nn =1 : length(handles.hAreaPatches)
for mm=1 : length(h)
if handles.hAreaPatches(nn) == h(mm)
removeHandles(mm) = nn;
end
end
end
handles.hAreaPatches(removeHandles) = [];
deletedArea = get(h,'userdata');
if iscell(deletedArea)
deletedArea = sum(cell2mat(deletedArea) );
end
delete(h);
temp = get(handles.totalArea,'userdata');
totalArea = temp(2);
totalArea = totalArea - deletedArea;
temp(2) = totalArea;
set(handles.totalArea,'userData',temp);
str = ['Total Area is = ' num2str(temp(2)) ];
set(handles.totalArea,'string',str);
guidata(hObject,handles);
return
end
n = length(handles.hAreaPoints);
if n > 1
delete(handles.hAreaLines(end));
handles.hAreaLines = handles.hAreaLines(1:end-1);
end
if n > 0
handles.areaPoints = handles.areaPoints(1:end-1,:);
delete(handles.hAreaPoints(end));
handles.hAreaPoints = handles.hAreaPoints(1:end-1,:);
end
guidata(hObject,handles);
case 3
% length measuring mode
if size(handles.lengthPoints,1) > 0
delete(handles.hLengthPointsPlot(end));
handles.hLengthPointsPlot = handles.hLengthPointsPlot(1:end-1);
handles.lengthPoints = handles.lengthPoints(1:end-1,:);
if length(handles.lengthLines) > 0
dist = str2double( get(handles.hLengthData(end),'string') );
delete(handles.lengthLines(end));
handles.lengthLines = handles.lengthLines(1:end-1);
delete(handles.hLengthData(end));
handles.hLengthData = handles.hLengthData(1:end-1);
temp = get(handles.totalArea,'userdata');
temp(3) = temp(3) - dist;
set(handles.totalArea,'userdata',temp);
updateTitle(handles);
end
end
guidata(hObject,handles);
end
end
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function area=triangleArea(cord,A)
cord = [cord ; cord(1,:)];
dcord = diff(cord);
points = zeros(3,1);
for n=1:3
points(n) = sqrt(sum(dcord(n,:).^2));
end
points=points*A;
s = sum(points)/2;
area = sqrt(s*(s-points(1))*(s-points(2))*(s-points(3)));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -