当前位置:   article > 正文

【Matlab学习手记】利用Matlab提取图片曲线数据_对mat lab 图片取数据ginput

对mat lab 图片取数据ginput

2021-6-25 新增 github 源码链接,所以,自行下载,enjoy

https://github.com/AFei19911012/MatlabSamples/tree/master/AppDesigner/SimpleImageToData

目的:提取图片上的曲线数据点。比如,文献中的图表数据。

2021-4-24 更新,代码已修改为 AppDesigner 设计,增加了自动提取功能:https://zhuanlan.zhihu.com/p/347520891

  •  源代码
  1. function varargout = DataExtract(varargin)
  2. % DATAEXTRACT MATLAB code for DataExtract.fig
  3. % DATAEXTRACT, by itself, creates a new DATAEXTRACT or raises the existing
  4. % singleton*.
  5. %
  6. % H = DATAEXTRACT returns the handle to a new DATAEXTRACT or the handle to
  7. % the existing singleton*.
  8. %
  9. % DATAEXTRACT('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in DATAEXTRACT.M with the given input arguments.
  11. %
  12. % DATAEXTRACT('Property','Value',...) creates a new DATAEXTRACT or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before DataExtract_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to DataExtract_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help DataExtract
  23. % Last Modified by GUIDE v2.5 30-Aug-2017 09:01:43
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @DataExtract_OpeningFcn, ...
  29. 'gui_OutputFcn', @DataExtract_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before DataExtract is made visible.
  42. function DataExtract_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to DataExtract (see VARARGIN)
  48. % Choose default command line output for DataExtract
  49. handles.output = hObject;
  50. % Update handles structure
  51. guidata(hObject, handles);
  52. % UIWAIT makes DataExtract wait for user response (see UIRESUME)
  53. % uiwait(handles.figure1);
  54. % --- Outputs from this function are returned to the command line.
  55. function varargout = DataExtract_OutputFcn(hObject, eventdata, handles)
  56. % varargout cell array for returning output args (see VARARGOUT);
  57. % hObject handle to figure
  58. % eventdata reserved - to be defined in a future version of MATLAB
  59. % handles structure with handles and user data (see GUIDATA)
  60. % Get default command line output from handles structure
  61. varargout{1} = handles.output;
  62. function pushbutton1_Callback(hObject, eventdata, handles)
  63. axes(handles.axes1)
  64. cla reset
  65. axis off
  66. set(handles.uitable1, 'data', []);
  67. set(handles.text5, 'string', 'Load a picture !!!');
  68. [FileName, PathName] = uigetfile({'*.bmp'}, 'Load a picture');
  69. if length(PathName) < 3
  70. msgbox('Failed to load a picture !!!');
  71. return;
  72. end
  73. FilePath = [PathName, FileName];
  74. Img = imread(FilePath);
  75. axes(handles.axes1);
  76. imshow(Img, 'Border', 'tight');
  77. set(handles.text5, 'string', 'Input the values in EditBoxs, press Enter to continue !!!');
  78. pause();
  79. set(handles.text5, 'string', 'Locate the start point !!!');
  80. currPt = ginput(1);
  81. x0 = currPt(1, 1);
  82. y0 = currPt(1, 2);
  83. line(x0, y0, 'marker', '.', 'color', 'r', 'markersize', 20);
  84. set(handles.text5, 'string', 'Locate the x-end point !!!');
  85. currPt = ginput(1);
  86. xend = currPt(1, 1);
  87. y = currPt(1, 2);
  88. line(xend, y, 'marker', '.', 'color', 'r', 'markersize', 20);
  89. line([x0, xend], [y0, y], 'color', 'r');
  90. set(handles.text5, 'string', 'Locate the y-end point !!!');
  91. currPt = ginput(1);
  92. x = currPt(1, 1);
  93. yend = currPt(1, 2);
  94. line(x, yend, 'marker', '.', 'color', 'r', 'markersize', 20);
  95. line([x0, x], [y0, yend], 'color', 'r');
  96. set(handles.text5, 'string', 'Now you can locate the point, press RightButton to locate the last point !!!');
  97. tableData = cell(0);
  98. con = 1;
  99. i = 1;
  100. xmin = str2double(get(handles.edit1, 'string'));
  101. xmax = str2double(get(handles.edit2, 'string'));
  102. ymin = str2double(get(handles.edit3, 'string'));
  103. ymax = str2double(get(handles.edit4, 'string'));
  104. scale1 = get(handles.radiobutton1, 'value');
  105. scale2 = get(handles.radiobutton3, 'value');
  106. while con == 1
  107. [x, y, con] = ginput(1);
  108. line(x, y, 'marker', '.', 'color', 'r', 'markersize', 20);
  109. if scale1 == 1
  110. xx = (xmax - xmin)*(x - x0)/(xend - x0) + xmin;
  111. else
  112. xx = 10^((log10(xmax) - log10(xmin))*(x - x0)/(xend - x0) + log10(xmin));
  113. end
  114. if scale2 == 1
  115. yy = (ymax - ymin)*(y - y0)/(yend - y0) + ymin;
  116. else
  117. %yy = 10^((log10(ymax) - log10(ymin))*(log10(y) - log10(y0))/(log10(yend) - log10(y0)) + log10(ymin));
  118. yy = 10^((log10(ymax) - log10(ymin))*(y - y0)/(yend - y0) + log10(ymin));
  119. end
  120. tableData{i, 1} = i;
  121. tableData{i, 2} = xx;
  122. tableData{i, 3} = yy;
  123. set(handles.uitable1, 'data', tableData);
  124. i = i + 1;
  125. end
  126. function pushbutton2_Callback(hObject, eventdata, handles)
  127. [FileName, PathName] = uiputfile('*.txt', 'save file name');
  128. tableData = get(handles.uitable1, 'data');
  129. tableData = cell2mat(tableData);
  130. if ~isempty(tableData)
  131. dlmwrite([PathName, FileName], tableData(:, 2:end), 'delimiter', '\t', 'precision', '%8.4f');
  132. end
  133. function pushbutton3_Callback(hObject, eventdata, handles)
  134. close(gcf);
  135. function edit1_Callback(hObject, eventdata, handles)
  136. function edit1_CreateFcn(hObject, eventdata, handles)
  137. % Hint: edit controls usually have a white background on Windows.
  138. % See ISPC and COMPUTER.
  139. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  140. set(hObject,'BackgroundColor','white');
  141. end
  142. function edit2_Callback(hObject, eventdata, handles)
  143. function edit2_CreateFcn(hObject, eventdata, handles)
  144. % Hint: edit controls usually have a white background on Windows.
  145. % See ISPC and COMPUTER.
  146. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  147. set(hObject,'BackgroundColor','white');
  148. end
  149. function edit3_Callback(hObject, eventdata, handles)
  150. function edit3_CreateFcn(hObject, eventdata, handles)
  151. % Hint: edit controls usually have a white background on Windows.
  152. % See ISPC and COMPUTER.
  153. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  154. set(hObject,'BackgroundColor','white');
  155. end
  156. function edit4_Callback(hObject, eventdata, handles)
  157. function edit4_CreateFcn(hObject, eventdata, handles)
  158. % Hint: edit controls usually have a white background on Windows.
  159. % See ISPC and COMPUTER.
  160. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  161. set(hObject,'BackgroundColor','white');
  162. end
  • 源代码链接

https://download.csdn.net/download/u012366767/10593012

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/412823
推荐阅读
相关标签
  

闽ICP备14008679号