加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

MATLAB提取图像中的数据

(2015-07-06 15:06:05)
标签:

股票

分类: Matlab
前几天看到一篇日志关于如何利用MATLAB编写GUI函数,实现从图像中提取数据,便认真学习了一番。(原地址:http://shanyunh.blog.163.com/blog/static/78878720115963541567/)由于本人属于菜鸟级,实在不懂作者所给代码应该放在哪一个callback函数中,于是通过查各种资料,复现了该程序,对于GUI有了更好的学习和理解,非常感谢作者的贡献,我现将这个程序的过程给出详细的解释和说明,希望对像我一样的菜鸟们有所帮助。

具体步骤:
1. 在MATLAB新建菜单中,选择Graphical User Interface,再选Blank GUI(Default)选项,按照下图建立用户界面。
其中:edit控件的tag分别为:fileEdit(用于选择文件目录),countEdit(用于显示当前所取点的数目,12为设定的默认值,可为任意值或不要也可以),xmin,xmax,ymin,ymax(用于设定坐标轴起始值);
pushbutton控件的tag依次为:pushbutton1,pushbutton2,ZoomPB,LinePB,change,save,功能显而易见;
Popupmenu控件tag为:xTypePM,yTypePM
坐标系控件tag为默认的axes1.
2. 创建好上述界面后点击保存,会自动弹出.m文件,寻找适当位置添加一下代码即可。
function varargout = test(varargin)
%TEST M-file for test.fig
%      TEST, by itself, creates a new TEST or raises the existing
%      singleton*.
%
%      H = TEST returns the handle to a new TEST or the handle to
%      the existing singleton*.
%
%      TEST('Property','Value',...) creates a new TEST using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to test_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      TEST('CALLBACK') and TEST('CALLBACK',hObject,...) call the
%      local function named CALLBACK in TEST.M with the given input
%      arguments.
%
%      *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 test

% Last Modified by GUIDE v2.5 06-Jul-2015 09:18:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @test_OpeningFcn, ...
                   'gui_OutputFcn',  @test_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 test is made visible.
function test_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

handles.dataCount=0;
handles.linedata=[];

handles.axesCount=0;
handles.axesdata=[];

handles.optMode='pickaxes';

handles.datah=[];
handles.axesh=[];
handles.newdata=[];

himage=findobj('tag','axes1');

axes(himage);
% Choose default command line output for test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = test_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;


%--------------------------------------------------------------------------
%*******************************  Step1 ***********************************    
% Select the figure you want to deal with.
% Then import it.
%--------------------------------------------------------------------------
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path]=uigetfile({'*.jpg','图片文件(*.jpg)';'*.*','所有文件'},'请选择待分析图片...');

if ~isequal(file, 0)

cd(path);

set(handles.fileEdit,'String',[path file]);

end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.dataCount=0;
handles.axesCount=0; 

image_temp=imread(get(handles.fileEdit,'String'));

imshow(image_temp); %read and show

 handles.ximage=size(image_temp,2);

 handles.yimage=size(image_temp,1);

 guidata(hObject,handles);

% 更新状态栏

 h=findobj('tag','countEdit');

 set(h,'String',num2str(handles.dataCount));
 

%--------------------------------------------------------------------------
%******************************* step 2 ***********************************  
% Select your option: pick data of axes or curve
% Then click on the figure and press down 'space' to save it
% Notes: the axes needs three dots
%        the curve needs six dots at least
%--------------------------------------------------------------------------
% --- Executes on button press in ZoomPB.
function ZoomPB_Callback(hObject, eventdata, handles)
% hObject    handle to ZoomPB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.optMode='pickaxes';
 guidata(hObject,handles);



