Ask Your Question
0

Is that impossible to use if condition in opencv with float variable?

asked 2017-02-10 05:51:20 -0500

updated 2017-02-10 06:22:58 -0500

LBerger gravatar image

I want to match two images using opencv. but I just found some mismatches and I tried to eliminate those by checking the slope between two matching points. But it does not working with if statement.the condition in the if statement is not checking. Plz help me out with an solution.

the code is here....


#include "opencv2\opencv.hpp"
#include <stdio.h>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>
using namespace std;
using namespace cv;
//
int main(  )
{
    Mat tmp = cv::imread( "1.jpg", 1 );
    Mat in  = cv::imread( "2.jpg", 1 );
     cv::SiftFeatureDetector detector( 0.5, 1.0 );
    cv::SiftDescriptorExtractor extractor;
    /*cv::SurfFeatureDetector detector(400, 1.0);
    cv::SurfDescriptorExtractor extractor(0.01);*/
   vector<KeyPoint> keypoints1, keypoints2;
    detector.detect( tmp, keypoints1 );
    detector.detect( in, keypoints2 );
    Mat feat1,feat2;
    drawKeypoints(tmp,keypoints1,feat1,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    drawKeypoints(in,keypoints2,feat2,Scalar(255, 255, 255),DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
    imwrite( "feat1.bmp", feat1 );
    imwrite( "feat2.bmp", feat2 );
    int key1 = keypoints1.size();
    int key2 = keypoints2.size();
    printf("Keypoint1=%d \nKeypoint2=%d", key1, key2);
    Mat descriptor1,descriptor2;
    extractor.compute( tmp, keypoints1, descriptor1 );
    extractor.compute( in, keypoints2, descriptor2 );
BruteForceMatcher<L2<float> > matcher;
std::vector< DMatch > matches;
matcher.match( descriptor1, descriptor2, matches );
std::vector< DMatch > good_matches;
std::vector< DMatch > fnl_matches;
  Mat img_matches;
 int cnt=0;
for( int i = 0; i < (int)matches.size(); i++ )
{
   float x=keypoints1[matches[i].queryIdx].pt.x;
   float y=keypoints1[matches[i].queryIdx].pt.y;
   float x2=keypoints2[matches[i].trainIdx].pt.x;
   float y2=keypoints2[matches[i].trainIdx].pt.y;
   float slope=(y2-y)/(x2-x);
if( slope = 0 )
    {
     good_matches.push_back( matches[i]);
     cnt=cnt+1;
    }
  }
for( int i = 0; i < (int)matches.size(); i++ )
  { printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  count %d \n", i, matches[i].queryIdx, matches[i].trainIdx,cnt ); }
drawMatches( tmp, keypoints1, in, keypoints2,
               matches, img_matches, Scalar::all(-1), Scalar::all(-1),
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
    namedWindow("SIFT", CV_WINDOW_AUTOSIZE );
    imshow("SIFT", img_matches);
    imwrite("sift_1.jpg",img_matches);
    waitKey(0);
    return 0;
}**
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-02-10 06:23:57 -0500

LBerger gravatar image

if( slope = 0 ) no it is if( slope == 0 )

edit flag offensive delete link more
1

answered 2017-02-11 02:14:12 -0500

berak gravatar image

apart from the obvious error, do not compare floats to 0 directly (this might go wrong in various ways).

rather compare against some epsilon, something like:

if (fabs(slope) < 0.00001f)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-02-10 05:51:20 -0500

Seen: 92 times

Last updated: Feb 11 '17