function MotBoostEvalOneFrame( frameNum, SeqTemplate, numDigs, outBaseName, iFirsts, iLasts, DataBlock ) % Now find which batch the requested frame sits in most comfortably % (assuming 50 frame overlap of tracked sequences, try to give f +/- 25 % frames of space to exist. numLasts = size(iLasts(:),1); iLast = iLasts( numLasts ); if( frameNum > iLast ) fprintf( 1, 'Error: Wanted Eval frame %d but batches only reach %d\n', frameNum, iLast ); elseif( frameNum > iLast -25 ) iBatch = numLasts; else iBatch = find( iLasts - 25 > frameNum, 1 ); % The (...,1) says: give me at most 1 index. end; if( numDigs == 4 ) % These two have been precomputed already! %banFileName = sprintf( '%s%.4d_%.4d.ban', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); %txt2DfileName = sprintf( '%s%.4d_%.4d_2D.txt', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); matFileName = sprintf( '%s%.4d_%.4d.mat', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); elseif( numDigs == 5 ) % These two have been precomputed already! %banFileName = sprintf( '%s%.5d_%.5d.ban', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); %txt2DfileName = sprintf( '%s%.5d_%.5d_2D.txt', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); matFileName = sprintf( '%s%.5d_%.5d.mat', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); elseif( numDigs == 6 ) % These two have been precomputed already! %banFileName = sprintf( '%s%.5d_%.5d.ban', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); %txt2DfileName = sprintf( '%s%.5d_%.5d_2D.txt', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); matFileName = sprintf( '%s%.6d_%.6d.mat', SeqTemplate, iFirsts(iBatch), iLasts(iBatch) ); end; % Now load up the 2D traj file and 3D camera calibs load( matFileName ); if( DataBlock == 1 ) % Restore! After ICCV deadline. %debugImgName = sprintf( 'G:\\MotBoostData01\\StillsRaw_full\\Test\\Seq05VD_f%.5d.png', frameNum ); debugImgName = sprintf( 'G:\\MotBoostData01\\701_StillsRaw_full\\Seq05VD_f%.5d.png', frameNum ); elseif( DataBlock == 2 ) debugImgName = sprintf( 'G:\\MotBoostData01\\701_StillsRaw_full\\0016E5_%.5d.png', frameNum ); elseif( DataBlock ==3 ) debugImgName = sprintf( 'G:\\MotBoostData01\\701_StillsRaw_full\\0006R0_f%.5d.png', frameNum ); elseif( DataBlock ==4 ) debugImgName = sprintf( 'G:\\MotBoostData01\\701_StillsRaw_full\\0001TP_%.6d.png', frameNum ); end; % debugImgName = 'G:\\MotBoostData01\\StillsRaw_full\\blankWhite320x240.png'; % find( TrajTable(:, 1) == 7074 ); % Find indexes to all frames with this Traj# % find( TrajTable(:, 2) == 1502 ); % Find indexes to all Traj's with this Frame# isOfTrajsExistingNow = find( TrajTable(:, 2) == frameNum ); % Stupid robustness hack like below because of Error: % ??? Subscript indices must either be real positive integers or logicals. while(size(isOfTrajsExistingNow,1) < 1 || min(isOfTrajsExistingNow) < 1 || max(isOfTrajsExistingNow) > size(TrajTable,1) ) fprintf(1, ','); fprintf(1, 'size(isOfTrajsExistingNow,1): %d\n', size(isOfTrajsExistingNow,1) ); fprintf(1, 'min(isOfTrajsExistingNow): %d\n', min(isOfTrajsExistingNow) ); fprintf(1, 'max(isOfTrajsExistingNow): %d\n', max(isOfTrajsExistingNow) ); isOfTrajsExistingNow = find( TrajTable(:, 2) == frameNum ); end; XYs = TrajTable( isOfTrajsExistingNow, 3:4 ); % %-- OUT: Density of Features? % Restore me!! featsFileName = sprintf( '%s%.5d_WhereAreFeatures2D.txt', outBaseName, frameNum ); fid = fopen(featsFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f\n', XYs'); fclose( fid ); %% Height above the Camera Center in current frame. numXYs = size(XYs,1); offsetForFrameNums = iFirsts(iBatch) -1; Cs = zeros( iLasts(iBatch) - iFirsts(iBatch) + 1, 3); j=1; for( iFrameC = iFirsts(iBatch):iLasts(iBatch) ) iInBig = iFrameC - offsetForFrameNums; % Long way, if we only have P... %P = Kbig(:,:, iInBig ) * Rbig(:,:, iInBig ) * [eye(3) -Tbig(:,:, iInBig )]; %p1 = P(:,1); p2 = P(:,2); P(:,3); p3 = P(:,3); p4 = P(:,4); %C = [det([p2 p3 p4]) -det([p1 p3 p4]) det([p1 p2 p4]) -det([p1 p2 p3]) ]; %Rbig(:,:, iInBig ) * [eye(3) -Tbig(:,:, iInBig )] %C = C(1:3) / C(4); %Cs(j, 1:3) = C; Cs(j, 1:3) = [Tbig(:,:, iInBig )]'; % ...vs short way if we have T j = j+1; end; %plot3( Cs(:,1), Cs(:,2), Cs(:,3), 'bo' ); curC = Cs( frameNum-offsetForFrameNums, 1:3); % plot3( curC(:,1), curC(:,2), curC(:,3), 'md', 'MarkerSize', 10 ); XYsIn3D = zeros( numXYs, 3 ); for( iTraj = 1:numXYs ) %fprintf(1, 'iTraj #%d\n', iTraj); trajNum = TrajTable(isOfTrajsExistingNow(iTraj), 1); rowsTrajWasTracked = find( TrajTable(:, 1) == trajNum ); %rowsTrajWasTracked = find( (TrajTable(:, 1) <= trajNum+0.001) && (TrajTable(:, 1) >= trajNum-0.001) ); %% Weird hacks, attempting to circumvent Matlab bug that would crash %% here... Still breaks! %% %TrajByFrame = sortrows( TrajTable(rowsTrajWasTracked, 2:4), 1); rowsTrajWasTracked2 = find( TrajTable(:, 1) == trajNum ); while( (size(rowsTrajWasTracked,1) ~= size(rowsTrajWasTracked2,1)) || sum(rowsTrajWasTracked == rowsTrajWasTracked2) ~= size(rowsTrajWasTracked,1) ) fprintf(1, ','); rowsTrajWasTracked = find( TrajTable(:, 1) == trajNum ); rowsTrajWasTracked2 = find( TrajTable(:, 1) == trajNum ); end; TrajByFrame = TrajTable(round(rowsTrajWasTracked), 2:4); if( size( TrajByFrame, 1 ) < 3 ) continue; % No point in processing too short a track. end; %[VarianceIn3D Res] = VarOfBackProjectTraj( TrajByFrame, offsetForFrameNums, Kbig, Rbig, Tbig ); XYsIn3D(iTraj, 1:3) = BackProject2DTrajTo3D( TrajByFrame, offsetForFrameNums, Kbig, Rbig, Tbig ); fprintf(1, '.'); end; fprintf(1, '\n'); % hold on; % % plot3( XYsIn3D(:,1), XYsIn3D(:,2), XYsIn3D(:,3), 'r.' ); % HighXYs = XYsIn3D( find( XYsIn3D(:, 2) < 0 ), : ); % plot3( HighXYs(:,1), HighXYs(:,2), HighXYs(:,3), 'r.' ); % LowXYs = XYsIn3D( find( XYsIn3D(:, 2) >= 0 ), : ); % plot3( LowXYs(:,1), LowXYs(:,2), LowXYs(:,3), 'g.' ); % hold off; % % %-- OUT: HaboveCamPlusDC HaboveCamPlusDC = -curC(2) + XYsIn3D(:,2); % (+) is above the camera, (-) is below, it hopefully no lower than -7? RawResponse = HaboveCamPlusDC; HaboveCamPlusDC(HaboveCamPlusDC <= 0.1 ) = 0.1; HaboveCamPlusDC(HaboveCamPlusDC > 2) = 2; RawishResponse = HaboveCamPlusDC; radii = 15 * HaboveCamPlusDC; %plot( radii ); %% Just for ICCV video A = imread( debugImgName ); imagesc(A); hold on; plot( XYs(:,1), XYs(:,2), 'r.' ); %%plot( XYs(:,1)/3, XYs(:,2)/3, 'r.' ); num = size(HaboveCamPlusDC, 1); hold off fig=gcf; units=get(fig,'units'); set(fig,'units','normalized','outerposition',[0 0 0.5 0.5]); set(fig,'units',units); pngFileName = sprintf( '%s%.5d_Density.png', outBaseName, frameNum ); %saveas(gcf, bmpFileName, 'bmp'); %print( gcf, '-dpng', '-r80', pngFileName ); print( gcf, '-dpng', '-r147', pngFileName ); % 911 x 719 A = imread( debugImgName ); imagesc(A); hold on; % %%plot( XYs(:,1), XYs(:,2), 'r.' ); Obsolete? % %%plot( XYs(:,1)/3, XYs(:,2)/3, 'r.' ); Obsolete? num = size(HaboveCamPlusDC, 1); for( i=1:num ) plot( XYs(i,1), XYs(i,2), 'o', 'MarkerSize', radii(i), 'MarkerFaceColor', 'none' ); end; %%for( i=1:num ) plot( XYs(i,1)/3, XYs(i,2)/3, 'o', 'MarkerSize', radii(i), 'MarkerFaceColor', 'none' ); end; hold off %plot(30 *radii ) % Seems like a good scaling % % Save active fig to disk. fig=gcf; units=get(fig,'units'); set(fig,'units','normalized','outerposition',[0 0 0.5 0.5]); set(fig,'units',units); pngFileName = sprintf( '%s%.5d_HaboveCamPlusDC.png', outBaseName, frameNum ); %saveas(gcf, bmpFileName, 'bmp'); %print( gcf, '-dpng', '-r80', pngFileName ); print( gcf, '-dpng', '-r147', pngFileName ); % 911 x 719 %---------------------------------- % Just for ICCV video %% End of Just for ICCV video % Restore me!! HaboveCamFileName = sprintf( '%s%.5d_HaboveCamPlusDC.txt', outBaseName, frameNum ); fid = fopen(HaboveCamFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, radii]'); fclose( fid ); % ----------------------------------------------------- % % Extra, to save off Raw and Rawish filter-responses to text % HaboveCamFileName = sprintf( '%sRaw_%.5d_HaboveCamPlusDC.txt', outBaseName, frameNum ); fid = fopen(HaboveCamFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawResponse]'); fclose( fid ); % HaboveCamFileName = sprintf( '%sRawish_%.5d_HaboveCamPlusDC.txt', outBaseName, frameNum ); fid = fopen(HaboveCamFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawishResponse]'); fclose( fid ); % ----------------------------------------------------- % %% Min 3D distance: Closest the camera passes to each World Point. MinDistances = zeros( numXYs, 1 ); for( iTraj = 1:numXYs ) WorldTgt = XYsIn3D(iTraj, 1:3); D = [Cs(:,1) - WorldTgt(1), Cs(:,2) - WorldTgt(2), Cs(:,3) - WorldTgt(3)]; % Now rows of D have a vector from that Pt to each camera. We want the % magnitude of the smallest vector: minDist = min( D(:,1) .* D(:,1) + D(:,2) .* D(:,2) + D(:,3) .* D(:,3) ); % NB!! Since camera tracks are in batches, this is INaccurate for World-Pts % found near batch-boundaries. MinDistances(iTraj) = sqrt(minDist); end; RawResponse = MinDistances; radii = log(500*MinDistances); RawishResponse = radii; radii(radii < 4) = 4; radii = 5 *(radii - 3.9); %plot( radii ); %% Just for ICCV video %debugImgName = 'G:\\MotBoostData01\\StillsRaw_full\\blank320x240.png'; A = imread( debugImgName ); %debugImgName = 'G:\\MotBoostData01\\StillsRaw_full\\blankWhite320x240.png'; imagesc(A); hold on; %%plot( XYs(:,1), XYs(:,2), 'r.' ); Obsolete? %%plot( XYs(:,1)/3, XYs(:,2)/3, 'r.' ); Obsolete? num = size(MinDistances, 1); for( i=1:num ) plot( XYs(i,1), XYs(i,2), 'go', 'MarkerSize', radii(i) ); end; %%for( i=1:num ) plot( XYs(i,1)/3, XYs(i,2)/3, 'go', 'MarkerSize', radii(i) ); end; hold off %plot(20 *radii ) % Seems like a good scaling % % Save active fig to disk. fig=gcf; units=get(fig,'units'); set(fig,'units','normalized','outerposition',[0 0 1 1]); set(fig,'units',units); pngFileName = sprintf( '%s%.5d_MinDistances.png', outBaseName, frameNum ); %saveas(gcf, bmpFileName, 'bmp'); %%print( gcf, '-dpng', pngFileName ); %print( gcf, '-dpng', '-r80', pngFileName ); print( gcf, '-dpng', '-r147', pngFileName ); % 911 x 719 %---------------------------------- % Just for ICCV video %% End of Just for ICCV video % Restore me!! % % % %-- OUT: BuzzWorldPtDist BuzzWorldPtFileName = sprintf( '%s%.5d_BuzzWorldPtDist.txt', outBaseName, frameNum ); fid = fopen(BuzzWorldPtFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, radii]'); fclose( fid ); % ----------------------------------------------------- % % Extra, to save off Raw and Rawish filter-responses to text % BuzzWorldPtFileName = sprintf( '%sRaw_%.5d_BuzzWorldPtDist.txt', outBaseName, frameNum ); fid = fopen(BuzzWorldPtFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawResponse]'); fclose( fid ); % BuzzWorldPtFileName = sprintf( '%sRawish_%.5d_BuzzWorldPtDist.txt', outBaseName, frameNum ); fid = fopen(BuzzWorldPtFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawishResponse]'); fclose( fid ); % ----------------------------------------------------- % %return; % !!!!!!! Shortcut! %% "Variance" in 3D reconstructions, ie does the 3D point seem to shift? MagOf3DVariance = zeros( numXYs, 1 ); for( iTraj = 1:numXYs ) fprintf(1, 'iTraj #%d\n', iTraj); trajNum = TrajTable(isOfTrajsExistingNow(iTraj), 1); rowsTrajWasTracked = find( TrajTable(:, 1) == trajNum ); %TrajByFrame = sortrows( TrajTable(rowsTrajWasTracked, 2:4), 1); rows = round(rowsTrajWasTracked); while( size(rows,1) < 1 || min(rows) < 1 || max(rows) > size(TrajTable,1) ) fprintf(1, ','); fprintf(1, 'size(rows,1): %d\n', size(rows,1) ); fprintf(1, 'min(rows): %d\n', min(rows) ); fprintf(1, 'max(rows): %d\n', max(rows) ); rowsTrajWasTracked = find( TrajTable(:, 1) == trajNum ); rows = round(rowsTrajWasTracked); end; TrajByFrame = TrajTable(rows, 2:4); %Frequently errored above with: "??? Subscript indices must either be real positive integers or logicals." % Even with round()...now trying uint8() if( size( TrajByFrame, 1 ) < 3 ) continue; % No point in processing too short a track. end; [VarianceIn3D Res] = VarOfBackProjectTraj( TrajByFrame, offsetForFrameNums, Kbig, Rbig, Tbig ); %VarianceIn3D(1);%VarianceIn3D;%norm(VarianceIn3D); %MagOf3DVariance(iTraj) = norm(Res); %MagOf3DVariance(iTraj) = norm(VarianceIn3D); % V01 %V05 MagOf3DVariance(iTraj) = Res; % V02 % V03 % V04 %fprintf(1, '.'); end; fprintf(1, '\n'); A = imread( debugImgName ); imagesc(A); hold on; %%plot( XYs(:,1), XYs(:,2), 'r.' ); %%plot( XYs(:,1)/3, XYs(:,2)/3, 'r.' ); %radii = 30 * MagOf3DVariance/max(MagOf3DVariance); RawResponse = MagOf3DVariance; radii = log(1+MagOf3DVariance); RawishResponse = radii; radii = 30 * radii / max(radii); % V01 %V02 %V03 %V04 %V05 % plot(25*radii) % Seems like a good multiplier... Check that: after % removing normalization by "count" - the # of features in a rect, the best % multiplier is 1.0 with the Boujou-density of detected features. num = size(radii, 1); for( i=1:num ) plot( XYs(i,1), XYs(i,2), '.', 'MarkerSize', radii(i) ); end; %for( i=1:num ) plot( XYs(i,1)/3, XYs(i,2)/3, '.', 'MarkerSize', radii(i) ); end; hold off % Save active fig to disk. fig=gcf; units=get(fig,'units'); set(fig,'units','normalized','outerposition',[0 0 1 1]); set(fig,'units',units); pngFileName = sprintf( '%s%.5d_VariancePlot2D.png', outBaseName, frameNum ); %saveas(gcf, bmpFileName, 'bmp'); %%print( gcf, '-dpng', pngFileName ); %print( gcf, '-dpng', '-r80', pngFileName ); print( gcf, '-dpng', '-r147', pngFileName ); % 911 x 719 %---------------------------------- % Restore me!! % % %-- OUT: VarianceMag of 3D reconstructs? varianceFileName = sprintf( '%s%.5d_VarianceMagOf3DReconstructs.txt', outBaseName, frameNum ); fid = fopen(varianceFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; %fprintf( fid, '%f\n', [XYs, MagOf3DVariance]'); %fprintf( fid, '%f %f %f\n', [XYs, MagOf3DVariance]'); fprintf( fid, '%f %f %f\n', [XYs, radii]'); %V04 fclose( fid ); % ----------------------------------------------------- % % Extra, to save off Raw and Rawish filter-responses to text % varianceFileName = sprintf( '%sRaw_%.5d_VarianceMagOf3DReconstructs.txt', outBaseName, frameNum ); fid = fopen(varianceFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawResponse]'); fclose( fid ); % varianceFileName = sprintf( '%sRawish_%.5d_VarianceMagOf3DReconstructs.txt', outBaseName, frameNum ); fid = fopen(varianceFileName,'wt'); % 'rt' means read text if (fid < 0) error('could not open file'); % just abort if error end; fprintf( fid, '%f %f %f\n', [XYs, RawishResponse]'); fclose( fid ); % ----------------------------------------------------- %