% --- Executes on button press in LinePB.
function LinePB_Callback(hObject, eventdata, handles)
% hObject    handle to LinePB (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.optMode='pickdata';
 guidata(hObject,handles);





% --- Executes on key press with focus on figure1 or any of its controls.
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  structure with the following fields (see FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)
p=get(gca,'CurrentPoint');

switch handles.optMode

case ('pickdata')

if p(end,1)>0&&p(end,1)0&&p(end,2)

handles.dataCount=handles.dataCount+1;

handles.linedata(handles.dataCount,:)=p(end,1:2);

set(handles.countEdit,'String',num2str(handles.dataCount));

hold on;

handles.datah(handles.dataCount,1)=plot(p(end,1),p(end,2),'b*');

guidata(hObject, handles);

end

case ('pickaxes')

if p(end,1)>0&&p(end,1)0&&p(end,2)<3 

handles.axesCount=handles.axesCount+1;

handles.axesdata(handles.axesCount,:)=p(end,1:2);

set(handles.countEdit,'String',num2str(handles.axesCount));

hold on;

handles.axesh(handles.axesCount,1)=plot(p(end,1),p(end,2),'r+');

guidata(hObject, handles);

end

end
% switch get(gcf,'CurrentKey') 
% case('space') 
% PickDataPB_Callback(hObject, eventdata, handles); 
% case('z') 
% ZoomPB_Callback(hObject, eventdata, handles); 
% case('m') 
% PanPB_Callback(hObject, eventdata, handles); 
% case('delete') 
% switch handles.optMode 
% case ('pickdata') 
% if(handles.dataCount>0) 
% delete(handles.datah(handles.dataCount,1)); 
% handles.dataCount=handles.dataCount-1; 
% set(handles.countEdit,'String',num2str(handles.dataCount)); 
% guidata(hObject, handles); 
% end 
% case ('pickaxes') 
% if(handles.axesCount>0) 
% delete(handles.axesh(handles.axesCount,1)); 
% handles.axesCount=handles.axesCount-1; 
% set(handles.countEdit,'String',num2str(handles.axesCount)); 
% guidata(hObject, handles); 
% end 
% end 
% end


%--------------------------------------------------------------------------
%*******************************  Step 3 **********************************
% Press down change to tackle your data and draw your figure
%--------------------------------------------------------------------------
% --- Executes on button press in change.
function change_Callback(hObject, eventdata, handles)
% hObject    handle to change (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
xmin=str2double(get(handles.xmin,'String'));

xmax=str2double(get(handles.xmax,'String'));

ymin=str2double(get(handles.ymin,'String'));

ymax=str2double(get(handles.ymax,'String'));

xType=get(handles.xTypePM,'value');
yType=get(handles.yTypePM,'value');

%数据分类整理

tmpaxes=handles.axesdata;

ct=6;

[c,r]=max(tmpaxes(:,1));

axesxP=tmpaxes(r,:);

ct=ct-r;

[c,r]=min(tmpaxes(:,2));

axesyP=tmpaxes(r,:);

ct=ct-r;

originP=handles.axesdata(ct,:);

Data=zeros(handles.dataCount,2);

data_new=zeros(handles.dataCount,2);

%起点归零和坐标调整

for i=1:handles.dataCount

Data(i,2)=det([originP-axesxP;handles.linedata(i,:)-axesxP])/norm(originP-axesxP);

Data(i,1)=-det([originP-axesyP;handles.linedata(i,:)-axesyP])/norm(originP-axesyP);

end

switch xType
    case 1
        data_new(:,1)=Data(:,1)/pdist([originP;axesxP]); %坐标归一化
        data_new(:,1)=xmin+data_new(:,1)*(xmax-xmin); %数据还原
    case 2
        data_new(:,1)=Data(:,1)/pdist([originP;axesxP]); %坐标归一化
        data_new(:,1)=10.^(log10(xmin)+data_new(:,1)*(log10(xmax)-log10(xmin)));
end
switch yType
    case 1
        data_new(:,2)=Data(:,2)/pdist([originP;axesyP]);
        data_new(:,2)=ymin+data_new(:,2)*(ymax-ymin);
    case 2'
        data_new(:,2)=Data(:,2)/pdist([originP;axesyP]); %坐标归一化
        data_new(:,2)=10.^(log10(ymin)+data_new(:,2)*(log10(ymax)-log10(ymin))); %数据还原
end

figure(3);
if xType==2
    if yType==2
        loglog(data_new(:,1),data_new(:,2),'b*');
    else
        semilogx(data_new(:,1),data_new(:,2),'b*');
    end
else
    if yType==2
        semilogy(data_new(:,1),data_new(:,2),'b*');
    else
        plot(data_new(:,1),data_new(:,2),'b*');
    end
end

handles.newdata=data_new;

guidata(hObject,handles);




%--------------------------------------------------------------------------
%*******************************  Step 4 **********************************
% Press down "save" to save your data in a txt file.
%--------------------------------------------------------------------------
% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[file,path]=uiputfile({'*.txt','文本文件(*.txt)';'*.*','所有文件'},'请选择保存文件...');

if ~isequal(file, 0)
    idata=handles.newdata;
save([path file],'idata','-ascii');

end


%--------------------------------------------------------------------------
%*************************  unused functions ******************************                
%--------------------------------------------------------------------------
function fileEdit_Callback(hObject, eventdata, handles)
% hObject    handle to fileEdit (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 fileEdit as text
%        str2double(get(hObject,'String')) returns contents of fileEdit as a double


% --- Executes during object creation, after setting all properties.
function fileEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fileEdit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function countEdit_Callback(hObject, eventdata, handles)
% hObject    handle to countEdit (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 countEdit as text
%        str2double(get(hObject,'String')) returns contents of countEdit as a double


% --- Executes during object creation, after setting all properties.
function countEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to countEdit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function xmin_Callback(hObject, eventdata, handles)
% hObject    handle to xmin (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 xmin as text
%        str2double(get(hObject,'String')) returns contents of xmin as a double


% --- Executes during object creation, after setting all properties.
function xmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xmin (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function xmax_Callback(hObject, eventdata, handles)
% hObject    handle to xmax (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 xmax as text
%        str2double(get(hObject,'String')) returns contents of xmax as a double


% --- Executes during object creation, after setting all properties.
function xmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xmax (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ymin_Callback(hObject, eventdata, handles)
% hObject    handle to ymin (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 ymin as text
%        str2double(get(hObject,'String')) returns contents of ymin as a double


% --- Executes during object creation, after setting all properties.
function ymin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ymin (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ymax_Callback(hObject, eventdata, handles)
% hObject    handle to ymax (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 ymax as text
%        str2double(get(hObject,'String')) returns contents of ymax as a double


% --- Executes during object creation, after setting all properties.
function ymax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ymax (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

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


% --- Executes during object creation, after setting all properties.
function xTypePM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xTypePM (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

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


% --- Executes during object creation, after setting all properties.
function yTypePM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yTypePM (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
3. 完成上述两个步骤就大功告成了,点击.m 文件或者fig窗口上的运行按钮,即可运行本程序。如下图:
http://s1/mw690/0049SVn1zy6TCLabytq10&690
4. 接下来就可以处理你的图片了,操作步骤:
a. 单击.......按钮弹出如下窗口
选择你的图片如 test文件,单击打开,再单击import按钮,即可将图片显示在axes1的位置。
b. 单击坐标轴按钮,确定坐标轴(需确定三个点,在图上单击鼠标后按空格键,即可显示红色标线),再选取图上的数据(点击曲线数据,在图上取点,此时用蓝色星号标出选定的点),如下图:
http://s16/mw690/0049SVn1zy6TCLJr8VV3f&690
c. 根据你的图片选取坐标系是线性的还是对数的,并输入相应的纵横坐标。
d. 再点击变换按钮,可得到新的figure
http://s2/mw690/0049SVn1zy6TCLWqTo5a1&690
至此,数据已提取结束
e. 单击保存按钮,选择txt文件,保存数据即可。
最后再利用MATLAB曲线拟合的功能处理这些数据,即可得到更为准确的曲线数据。


注:由于时间紧张,本程序中没有实现坐标系缩放、平移,以及数据删除的功能,请酌情参考。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有