Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Opencv FeatureDetecter on tiny images

Tags:

java

opencv

knn

I am using Opencv 3 in Java, I am trying to find small images(like 25x25 pixels) on other image. But FeatureDetector detection (0,0) size Mat on small image.

    Mat smallImage = ...

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

    Mat descriptorsSmall = new Mat();
    MatOfKeyPoint keyPointsSmall = new MatOfKeyPoint();

    detector.detect(smallImage, keyPointsSmall);
    descriptor.compute(smallImage, keyPointsSmall, descriptorsSmall);

Here I am getting keyPointsSmall and descriptorsSmall size as zero, and sure detection is not working.

But if I try this on larger images like 150x150 pixels that is working fine. Any suggestions? Thank you.

Here I am adding samples. we have this source image: This is source image

And let it say we have template for P letter, so we need to detect this P on source image. This is template

well, scaling image to higher resolution will not work for me. That will be lost of time and resource. Ideally it should be rotation-scale invariant. But simple solution without rotation and scale is also ok.

Other solutions except OpenCv is not acceptable for me. (for example using Tesseract)

like image 464
RustamIS Avatar asked Jun 02 '16 05:06

RustamIS


1 Answers

Keypoint detection for text recognition is not the best solution, since you will get many features which look alike and if the templates are very small, the sliding window will not yield enough detected features.

Lucky for you, OpenCV 3 contains a text detection/recognition module in the contrib repository: link, with an example taken from here and many others to find here:

/*
 * cropped_word_recognition.cpp
 *
 * A demo program of text recognition in a given cropped word.
 * Shows the use of the OCRBeamSearchDecoder class API using the provided default classifier.
 *
 * Created on: Jul 9, 2015
 *     Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
 */

#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>

using namespace std;
using namespace cv;
using namespace cv::text;

int main(int argc, char* argv[])
{

    cout << endl << argv[0] << endl << endl;
    cout << "A demo program of Scene Text Character Recognition: " << endl;
    cout << "Shows the use of the OCRBeamSearchDecoder::ClassifierCallback class using the Single Layer CNN character classifier described in:" << endl;
    cout << "Coates, Adam, et al. \"Text detection and character recognition in scene images with unsupervised feature learning.\" ICDAR 2011." << endl << endl;

    Mat image;
    if(argc>1)
        image  = imread(argv[1]);
    else
    {
        cout << "    Usage: " << argv[0] << " <input_image>" << endl;
        cout << "           the input image must contain a single character (e.g. scenetext_char01.jpg)." << endl << endl;
        return(0);
    }

    string vocabulary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // must have the same order as the clasifier output classes

    Ptr<OCRHMMDecoder::ClassifierCallback> ocr = loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");

    double t_r = (double)getTickCount();
    vector<int> out_classes;
    vector<double> out_confidences;

    ocr->eval(image, out_classes, out_confidences);

    cout << "OCR output = \"" << vocabulary[out_classes[0]] << "\" with confidence "
         << out_confidences[0] << ". Evaluated in "
         << ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl << endl;

    return 0;
}
like image 78
PhilLab Avatar answered Sep 28 '22 07:09

PhilLab