Go to the documentation of this file.00001
00002 #include "ptam/MiniPatch.h"
00003 using namespace CVD;
00004 using namespace std;
00005
00006
00007 inline int MiniPatch::SSDAtPoint(CVD::BasicImage<CVD::byte> &im, const CVD::ImageRef &ir)
00008 {
00009 if(!im.in_image_with_border(ir, mnHalfPatchSize))
00010 return mnMaxSSD + 1;
00011 ImageRef irImgBase = ir - ImageRef(mnHalfPatchSize, mnHalfPatchSize);
00012 int nRows = mimOrigPatch.size().y;
00013 int nCols = mimOrigPatch.size().x;
00014 byte *imagepointer;
00015 byte *templatepointer;
00016 int nDiff;
00017 int nSumSqDiff = 0;
00018 for(int nRow = 0; nRow < nRows; nRow++)
00019 {
00020 imagepointer = &im[irImgBase + ImageRef(0,nRow)];
00021 templatepointer = &mimOrigPatch[ImageRef(0,nRow)];
00022 for(int nCol = 0; nCol < nCols; nCol++)
00023 {
00024 nDiff = imagepointer[nCol] - templatepointer[nCol];
00025 nSumSqDiff += nDiff * nDiff;
00026 };
00027 };
00028 return nSumSqDiff;
00029 }
00030
00031
00032
00033
00034 bool MiniPatch::FindPatch(CVD::ImageRef &irPos,
00035 CVD::BasicImage<CVD::byte> &im,
00036 int nRange,
00037 vector<ImageRef> &vCorners,
00038 std::vector<int> *pvRowLUT)
00039 {
00040 ImageRef irCenter = irPos;
00041 ImageRef irBest;
00042 int nBestSSD = mnMaxSSD + 1;
00043 ImageRef irBBoxTL = irPos - ImageRef(nRange, nRange);
00044 ImageRef irBBoxBR = irPos + ImageRef(nRange, nRange);
00045 vector<ImageRef>::iterator i;
00046 if(!pvRowLUT)
00047 {
00048 for(i = vCorners.begin(); i!=vCorners.end(); i++)
00049 if(i->y >= irBBoxTL.y) break;
00050 }
00051 else
00052 {
00053 int nTopRow = irBBoxTL.y;
00054 if(nTopRow < 0)
00055 nTopRow = 0;
00056 if(nTopRow >= (int) pvRowLUT->size())
00057 nTopRow = (int) pvRowLUT->size() - 1;
00058 i = vCorners.begin() + (*pvRowLUT)[nTopRow];
00059 }
00060
00061 for(; i!=vCorners.end(); i++)
00062 {
00063 if(i->x < irBBoxTL.x || i->x > irBBoxBR.x)
00064 continue;
00065 if(i->y > irBBoxBR.y)
00066 break;
00067 int nSSD = SSDAtPoint(im, *i);
00068
00069 if(nSSD < nBestSSD)
00070 {
00071 irBest = *i;
00072 nBestSSD = nSSD;
00073 }
00074 }
00075 if(nBestSSD < mnMaxSSD)
00076 {
00077 irPos = irBest;
00078 return true;
00079 }
00080 else
00081 return false;
00082 }
00083
00084
00085 void MiniPatch::SampleFromImage(ImageRef irPos, BasicImage<byte> &im)
00086 {
00087 assert(im.in_image_with_border(irPos, mnHalfPatchSize));
00088 CVD::ImageRef irPatchSize( 2 * mnHalfPatchSize + 1 , 2 * mnHalfPatchSize + 1);
00089 mimOrigPatch.resize(irPatchSize);
00090 copy(im, mimOrigPatch, mimOrigPatch.size(), irPos - mimOrigPatch.size() / 2);
00091 }
00092
00093
00094 int MiniPatch::mnHalfPatchSize = 4;
00095 int MiniPatch::mnRange = 10;
00096 int MiniPatch::mnMaxSSD = 9999;
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109