+++ /dev/null
-function flowdemo(action,varargin);\r
-% OpenCV Optical Flow functions demo\r
-\r
-global demoName;\r
-demoName = 'OpenCV Optical Flow demo';\r
-if nargin<1,\r
- action='InitializeDEMO';\r
-end;\r
-\r
-feval(action,varargin{:});\r
-return;\r
-\r
-\r
-%%%\r
-%%% Sub-function - InitializeDEMO\r
-%%%\r
-\r
-function InitializeDEMO()\r
-\r
-global demoName;\r
-\r
-% If demo is already running, bring it to the foreground.\r
-h = findobj(allchild(0), 'tag', demoName);\r
-if ~isempty(h)\r
- figure(h(1))\r
- return\r
-end\r
-\r
-screenD = get(0, 'ScreenDepth');\r
-if screenD>8\r
- grayres=256;\r
-else\r
- grayres=128;\r
-end\r
- \r
-imgSize = [320, 240];\r
-ctrHt = 19; % controls' height\r
-\r
-%==================================\r
-% Set up the figure\r
-DemoFig=figure( ...\r
- 'Name',demoName, ...\r
- 'NumberTitle','off', 'HandleVisibility', 'on', ...\r
- 'tag', demoName, ...\r
- 'Visible','off', 'Resize', 'off',...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'IntegerHandle', 'off', ...\r
- 'Doublebuffer', 'on', ...\r
- 'Units', 'pixels', ...\r
- 'Colormap', gray(grayres));\r
-\r
-figpos = get(DemoFig, 'Position');\r
-figpos(3) = 2.2*imgSize(1);\r
-figpos(4) = imgSize(2) + 10*ctrHt;\r
-set(DemoFig, 'Position', figpos);\r
-\r
-%==================================\r
-% Set up the image axes\r
-row = figpos(4); col = figpos(3);\r
-vertSpac = ctrHt;\r
-horSpac = (col-2*imgSize(1))/3;\r
-hSrcAx = axes('Parent', DemoFig, ...\r
- 'units', 'pixels', ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'ydir', 'reverse', ...\r
- 'XLim', [.5 imgSize(1)+0.5], ...\r
- 'YLim', [.5 imgSize(2)+0.5],...\r
- 'CLim', [0 255], ...\r
- 'XTick',[],'YTick',[], ...\r
- 'Position', [horSpac row-vertSpac-imgSize(2) imgSize], ...\r
- 'UserData', []);\r
-title('Source Image');\r
-\r
-hDstAx = axes('Parent', DemoFig, ...\r
- 'units', 'pixels', ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'ydir', 'reverse', ...\r
- 'XLim', [.5 imgSize(1)+0.5], ...\r
- 'YLim', [.5 imgSize(2)+0.5],...\r
- 'CLim', [0 255], ...\r
- 'XTick',[],'YTick',[], ...\r
- 'Position', [2 * horSpac + imgSize(1) row-vertSpac-imgSize(2) imgSize], ...\r
- 'UserData', []);\r
-title('Destination Image');\r
-\r
-%==================================\r
-% Set up the images\r
-blank = repmat(uint8(0),imgSize(2),imgSize(1));\r
-hSrcImage = image('Parent', hSrcAx,...\r
- 'CData', blank, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'CDataMapping', 'scaled', ...\r
- 'Xdata', [1 1+imgSize(1)],...\r
- 'Ydata', [1 1+imgSize(2)],...\r
- 'ButtonDownFcn', 'flowdemo(''GetFeatures'')', ...\r
- 'EraseMode', 'none', ...\r
- 'UserData', []);\r
-\r
-hDstImage = image('Parent', hDstAx,...\r
- 'CData', blank, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'CDataMapping', 'scaled', ...\r
- 'Xdata', [1 1+imgSize(1)],...\r
- 'Ydata', [1 1+imgSize(2)],...\r
- 'EraseMode', 'none', ...\r
- 'UserData', []);\r
-\r
-%====================================\r
-% Information for all buttons (and menus)\r
-btnWid=100;\r
-btnHt=1.5*ctrHt;\r
-\r
-%====================================\r
-% The CONSOLE frame\r
-frmBorder=0.5 * ctrHt;\r
-dstImgPos = get(hDstAx, 'Position');\r
-\r
-frmColor = [0.45, 0.45, 0.45];\r
-\r
-frmWidth = 2*frmBorder + btnWid;\r
-frmHeight = 3*frmBorder + 2*btnHt;\r
-frmLeft = dstImgPos(1);\r
-frmBottom = dstImgPos(2)-ctrHt-frmHeight;\r
-frmPos=[frmLeft, frmBottom, ...\r
- frmWidth, frmHeight];\r
-h=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','frame', ...\r
- 'Units','pixels', ...\r
- 'Position',frmPos, ...\r
- 'BackgroundColor', frmColor);\r
-\r
-%====================================\r
-% The Apply button\r
-labelStr='Apply';\r
-callbackStr='flowdemo(''Apply'')';\r
-yPos=frmBottom+frmBorder;\r
-applyHndl=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','pushbutton', ...\r
- 'Units','pixels', ...\r
- 'Position',[frmLeft+frmBorder frmBottom+btnHt+2*frmBorder btnWid btnHt], ...\r
- 'String',labelStr, ...\r
- 'Enable', 'off', ...\r
- 'Callback',callbackStr);\r
-\r
-\r
-%====================================\r
-% The CLOSE button\r
-labelStr='Close';\r
-callbackStr='close(gcf)';\r
-\r
-closeHndl=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','pushbutton', ...\r
- 'Units','pixels', ...\r
- 'Position',[frmLeft+frmBorder frmBottom+frmBorder btnWid btnHt], ...\r
- 'Enable', 'off', ...\r
- 'String',labelStr, ...\r
- 'Callback',callbackStr);\r
-\r
-c = get(DemoFig,'Color');\r
-\r
-% Status bar\r
-% rangePos = [64 3 280 15];\r
-rangePos = [0 .01 1 .05];\r
-hStatus = uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','text', ...\r
- 'Units','normalized', ...\r
- 'Position',rangePos, ...\r
- 'Horiz','center', ...\r
- 'Background',c, ...\r
- 'Foreground',[.8 0 0], ...\r
- 'Tag', 'Status', ...\r
- 'String','Status bar');\r
-\r
-srcImgPos = get(hSrcAx, 'Position');\r
-\r
-%====================================\r
-% The Good Features frame\r
-\r
-frmWidth = 2*frmBorder + btnWid;\r
-frmHeight = 3*frmBorder + 2*btnHt;\r
-frmLeft = srcImgPos(1)+srcImgPos(3)-frmWidth;\r
-frmBottom = srcImgPos(2)-ctrHt-frmHeight;\r
-frmPos=[frmLeft, frmBottom, ...\r
- frmWidth, frmHeight];\r
-h=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','frame', ...\r
- 'Units','pixels', ...\r
- 'Position',frmPos, ...\r
- 'Background', frmColor);\r
-\r
-\r
-%====================================\r
-% The Good Features Label\r
-pos = [frmLeft+frmBorder, frmBottom + frmHeight - ctrHt-1, ...\r
- frmWidth-2*frmBorder, ctrHt];\r
-h = uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','text', ...\r
- 'Units','pixels', ...\r
- 'Position',pos, ...\r
- 'Horiz','center', ...\r
- 'Background', frmColor, ...\r
- 'Foreground','black', ...\r
- 'String','Good Features');\r
-\r
-%====================================\r
-% The Good Features Number Label\r
-pos = [frmLeft+frmBorder, ...\r
- frmBottom + frmHeight - 1*ctrHt-2*frmBorder, ...\r
- (frmWidth-3*frmBorder)/2, ctrHt];\r
-h = uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','text', ...\r
- 'Units','pixels', ...\r
- 'Position',pos, ...\r
- 'Horiz','right', ...\r
- 'Background', frmColor, ...\r
- 'Foreground','black', ...\r
- 'String','Number: ');\r
-\r
-%====================================\r
-% The Good Features Number Edit\r
-pos = [frmLeft+2*frmBorder+(frmWidth-3*frmBorder)/2, ...\r
- frmBottom + frmHeight - 1*ctrHt-2*frmBorder, ...\r
- (frmWidth-3*frmBorder)/2, ctrHt];\r
-hNumEdit=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','edit', ...\r
- 'Background','white', ...\r
- 'Foreground','black', ...\r
- 'Units','pixels', ...\r
- 'Position',pos, ...\r
- 'String','20', ...\r
- 'Horiz', 'right',...\r
- 'Tag','AppEdit', ...\r
- 'UserData', [20], ...\r
- 'Callback','flowdemo(''ControlsUpdate'')');\r
-\r
-%====================================\r
-% The Generate button\r
-labelStr='Generate';\r
-callbackStr='flowdemo(''GoodFeatures'')';\r
-\r
-genHndl=uicontrol( ...\r
- 'Parent', DemoFig, ...\r
- 'BusyAction','Queue','Interruptible','off',...\r
- 'Style','pushbutton', ...\r
- 'Units','pixels', ...\r
- 'Position',[frmLeft+frmBorder frmBottom+frmBorder btnWid btnHt], ...\r
- 'Enable', 'off', ...\r
- 'String',labelStr, ...\r
- 'Callback',callbackStr);\r
-\r
-setstatus(DemoFig, 'Initializing Demo...');\r
-set(DemoFig, 'Pointer', 'watch');\r
-drawnow;\r
-set(DemoFig, 'Visible','on');\r
-\r
-% Put handles to graphics objects and controls in the figure's userdata\r
-handles.SrcImage = hSrcImage;\r
-handles.DstImage = hDstImage;\r
-\r
-handles.SrcAx = hSrcAx;\r
-handles.DstAx = hDstAx;\r
-\r
-handles.NumEdit = hNumEdit;\r
-\r
-set(DemoFig, 'UserData', handles);\r
-\r
-LoadNewImage(DemoFig);\r
-\r
-set(DemoFig, 'HandleVisibility','Callback')\r
-set([closeHndl applyHndl genHndl], 'Enable', 'on');\r
-return\r
-\r
-\r
-%%%\r
-%%% Sub-Function - LoadNewImage\r
-%%%\r
-\r
-function LoadNewImage(DemoFig)\r
-\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end\r
-\r
-set(DemoFig,'Pointer','watch');\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hSrcImage = handles.SrcImage;\r
-hDstImage = handles.DstImage;\r
-\r
-rock1 = [];\r
-rock2 = [];\r
-load cvdemos rock1 rock2;\r
-\r
-lkrock1 = [];\r
-load cvdemos lkrock1;\r
-\r
-img = rock1;\r
-featuresA = lkrock1(1:10:200,:);\r
-set(hSrcImage, 'Cdata', img);\r
-set(hSrcImage, 'UserData', featuresA);\r
-\r
-img = rock2;\r
-set(hDstImage, 'Cdata', img);\r
-set(hDstImage, 'UserData', []);\r
-\r
-drawnow;\r
-\r
-setstatus(DemoFig, 'Press ''Apply'' button');\r
-Apply(DemoFig);\r
-return;\r
-\r
-\r
-%%%\r
-%%% Sub-Function - Apply\r
-%%%\r
-\r
-function Apply(DemoFig)\r
-\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hSrcImage=handles.SrcImage;\r
-hDstImage=handles.DstImage;\r
-\r
-set(DemoFig,'Pointer','watch');\r
-setstatus(DemoFig, 'Calculating flow...'); drawnow;\r
-\r
-srcImage = get(hSrcImage, 'CData');\r
-dstImage = get(hDstImage, 'CData');\r
-\r
-featuresA = get(hSrcImage, 'UserData');\r
-\r
-winSize = [10, 10];\r
-level = 2;\r
-criteria = [20, 0.03];\r
-\r
-% [featuresA(:,2)-1, featuresA(:,1)-1], [featuresA(:,2)-1, featuresA(:,1)-1], ...\r
-[featuresB, error] = cvCalcOpticalFlowPyrLK(srcImage, dstImage, ...\r
- featuresA, featuresA, ...\r
- winSize, level, criteria);\r
-%featuresB = [featuresB(:,2), featuresB(:,1)]+1;\r
-\r
-set(hDstImage, 'UserData', featuresB);\r
-FeaturesUpdate(DemoFig);\r
-\r
-setstatus(DemoFig, 'Done');\r
-set(DemoFig,'Pointer','arrow'); drawnow\r
-return\r
-\r
-\r
-%%%\r
-%%% Sub-function - ControlsUpdate\r
-%%%\r
-\r
-function ControlsUpdate(DemoFig)\r
-\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end;\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hNumEdit = handles.NumEdit;\r
-\r
-% num value\r
-oldv = get(hNumEdit, 'UserData');\r
-newv = str2num(get(hNumEdit, 'String'));\r
-if isempty(newv) | newv(1) < 1 | newv(1) > 100\r
- newv = oldv;\r
-end\r
-set(hNumEdit, 'String', num2str(newv(1)));\r
-set(hNumEdit, 'UserData', newv(1));\r
-\r
-setstatus(DemoFig, 'Press ''Generate'' button');\r
-return;\r
-\r
-%%%\r
-%%% Sub-function - FeaturesUpdate\r
-%%%\r
-\r
-function FeaturesUpdate(DemoFig)\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end;\r
-\r
-pointSize = 10; % size of point cross\r
-width = 2; % width of cross lines\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hSrcAx = handles.SrcAx;\r
-hDstAx = handles.DstAx;\r
-\r
-hSrcImage = handles.SrcImage;\r
-hDstImage = handles.DstImage;\r
-\r
-hFeaturesA = get(hSrcAx, 'UserData');\r
-hFeaturesB = get(hDstAx, 'UserData');\r
-\r
-featuresA = get(hSrcImage, 'UserData');\r
-featuresB = get(hDstImage, 'UserData');\r
-\r
-delete(hFeaturesA(:));\r
-delete(hFeaturesB(:));\r
-hFeaturesA = [];\r
-hFeaturesB = [];\r
-% draw featuresA\r
-for fnum = 1:size(featuresA,1)\r
- hFeaturesA(2*fnum - 1) = line('Parent', hSrcAx, ...\r
- 'XData', [featuresA(fnum, 1)-pointSize featuresA(fnum, 1)+pointSize], ...\r
- 'YData', [featuresA(fnum, 2) featuresA(fnum, 2)], ...\r
- 'Color', 'blue', 'LineWidth', width);\r
- hFeaturesA(2*fnum) = line('Parent', hSrcAx, ...\r
- 'XData', [featuresA(fnum, 1) featuresA(fnum, 1)], ...\r
- 'YData', [featuresA(fnum, 2)-pointSize featuresA(fnum, 2)+pointSize], ...\r
- 'Color', 'blue', 'LineWidth', width);\r
-end\r
-% draw featuresB\r
-% If flow was not found, when red cross with featureA coordinates is drawn\r
-for fnum = 1:size(featuresB,1)\r
- if isnan(featuresB(fnum)) == 1\r
- hFeaturesB(2*fnum - 1) = line('Parent', hDstAx, ...\r
- 'XData', [featuresA(fnum, 1)-pointSize featuresA(fnum, 1)+pointSize], ...\r
- 'YData', [featuresA(fnum, 2) featuresA(fnum, 2)], ...\r
- 'Color', 'red', 'LineWidth', width);\r
- hFeaturesB(2*fnum) = line('Parent', hDstAx, ...\r
- 'XData', [featuresA(fnum, 1) featuresA(fnum, 1)], ...\r
- 'YData', [featuresA(fnum, 2)-pointSize featuresA(fnum, 2)+pointSize], ...\r
- 'Color', 'red', 'LineWidth', width);\r
- else\r
- hFeaturesB(2*fnum - 1) = line('Parent', hDstAx, ...\r
- 'XData', [featuresB(fnum, 1)-pointSize featuresB(fnum, 1)+pointSize], ...\r
- 'YData', [featuresB(fnum, 2) featuresB(fnum, 2)], ...\r
- 'Color', 'blue', 'LineWidth', width);\r
- hFeaturesB(2*fnum) = line('Parent', hDstAx, ...\r
- 'XData', [featuresB(fnum, 1) featuresB(fnum, 1)], ...\r
- 'YData', [featuresB(fnum, 2)-pointSize featuresB(fnum, 2)+pointSize], ...\r
- 'Color', 'blue', 'LineWidth', width);\r
- end\r
-end\r
-\r
-set(hSrcAx, 'UserData', hFeaturesA);\r
-set(hDstAx, 'UserData', hFeaturesB);\r
-return;\r
-\r
-%%%\r
-%%% Sub-function - GetFeatures\r
-%%%\r
-\r
-function GetFeatures(DemoFig)\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end;\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hSrcAx = handles.SrcAx;\r
-\r
-hSrcImage = handles.SrcImage;\r
-hDstImage = handles.DstImage;\r
-\r
-srcImage = get(hSrcImage, 'CData');\r
-\r
-featuresA = get(hSrcImage, 'UserData');\r
-featuresB = get(hDstImage, 'UserData');\r
-\r
-set(hSrcImage, 'UserData', []);\r
-set(hDstImage, 'UserData', []);\r
-FeaturesUpdate(DemoFig);\r
-\r
-[x, y] = getpts(hSrcAx);\r
-imgSize = [size(srcImage,2), size(srcImage,1)];\r
-points = [x, y];\r
-points(points(:,1)<1 | points(:,1) > imgSize(1) | ...\r
- points(:,2)<1 | points(:,2) > imgSize(2) ,:) = [];\r
-if ~isempty(points)\r
- featuresA = points;\r
- featuresB = [];\r
- setstatus(DemoFig, 'Press ''Apply'' button');\r
-end\r
-set(hSrcImage, 'UserData', featuresA);\r
-set(hDstImage, 'UserData', featuresB);\r
-FeaturesUpdate(DemoFig);\r
-return;\r
-\r
-%%%\r
-%%% Sub-function - GoodFeatures\r
-%%%\r
-\r
-function GoodFeatures(DemoFig)\r
-if nargin<1\r
- DemoFig = gcbf;\r
-end;\r
-\r
-handles = get(DemoFig,'UserData');\r
-\r
-hSrcAx = handles.SrcAx;\r
-hNumEdit = handles.NumEdit;\r
-hSrcImage = handles.SrcImage;\r
-hDstImage = handles.DstImage;\r
-\r
-srcImage = get(hSrcImage, 'CData');\r
-\r
-featuresA = get(hSrcImage, 'UserData');\r
-featuresB = get(hDstImage, 'UserData');\r
-\r
-set(hSrcImage, 'UserData', []);\r
-set(hDstImage, 'UserData', []);\r
-FeaturesUpdate(DemoFig);\r
-\r
-numFeatures = get(hNumEdit, 'UserData');\r
-qualityLevel = 0.1;\r
-minDistance = size(srcImage, 1) / 5;\r
-points = cvGoodFeaturesToTrack(srcImage, numFeatures, ...\r
- qualityLevel, minDistance);\r
-%points = [points(:, 2), points(:, 1)] + 1;\r
-if ~isempty(points)\r
- featuresA = points;\r
- featuresB = [];\r
- setstatus(DemoFig, 'Press ''Apply'' button');\r
-else\r
- setstatus(DemoFig, 'The features were not found');\r
-end\r
-set(hSrcImage, 'UserData', featuresA);\r
-set(hDstImage, 'UserData', featuresB);\r
-FeaturesUpdate(DemoFig);\r
-return;
\ No newline at end of file