android – How to detect specific object in image-ThrowExceptions

Exception or error:

I have 32 alphabet cards like OSMO application (
I wanna recognize them in image or real time in frams of a camera
and I want this app for android device
so I used opencv and c++ for this purpose.
the methods that I tried are

  • first I used sift and surf and orb for getting keyPoints and compare them with a static picture of card
  • second method was the k nearest neighbor.I train some picture and make two xml files which one of them contain the pixels of all train image in binary mode and the ohter one is a classification file which was a map for trained image.

first method had a problem which was the keypoints can change in real time and are not unique.
so matching the keypoints is not good.
second method problem was that xml file loading was too long in android device.
so now I need help
is there any fast reliable method? what can I do?

How to solve:

finally I found a way to detect letters in may cards
for recognize the letters in the cards I use Hu moments
which is some thing like matching the Shape but it is more accurate
below code retrun a double which is the different between two image
so I use the different and compare it with other image and then recognize the letter in the image.

double HuMatchingShape(double*  secondMoments,double* firstMoments)
    double dif = 0;
    int counter = 0;
    for (int i = 0; i < 7; i++)
        dif += (firstMoments[i] - secondMoments[i]) * (firstMoments[i] -secondMoments[i]);
    return sqrt(dif);

double* getHuMoments(Mat img)
    Moments moments = cv::moments(img,true);

    // Calculate Hu Moments
    double * huMoments =  new double[7];

    HuMoments(moments, huMoments);
    for (int i = 0; i < 7; i++)
        huMoments[i] = -1 * copysign(1.0, huMoments[i]) * log10(abs(huMoments[i]));

    return huMoments;

int main()
   Mat image1 = imread("image path");
   Mat image2 =  imread("second image path");
   // make these two images binary 
   image1 = makeItBinay(image1);
   image2 = makeItBinay(image2);
   // calculate different between two image with hu moments 
   double different = HuMatchingShape(getHuMoments(image1), getHuMoments(image2));
   return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *