0

Right now I am working on an OCR algorithm with Template Matching, using the opencv library. I am comparing pixel by pixel, and till now I have obtained good results. The problem comes when the area I want to match is of different size.

Ex: Template size = 70x100  while ROI = 140x200. 

Is there any function that I can use in order adapt the required size and end up with the same amount of rows and columns?

Thanks Robert Grech

1 Answers1

0

Usually one makes an image scale pyramid and then only scans with the 70x100 windows across all scales i.e. as in opencv HOGDescriptor:

    double scale = 1.;
double scale0 = 1.05;
int maxLevels = 64;
int nLevels;
Size templateSize(70,100);
cv::Mat testImage = cv::imread("test1.jpg");

vector<double> levelScale;
for( nLevels = 0; nLevels < maxLevels; nLevels++ )
{
    levelScale.push_back(scale);
    if( cvRound(testImage.cols/scale) < templateSize.width ||
        cvRound(testImage.rows/scale) < templateSize.height ||
        scale0 <= 1 )
        break;
    scale *= scale0;
}
nLevels = std::max(nLevels, 1);
levelScale.resize(nLevels);

int level;
for(level =0; level<nLevels; level++)
{
    cv::Mat testAtScale;
    Size sz(cvRound(testImage.cols/levelScale[level]),
                    cvRound(testImage.rows/levelScale[level]));
    resize(testImage,testAtScale,sz);
    //result = match(template,testAtScale);
            //cv::imshow("sclale",testAtScale);
    //cv::waitKey();

}

you would then need to post-process your results back to the original scale, this is simple with a box, but if you have a heat map / response map / probability map, then re-sizing it back up maybe somewhat hacky.

QED
  • 808
  • 8
  • 11