Opencv Feature Matching is not matching correctly for cropped images of different sizes and images taken from various sources?

asked 2016-01-04 03:08:07 -0500

Alagiri gravatar image

I am trying to match two images one is screen shot of mobile screen and template image is any app icon.If i match source and template cropped from same images it is matching perfectly.But when i use app icon cropped from different mobile screen it is not matching properly.

For image matching am working on the following code:

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

Mat objectImg = imread("source.jpg", cv::IMREAD_GRAYSCALE);
Mat sceneImg = imread("note4-3.jpg", cv::IMREAD_GRAYSCALE);

//cv::resize(sceneImg,sceneImg,objectImg.size(),0,0,CV_INTER_CUBIC);

if( !objectImg.data || !sceneImg.data )
{
    printf( " No image data \n " );
    return -1337;
}

std::vector<cv::KeyPoint> objectKeypoints;
std::vector<cv::KeyPoint> sceneKeypoints;
cv::Mat objectDescriptors;
cv::Mat sceneDescriptors;

Ptr<FeatureDetector> detector;
detector = cv::MSER::create();
detector->detect(objectImg, objectKeypoints);
detector->detect(sceneImg, sceneKeypoints);

Ptr<DescriptorExtractor> extractor = cv::ORB::create();
extractor->compute( objectImg, objectKeypoints, objectDescriptors );
extractor->compute( sceneImg, sceneKeypoints, sceneDescriptors );

if(objectDescriptors.type()!=CV_32F) {
objectDescriptors.convertTo(objectDescriptors, CV_32F);
}

if(sceneDescriptors.type()!=CV_32F) {
sceneDescriptors.convertTo(sceneDescriptors, CV_32F);
}

vector< vector<DMatch> > matches;
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce");
matcher->knnMatch( objectDescriptors, sceneDescriptors, matches, 8 );

double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < objectDescriptors.rows; i++ )
{ 
    double dist = matches[i][0].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
}

std::vector<cv::DMatch> good_matches;

for( int i = 0; i < objectDescriptors.rows; i++ )
{ 
    if( matches[i][0].distance <= max(2*min_dist, 0.02) ) {
        good_matches.push_back( matches[i][0]); 
    }

}

//look whether the match is inside a defined area of the image
//only 25% of maximum of possible distance
/*double tresholdDist = 0.50 * sqrt(double(sceneImg.size().height*sceneImg.size().height + sceneImg.size().width*sceneImg.size().width));

vector< DMatch > good_matches2;
good_matches2.reserve(matches.size());  
for (size_t i = 0; i < matches.size(); ++i)
{ 
    for (int j = 0; j < matches[i].size(); j++)
    {
        Point2f from = objectKeypoints[matches[i][j].queryIdx].pt;
        Point2f to = sceneKeypoints[matches[i][j].trainIdx].pt;

        //calculate local distance for each possible match
        double dist = sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y));

        //save as best match if local distance is in specified area and on same height
        if (dist < tresholdDist && abs(from.y-to.y)<5)
        {
            good_matches2.push_back(matches[i][j]);
            j = matches[i].size();
        }
    }
}*/

Mat allmatchs;
   drawMatches(objectImg,objectKeypoints,sceneImg,sceneKeypoints,good_matches,allmatchs,Scalar::all(-1), Scalar::all(-1),vector<char>(),0);
namedWindow("Matchs" , CV_WINDOW_NORMAL);
imshow( "Matchs",allmatchs);

waitKey(0);

}

Wrong Match When cropped from different source

The above result is obtained when matching source from one mobile screen shot and template from different screen shot.

I am using opencv3.0

Please help whether I have make changes in code or i have to use template matching or some other technique.I cannot use SUR detectors since i cannot have paid versions due to license conflits??

edit retag flag offensive close merge delete