Move the sources to trunk
[opencv] / interfaces / matlab / toolbox / opencv / cvdemos / contdemo.m
1 function contdemo(action,varargin);\r
2 %   OpenCV contour functions demo\r
3 \r
4 global demoName;\r
5 demoName = 'OpenCV Contour Demo';\r
6 if nargin<1,\r
7    action='InitializeDEMO';\r
8 end;\r
9 \r
10 feval(action,varargin{:});\r
11 return;\r
12 \r
13 \r
14 %%%\r
15 %%%  Sub-function - InitializeDEMO\r
16 %%%\r
17 \r
18 function InitializeDEMO()\r
19 \r
20 global demoName;\r
21 \r
22 % If demo is already running, bring it to the foreground.\r
23 h = findobj(allchild(0), 'tag', demoName);\r
24 if ~isempty(h)\r
25    figure(h(1))\r
26    return\r
27 end\r
28 \r
29 screenD = get(0, 'ScreenDepth');\r
30 if screenD>8\r
31    grayres=256;\r
32 else\r
33    grayres=128;\r
34 end\r
35  \r
36 DemoFig=figure( ...\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
45 \r
46 %====================================\r
47 % Information for all buttons (and menus)\r
48 btnWid=0.175;\r
49 btnHt=0.06;\r
50 \r
51 %====================================\r
52 % The CONSOLE frame\r
53 frmBorder=0.02;\r
54 frmBottom = 0.15;\r
55 \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
60 h=uicontrol( ...\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
67 \r
68 %====================================\r
69 % The Apply button\r
70 labelStr='Apply';\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
82 \r
83 \r
84 %====================================\r
85 % The CLOSE button\r
86 labelStr='Close';\r
87 callbackStr='close(gcf)';\r
88 \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
98 \r
99 c = get(DemoFig,'Color');\r
100 if [.298936021 .58704307445 .114020904255]*c'<.5,\r
101    fgColor = [1 1 1];\r
102 else\r
103    fgColor = [0 0 0];\r
104 end\r
105 \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
110 \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
123 \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
134 \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
145 \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
156 \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
164 \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
172 \r
173 % Status bar\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
187 \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
206 h = uicontrol( ...\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
216 \r
217 %===================================\r
218 % Set up scroll bar\r
219 thresh = 128;\r
220 \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
230    'min',0, ...\r
231    'max',255, ...\r
232    'Interruptible','off', ...\r
233    'Callback',callbackStr);\r
234 \r
235 % Left and right range indicators\r
236 pos = [binImgPos(1) binImgPos(2) - 6*ctrHt binImgPos(3)/2 ctrHt];\r
237 uicontrol( ...\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
246    'String','0');\r
247 \r
248 pos = [binImgPos(1)+binImgPos(3)/2 binImgPos(2) - 6*ctrHt binImgPos(3)/2 ctrHt];\r
249 uicontrol( ...\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
258    'String','255');\r
259 \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
271 \r
272 %====================================\r
273 % The Approx Popup\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
287 % The Approx Label\r
288 pos = [contImgPos(1) contImgPos(2) - 2*ctrHt contImgPos(3) ctrHt];\r
289 h = uicontrol( ...\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
299 \r
300 %====================================\r
301 % The Approx Edit\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
320 h = uicontrol( ...\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
330 \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
334 drawnow\r
335 set(DemoFig, 'Visible','on');\r
336 \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
349 \r
350 set(DemoFig, 'UserData', handles);\r
351 \r
352 LoadNewImage(DemoFig);\r
353 \r
354 set(DemoFig, 'HandleVisibility','Callback')\r
355 set([closeHndl applyHndl], 'Enable', 'on');\r
356 return\r
357 \r
358 \r
359 %%%\r
360 %%%  Sub-Function - LoadNewImage\r
361 %%%\r
362 \r
363 function LoadNewImage(DemoFig)\r
364 \r
365 if nargin<1\r
366    DemoFig = gcbf;\r
367 end\r
368 \r
369 set(DemoFig,'Pointer','watch');\r
370 handles = get(DemoFig,'UserData');\r
371 hSrcImage=handles.SrcImage;\r
372 hSrcAx = handles.SrcAx;\r
373 \r
374 saturn = [];  % Make sure saturn is parsed as a variable\r
375 load cvdemos saturn\r
376 \r
377 img = saturn;\r
378 \r
379 set(hSrcImage, 'Cdata', img);\r
380 blank = repmat(uint8(0),128,128);\r
381 set(handles.BinImage, 'Cdata', blank);\r
382 drawnow;\r
383 \r
384 ControlsUpdate(DemoFig);\r
385 Apply(DemoFig);\r
386 return;\r
387 \r
388 \r
389 %%%\r
390 %%%  Sub-Function - Apply\r
391 %%%\r
392 \r
393 function Apply(DemoFig)\r
394 \r
395 if nargin<1\r
396    DemoFig = gcbf;\r
397 end\r
398 \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
408 \r
409 img = get(hSrcImage, 'CData');\r
410 \r
411 set(DemoFig,'Pointer','watch');\r
412 setstatus(DemoFig, 'Binarizing image...'); drawnow;\r
413 \r
414 % Binarize and display image\r
415 \r
416 thresh = floor(get(hSlider, 'Value'));\r
417 \r
418 % threshType\r
419 v = get(hThreshPop,{'value','String'});\r
420 threshType = deblank(v{2}(v{1},:));\r
421 \r
422 binImage = cvThreshold(img, thresh, 255, threshType);\r
423 set(hBinImage, 'CData', binImage);\r
424 \r
425 contImage = repmat(uint8(0), size(img));\r
426 contImage = binImage;\r
427 \r
428 % approx type\r
429 v = get(hAppPop,{'Value','String'});\r
430 appType = deblank(v{2}(v{1},:));\r
431 \r
432 % approx value\r
433 appVal = get(hAppEdit, 'UserData');\r
434 \r
435 if strcmp(appType, 'dp') == 0\r
436     Contours = cvFindContours(contImage, 'list', appType);\r
437 else\r
438     Cont = cvFindContours(contImage, 'list', 'none');\r
439     Contours = cvApproxPoly(Cont, 1, 'dp', appVal, 1);\r
440 end\r
441 \r
442 contImage = repmat(uint8(0), size(img));\r
443 contImage = cvDrawContours(contImage, Contours, 1, 255, 255, 1);\r
444 \r
445 set(hContImage, 'CData', contImage);\r
446 \r
447 drawnow;\r
448 \r
449 setstatus(DemoFig, 'Done');\r
450 set(DemoFig,'Pointer','arrow'); drawnow\r
451 return\r
452 \r
453 \r
454 %%%\r
455 %%%  Sub-function - ControlsUpdate\r
456 %%%\r
457 \r
458 function ControlsUpdate(DemoFig)\r
459 \r
460 if nargin<1\r
461    DemoFig = gcbf;\r
462 end;\r
463 \r
464 handles = get(DemoFig,'UserData');\r
465 \r
466 hSlider=handles.Slider;\r
467 hThresh = handles.Thresh;\r
468 hAppEdit = handles.AppEdit;\r
469 hAppPop  = handles.AppPop;\r
470 \r
471 % threshold value\r
472 thresh = floor(get(hSlider, 'Value'));\r
473 set(hThresh, 'String', ['Threshold: ' int2str(thresh)]);\r
474 \r
475 % approx value\r
476 oldv = get(hAppEdit, 'UserData');\r
477 newv = str2num(get(hAppEdit, 'String'));\r
478 if isempty(newv) | newv(1) < 0\r
479     newv = oldv;\r
480 end\r
481 set(hAppEdit, 'String', num2str(newv(1)));\r
482 set(hAppEdit, 'UserData', newv(1));\r
483 \r
484 % approx type\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
489 else\r
490     set(hAppEdit, 'Enable', 'on');\r
491 end\r
492 \r
493 setstatus(DemoFig, 'Press ''Apply'' button');\r
494 return;\r