1 function contdemo(action,varargin);
\r
2 % OpenCV contour functions demo
\r
5 demoName = 'OpenCV Contour Demo';
\r
7 action='InitializeDEMO';
\r
10 feval(action,varargin{:});
\r
15 %%% Sub-function - InitializeDEMO
\r
18 function InitializeDEMO()
\r
22 % If demo is already running, bring it to the foreground.
\r
23 h = findobj(allchild(0), 'tag', demoName);
\r
29 screenD = get(0, 'ScreenDepth');
\r
37 'Name',demoName, ...
\r
38 'NumberTitle','off', 'HandleVisibility', 'on', ...
\r
39 'tag', demoName, ...
\r
40 'Visible','off', 'Resize', 'off',...
\r
41 'BusyAction','Queue','Interruptible','off',...
\r
42 'IntegerHandle', 'off', ...
\r
43 'Doublebuffer', 'on', ...
\r
44 'Colormap', gray(grayres));
\r
46 %====================================
\r
47 % Information for all buttons (and menus)
\r
51 %====================================
\r
56 frmLeft = 0.5-btnWid-1.5*frmBorder;
\r
57 frmWidth = 2*btnWid+3*frmBorder;
\r
58 frmHeight = btnHt+2*frmBorder;
\r
59 frmPos=[frmLeft frmBottom frmWidth frmHeight];
\r
61 'Parent', DemoFig, ...
\r
62 'BusyAction','Queue','Interruptible','off',...
\r
63 'Style','frame', ...
\r
64 'Units','normalized', ...
\r
65 'Position',frmPos, ...
\r
66 'BackgroundColor',[0.45 0.45 0.45]);
\r
68 %====================================
\r
71 callbackStr='contdemo(''Apply'')';
\r
72 yPos=frmBottom+frmBorder;
\r
73 applyHndl=uicontrol( ...
\r
74 'Parent', DemoFig, ...
\r
75 'BusyAction','Queue','Interruptible','off',...
\r
76 'Style','pushbutton', ...
\r
77 'Units','normalized', ...
\r
78 'Position',[frmLeft+frmBorder yPos btnWid btnHt], ...
\r
79 'String',labelStr, ...
\r
80 'Enable', 'off', ...
\r
81 'Callback',callbackStr);
\r
84 %====================================
\r
87 callbackStr='close(gcf)';
\r
89 closeHndl=uicontrol( ...
\r
90 'Parent', DemoFig, ...
\r
91 'BusyAction','Queue','Interruptible','off',...
\r
92 'Style','pushbutton', ...
\r
93 'Units','normalized', ...
\r
94 'Position',[frmLeft+btnWid+2*frmBorder yPos btnWid btnHt], ...
\r
95 'Enable', 'off', ...
\r
96 'String',labelStr, ...
\r
97 'Callback',callbackStr);
\r
99 c = get(DemoFig,'Color');
\r
100 if [.298936021 .58704307445 .114020904255]*c'<.5,
\r
106 %==================================
\r
107 % Set up the image axes
\r
108 figpos = get(DemoFig, 'position');
\r
109 row = figpos(4); col = figpos(3); % dimensions of figure window
\r
111 vertSpac = (row-256)/3;
\r
112 horSpac = (col-3*128)/4;
\r
113 hSrcAx = axes('Parent', DemoFig, ...
\r
114 'units', 'pixels', ...
\r
115 'BusyAction','Queue','Interruptible','off',...
\r
116 'ydir', 'reverse', ...
\r
117 'XLim', [.5 128.5], ...
\r
118 'YLim', [.5 128.5],...
\r
119 'CLim', [0 255], ...
\r
120 'XTick',[],'YTick',[], ...
\r
121 'Position', [horSpac row-vertSpac-128 128 128]);
\r
122 title('Source Image');
\r
124 hBinAx = axes('Parent', DemoFig, ...
\r
125 'units', 'pixels', ...
\r
126 'BusyAction','Queue', 'Interruptible','off',...
\r
127 'ydir', 'reverse', ...
\r
128 'XLim', [.5 128.5], ...
\r
129 'YLim', [.5 128.5],...
\r
130 'CLim', [0 255], ...
\r
131 'XTick',[],'YTick',[], ...
\r
132 'Position', [2*horSpac + 128 row - vertSpac-128 128 128]);
\r
133 title('Threshold Image');
\r
135 hContAx = axes('Parent', DemoFig, ...
\r
136 'units', 'pixels', ...
\r
137 'BusyAction','Queue', 'Interruptible','off',...
\r
138 'ydir', 'reverse', ...
\r
139 'XLim', [.5 128.5], ...
\r
140 'YLim', [.5 128.5],...
\r
141 'CLim', [0 255], ...
\r
142 'XTick',[],'YTick',[], ...
\r
143 'Position', [3*horSpac + 256 row - vertSpac-128 128 128]);
\r
144 title('Contour Image');
\r
146 %==================================
\r
147 % Set up the images
\r
148 blank = repmat(uint8(0),128,128);
\r
149 hSrcImage = image('Parent', hSrcAx,...
\r
150 'CData', blank, ...
\r
151 'BusyAction','Queue','Interruptible','off',...
\r
152 'CDataMapping', 'scaled', ...
\r
153 'Xdata', [1 128],...
\r
154 'Ydata', [1 128],...
\r
155 'EraseMode', 'none');
\r
157 hBinImage = image('Parent', hBinAx,...
\r
158 'CData', blank, ...
\r
159 'BusyAction','Queue','Interruptible','off',...
\r
160 'CDataMapping', 'scaled', ...
\r
161 'Xdata', [1 128],...
\r
162 'Ydata', [1 128],...
\r
163 'EraseMode', 'none');
\r
165 hContImage = image('Parent', hContAx,...
\r
166 'CData', blank, ...
\r
167 'BusyAction','Queue','Interruptible','off',...
\r
168 'CDataMapping', 'scaled', ...
\r
169 'Xdata', [1 128],...
\r
170 'Ydata', [1 128],...
\r
171 'EraseMode', 'none');
\r
174 % rangePos = [64 3 280 15];
\r
175 rangePos = [0 .01 1 .05];
\r
176 hStatus = uicontrol( ...
\r
177 'Parent', DemoFig, ...
\r
178 'BusyAction','Queue','Interruptible','off',...
\r
179 'Style','text', ...
\r
180 'Units','normalized', ...
\r
181 'Position',rangePos, ...
\r
182 'Horiz','center', ...
\r
183 'Background',c, ...
\r
184 'Foreground',[.8 0 0], ...
\r
185 'Tag', 'Status', ...
\r
186 'String','Status bar');
\r
188 %====================================
\r
189 % The Threshold Popup
\r
190 ctrHt = 19; % controls' height
\r
191 binImgPos = get(hBinAx, 'Position');
\r
192 pos = [binImgPos(1) binImgPos(2) - 3*ctrHt binImgPos(3) ctrHt];
\r
193 hThreshPop=uicontrol( ...
\r
194 'Parent', DemoFig, ...
\r
195 'BusyAction','Queue','Interruptible','off',...
\r
196 'Style','popupmenu', ...
\r
197 'BackgroundColor',[.8 .8 .8], ...
\r
198 'Units','pixels', ...
\r
199 'Position',pos, ...
\r
200 'String','binary|binary_inv|trunc|tozero|tozero_inv', ...
\r
201 'Tag','ThreshPop', ...
\r
202 'Callback','contdemo(''ControlsUpdate'')');
\r
203 %====================================
\r
204 % The Threshold Label
\r
205 pos = [binImgPos(1) binImgPos(2) - 2*ctrHt binImgPos(3) ctrHt];
\r
207 'Parent', DemoFig, ...
\r
208 'BusyAction','Queue','Interruptible','off',...
\r
209 'Style','text', ...
\r
210 'Units','pixels', ...
\r
211 'Position',pos, ...
\r
212 'Horiz','center', ...
\r
213 'Background',c, ...
\r
214 'Foreground','black', ...
\r
215 'String','Threshold Type:');
\r
217 %===================================
\r
218 % Set up scroll bar
\r
221 pos = [binImgPos(1) binImgPos(2) - 5*ctrHt binImgPos(3) ctrHt];
\r
222 callbackStr = 'contdemo(''ControlsUpdate'')';
\r
223 hSlider = uicontrol( ...
\r
224 'Parent', DemoFig, ...
\r
225 'BusyAction','Queue','Interruptible','off',...
\r
226 'Style','slider', ...
\r
227 'Units','pixels', ...
\r
228 'Position',pos, ...
\r
229 'Value',thresh, ...
\r
232 'Interruptible','off', ...
\r
233 'Callback',callbackStr);
\r
235 % Left and right range indicators
\r
236 pos = [binImgPos(1) binImgPos(2) - 6*ctrHt binImgPos(3)/2 ctrHt];
\r
238 'Parent', DemoFig, ...
\r
239 'BusyAction','Queue','Interruptible','off',...
\r
240 'Style','text', ...
\r
241 'Units','pixels', ...
\r
242 'Position',pos, ...
\r
243 'Horiz','left', ...
\r
244 'Background',c, ...
\r
245 'Foreground',fgColor, ...
\r
248 pos = [binImgPos(1)+binImgPos(3)/2 binImgPos(2) - 6*ctrHt binImgPos(3)/2 ctrHt];
\r
250 'Parent', DemoFig, ...
\r
251 'BusyAction','Queue','Interruptible','off',...
\r
252 'Style','text', ...
\r
253 'Units','pixels', ...
\r
254 'Position',pos, ...
\r
255 'Horiz','right', ...
\r
256 'Background',c, ...
\r
257 'Foreground',fgColor, ...
\r
260 pos = [binImgPos(1) binImgPos(2) - 4*ctrHt binImgPos(3) ctrHt];
\r
261 hThresh = uicontrol( ...
\r
262 'Parent', DemoFig, ...
\r
263 'BusyAction','Queue','Interruptible','off',...
\r
264 'Style','text', ...
\r
265 'Units','pixels', ...
\r
266 'Position',pos, ...
\r
267 'Horiz','center', ...
\r
268 'Background',c, ...
\r
269 'Foreground',fgColor, ...
\r
270 'String',['Threshold: ' int2str(thresh)]);
\r
272 %====================================
\r
274 contImgPos = get(hContAx, 'Position');
\r
275 pos = [contImgPos(1) contImgPos(2) - 3*ctrHt contImgPos(3) ctrHt];
\r
276 hAppPop=uicontrol( ...
\r
277 'Parent', DemoFig, ...
\r
278 'BusyAction','Queue','Interruptible','off',...
\r
279 'Style','popupmenu', ...
\r
280 'BackgroundColor',[.8 .8 .8], ...
\r
281 'Units','pixels', ...
\r
282 'Position',pos, ...
\r
283 'String','none|simple|tc89_l1|tc89_kcos|dp', ...
\r
284 'Tag','ThreshPop', ...
\r
285 'Callback','contdemo(''ControlsUpdate'')');
\r
286 %====================================
\r
288 pos = [contImgPos(1) contImgPos(2) - 2*ctrHt contImgPos(3) ctrHt];
\r
290 'Parent', DemoFig, ...
\r
291 'BusyAction','Queue','Interruptible','off',...
\r
292 'Style','text', ...
\r
293 'Units','pixels', ...
\r
294 'Position',pos, ...
\r
295 'Horiz','center', ...
\r
296 'Background',c, ...
\r
297 'Foreground','black', ...
\r
298 'String','Approx Type:');
\r
300 %====================================
\r
302 pos = [contImgPos(1) contImgPos(2) - 5*ctrHt contImgPos(3) ctrHt];
\r
303 hAppEdit=uicontrol( ...
\r
304 'Parent', DemoFig, ...
\r
305 'BusyAction','Queue','Interruptible','off',...
\r
306 'Style','edit', ...
\r
307 'Background','white', ...
\r
308 'Foreground','black', ...
\r
309 'Units','pixels', ...
\r
310 'Position',pos, ...
\r
311 'String','1.0', ...
\r
312 'Horiz', 'right',...
\r
313 'Tag','AppEdit', ...
\r
314 'Enable', 'off', ...
\r
315 'UserData', [1.0], ...
\r
316 'Callback','contdemo(''ControlsUpdate'')');
\r
317 %====================================
\r
318 % The Approx Edit Label
\r
319 pos = [contImgPos(1) contImgPos(2) - 4*ctrHt contImgPos(3) ctrHt];
\r
321 'Parent', DemoFig, ...
\r
322 'BusyAction','Queue','Interruptible','off',...
\r
323 'Style','text', ...
\r
324 'Units','pixels', ...
\r
325 'Position',pos, ...
\r
326 'Horiz','center', ...
\r
327 'Background',c, ...
\r
328 'Foreground','black', ...
\r
329 'String','Approx Param:');
\r
331 %set(DemoFig,'defaultaxesposition',[0.10 0.1 0.60 0.85])
\r
332 setstatus(DemoFig, 'Initializing Demo...');
\r
333 set(DemoFig, 'Pointer', 'watch');
\r
335 set(DemoFig, 'Visible','on');
\r
337 % Put handles to graphics objects and controls in the figure's userdata
\r
338 handles.SrcImage = hSrcImage;
\r
339 handles.BinImage = hBinImage;
\r
340 handles.ContImage = hContImage;
\r
341 handles.Slider = hSlider;
\r
342 handles.Thresh = hThresh;
\r
343 handles.SrcAx = hSrcAx;
\r
344 handles.BinAx = hBinAx;
\r
345 handles.ContAx = hContAx;
\r
346 handles.ThreshPop = hThreshPop;
\r
347 handles.AppPop = hAppPop;
\r
348 handles.AppEdit = hAppEdit;
\r
350 set(DemoFig, 'UserData', handles);
\r
352 LoadNewImage(DemoFig);
\r
354 set(DemoFig, 'HandleVisibility','Callback')
\r
355 set([closeHndl applyHndl], 'Enable', 'on');
\r
360 %%% Sub-Function - LoadNewImage
\r
363 function LoadNewImage(DemoFig)
\r
369 set(DemoFig,'Pointer','watch');
\r
370 handles = get(DemoFig,'UserData');
\r
371 hSrcImage=handles.SrcImage;
\r
372 hSrcAx = handles.SrcAx;
\r
374 saturn = []; % Make sure saturn is parsed as a variable
\r
375 load cvdemos saturn
\r
379 set(hSrcImage, 'Cdata', img);
\r
380 blank = repmat(uint8(0),128,128);
\r
381 set(handles.BinImage, 'Cdata', blank);
\r
384 ControlsUpdate(DemoFig);
\r
390 %%% Sub-Function - Apply
\r
393 function Apply(DemoFig)
\r
399 handles = get(DemoFig,'UserData');
\r
400 hSrcImage=handles.SrcImage;
\r
401 hBinImage=handles.BinImage;
\r
402 hSlider=handles.Slider;
\r
403 hBinAx=handles.BinAx;
\r
404 hContImage = handles.ContImage;
\r
405 hThreshPop = handles.ThreshPop;
\r
406 hAppPop = handles.AppPop;
\r
407 hAppEdit = handles.AppEdit;
\r
409 img = get(hSrcImage, 'CData');
\r
411 set(DemoFig,'Pointer','watch');
\r
412 setstatus(DemoFig, 'Binarizing image...'); drawnow;
\r
414 % Binarize and display image
\r
416 thresh = floor(get(hSlider, 'Value'));
\r
419 v = get(hThreshPop,{'value','String'});
\r
420 threshType = deblank(v{2}(v{1},:));
\r
422 binImage = cvThreshold(img, thresh, 255, threshType);
\r
423 set(hBinImage, 'CData', binImage);
\r
425 contImage = repmat(uint8(0), size(img));
\r
426 contImage = binImage;
\r
429 v = get(hAppPop,{'Value','String'});
\r
430 appType = deblank(v{2}(v{1},:));
\r
433 appVal = get(hAppEdit, 'UserData');
\r
435 if strcmp(appType, 'dp') == 0
\r
436 Contours = cvFindContours(contImage, 'list', appType);
\r
438 Cont = cvFindContours(contImage, 'list', 'none');
\r
439 Contours = cvApproxPoly(Cont, 1, 'dp', appVal, 1);
\r
442 contImage = repmat(uint8(0), size(img));
\r
443 contImage = cvDrawContours(contImage, Contours, 1, 255, 255, 1);
\r
445 set(hContImage, 'CData', contImage);
\r
449 setstatus(DemoFig, 'Done');
\r
450 set(DemoFig,'Pointer','arrow'); drawnow
\r
455 %%% Sub-function - ControlsUpdate
\r
458 function ControlsUpdate(DemoFig)
\r
464 handles = get(DemoFig,'UserData');
\r
466 hSlider=handles.Slider;
\r
467 hThresh = handles.Thresh;
\r
468 hAppEdit = handles.AppEdit;
\r
469 hAppPop = handles.AppPop;
\r
472 thresh = floor(get(hSlider, 'Value'));
\r
473 set(hThresh, 'String', ['Threshold: ' int2str(thresh)]);
\r
476 oldv = get(hAppEdit, 'UserData');
\r
477 newv = str2num(get(hAppEdit, 'String'));
\r
478 if isempty(newv) | newv(1) < 0
\r
481 set(hAppEdit, 'String', num2str(newv(1)));
\r
482 set(hAppEdit, 'UserData', newv(1));
\r
485 v = get(hAppPop,{'Value','String'});
\r
486 appType = deblank(v{2}(v{1},:));
\r
487 if strcmp(appType, 'dp') == 0
\r
488 set(hAppEdit, 'Enable', 'off');
\r
490 set(hAppEdit, 'Enable', 'on');
\r
493 setstatus(DemoFig, 'Press ''Apply'' button');
\r