findcorners

PURPOSE ^

FINDCORNERS is the main function called by the calibration GUI.

SYNOPSIS ^

function imgcout=findcorners(img,debug)

DESCRIPTION ^

 FINDCORNERS is the main function called by the calibration GUI.

 FINDCORNERS takes a grayscale image return a structure variable imgcout 
 containing the extracted chessboard grid.
 
 USAGE:
     imgcout=findcorners(img);
 
 INPUTS:
     img: grayscale image (any class)
     debug: for debugging
 
 OUTPUTS:
     imgcout: structure containing the following elements:
 
         grid: NxDx2 array containing the x and y coordinates of the
         chessboard corners detected
         
         win: the minimum distance between chessboard corners in pixels
         
         suspicious: flag indicating wether the output requires user
         confirmation
         
         fail: flag indicationg the failure of detection

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function imgcout=findcorners(img,debug)
0002 % FINDCORNERS is the main function called by the calibration GUI.
0003 %
0004 % FINDCORNERS takes a grayscale image return a structure variable imgcout
0005 % containing the extracted chessboard grid.
0006 %
0007 % USAGE:
0008 %     imgcout=findcorners(img);
0009 %
0010 % INPUTS:
0011 %     img: grayscale image (any class)
0012 %     debug: for debugging
0013 %
0014 % OUTPUTS:
0015 %     imgcout: structure containing the following elements:
0016 %
0017 %         grid: NxDx2 array containing the x and y coordinates of the
0018 %         chessboard corners detected
0019 %
0020 %         win: the minimum distance between chessboard corners in pixels
0021 %
0022 %         suspicious: flag indicating wether the output requires user
0023 %         confirmation
0024 %
0025 %         fail: flag indicationg the failure of detection
0026 
0027 %
0028 % Input Checking
0029 %
0030 
0031 % check image
0032 if size(img,3)>1
0033     error('Input image is required to be grayscale');
0034 end
0035 
0036 if min(size(img))<50
0037     error('Image too small');
0038 end
0039 
0040 % check debug flag
0041 if ~exist('debug','var') || isempty(debug)
0042     debug=0;
0043 end
0044 
0045 % change image into double
0046 img=double(img);
0047 img=gscale(img,'minmax');
0048 
0049 
0050 %
0051 % Parameters
0052 %
0053 
0054 hwin=3; % Harris window size
0055 th=0.5; % Parameter to adjust adaptive thresholding
0056 
0057 %
0058 % Make Pyramid
0059 %
0060 % Each level in pyramid consists of:
0061 % im: image
0062 % imadj: adjusted image
0063 % ime: sobel edge image
0064 % ix: x component of edge image
0065 % iy: y component of edge image
0066 
0067 nolevels=3; % no of levels
0068 pyr=cell(1,3); % initialise pyr
0069 
0070 for cntr=1:nolevels
0071     % downsize image
0072     im=imresize(img,1/(2^(cntr-1)));
0073     pyr{cntr}.im=im;
0074     
0075     % adaptively adjust
0076     pyr{cntr}.imadj=adaptimadj(im,[]);
0077     
0078     % get sobel edge image
0079     [imgedge,ix,iy]=getedges(im);
0080     pyr{cntr}.ime=imgedge;
0081     pyr{cntr}.ix=ix;
0082     pyr{cntr}.iy=iy;
0083 end
0084 
0085 % set nocrnrs and level
0086 nocrnrs=-1;
0087 level=1;
0088 
0089 for cntr=1:length(pyr)
0090     imgh=harris(pyr{cntr}.imadj,hwin);
0091 
0092     [mimg,stdv]=adaptstats(imgh);
0093     ctcrnrpts=getcrnrpts(imgh,mimg,stdv,th);
0094     
0095     % check
0096     if isempty(ctcrnrpts)
0097         continue;
0098     end
0099     
0100     [ctcrnrs,ctnocrnrs,ctpeaklocs]=chesscornerfilter(pyr{cntr}.im,pyr{cntr}.ime,ctcrnrpts,debug);
0101     if ctnocrnrs>nocrnrs
0102         nocrnrs=ctnocrnrs;
0103         crnrs=ctcrnrs;
0104         crnrpts=ctcrnrpts;
0105         peaklocs=ctpeaklocs;
0106         level=cntr;
0107     end
0108     
0109     % debugging
0110     if debug
0111         close all;
0112         figure;imshow(pyr{cntr}.imadj);
0113         hold on;plot(ctcrnrpts(2,:),ctcrnrpts(1,:),'+');
0114         if ~isempty(ctcrnrs)
0115             plot(ctcrnrs(2,:),ctcrnrs(1,:),'square','color','r');
0116         end
0117         pause;
0118     end
0119 end
0120 
0121 
0122 %   Check for enough no of corners
0123 if nocrnrs<10
0124     imgcout=failmode;
0125     return;
0126 end
0127 
0128 % debugging
0129 if debug
0130     close all;
0131     figure;imshow(pyr{level}.im);
0132     hold on;plot(crnrpts(2,:),crnrpts(1,:),'+');
0133     if ~isempty(crnrs)
0134         plot(crnrs(2,:),crnrs(1,:),'square','color','r');
0135         pause;
0136     end
0137 end
0138 
0139 
0140 %
0141 % Extract Grid
0142 %
0143 
0144 crnrsgrid=getgrid(crnrs,crnrpts,peaklocs,pyr{level}.ix,pyr{level}.iy,debug);
0145 
0146 crnrsgridfil=filtergrid(crnrsgrid);
0147 
0148 % check grid size
0149 if min(size(crnrsgridfil(:,:,1)))<3
0150     imgcout=failmode;
0151     return;
0152 end
0153 
0154 crnrsgridfil=adjgriddir(crnrsgridfil);
0155 [gridfullrect,nointerpolations]=getmisscrnrs(crnrsgridfil);
0156 if isempty(gridfullrect)
0157     imgcout=failmode;
0158     return;
0159 end
0160 gridfullrect=adjgridorigin(gridfullrect);
0161 
0162 % adjust grid back to full scale
0163 gridfullrect=gridfullrect*(2^(level-1));
0164 crnrpts=crnrpts*(2^(level-1));
0165 
0166 [grid,win,nobadpts]=getsubpixcrnrs(img,crnrpts,gridfullrect);
0167 
0168 if min(size(grid(:,:,1)))<3
0169     imgcout=failmode;
0170     return;
0171 end
0172 
0173 susth=4;
0174 
0175 suspicious=0;
0176 if nobadpts>susth || nointerpolations>susth
0177     suspicious=1;
0178 end
0179 
0180 if debug
0181     close all;
0182     figure;
0183     imshow(pyr{level}.im);
0184     hold on;   
0185     plot(crnrsgrid(:,:,2),crnrsgrid(:,:,1),'+');
0186     figure;
0187     imshow(pyr{level}.im);
0188     hold on;
0189     plot(crnrsgridfil(:,:,2),crnrsgridfil(:,:,1),'o');
0190     figure;
0191     imshow(img);
0192     hold on;
0193     plot(gridfullrect(:,:,2),gridfullrect(:,:,1),'square');
0194     figure;imshow(img);hold on;
0195     plot(grid(:,:,2),grid(:,:,1),'o');
0196     imgcptrd=getframe(gcf);
0197     imgcout.img=imgcptrd.cdata;
0198 end
0199 
0200 %Output Results
0201 imgcout.grid=grid;
0202 imgcout.suspicious=suspicious;
0203 imgcout.win=win;
0204 imgcout.fail=false;
0205 
0206 
0207 %fail mode
0208 function imgcout=failmode
0209 imgcout.grid=[];
0210 imgcout.suspicious=[];
0211 imgcout.win=[];
0212 imgcout.fail=true;

Generated on Sun 04-Apr-2010 17:13:59 by m2html © 2005