Commit 97854e2b authored by biagio montesano's avatar biagio montesano

Corrected problem of descriptors computation using LSD lines

parent ca0f70b2
......@@ -229,10 +229,10 @@ class CV_EXPORTS_W BinaryDescriptor : public Algorithm
private:
/* compute Gaussian pyramids */
void computeGaussianPyramid( const Mat& image );
void computeGaussianPyramid( const Mat& image, const int numOctaves );
/* compute Sobel's derivatives */
void computeSobel( const Mat& image );
void computeSobel( const Mat& image, const int numOctaves );
/* conversion of an LBD descriptor to its binary representation */
unsigned char binaryConversion( float* f1, float* f2 );
......
......@@ -49,6 +49,8 @@
#include <iostream>
#define MATCHES_DIST_THRESHOLD 25
using namespace cv;
static const char* keys =
......@@ -63,225 +65,9 @@ static void help()
}
inline void writeMat( cv::Mat m, std::string name, int n )
{
std::stringstream ss;
std::string s;
ss << n;
ss >> s;
std::string fileNameConf = name + s;
cv::FileStorage fsConf( fileNameConf, cv::FileStorage::WRITE );
fsConf << "m" << m;
fsConf.release();
}
inline void loadMat( cv::Mat& m, std::string name )
{
cv::FileStorage fsConf( name, cv::FileStorage::READ );
fsConf["m"] >> m;
fsConf.release();
}
int binaryDist( const uchar * p_descriptor, const uchar * p_trained )
{
int count = 0;
for ( int i = 0; i < 32; i++ )
{
uchar a = p_descriptor[i];
uchar a1 = a & 1;
uchar a2 = a & 2;
uchar a4 = a & 4;
uchar a8 = a & 8;
uchar a16 = a & 16;
uchar a32 = a & 32;
uchar a64 = a & 64;
uchar a128 = a & 128;
uchar b = p_trained[i];
uchar b1 = b & 1;
uchar b2 = b & 2;
uchar b4 = b & 4;
uchar b8 = b & 8;
uchar b16 = b & 16;
uchar b32 = b & 32;
uchar b64 = b & 64;
uchar b128 = b & 128;
if( a1 == b1 )
count++;
if( a2 == b2 )
count++;
if( a4 == b4 )
count++;
if( a8 == b8 )
count++;
if( a16 == b16 )
count++;
if( a32 == b32 )
count++;
if( a64 == b64 )
count++;
if( a128 == b128 )
count++;
}
return count;
}
std::vector<DMatch> computeBruteForceSingleImages( Mat descriptor_query, Mat descriptor_db )
{
//BRUTE FORCE//
std::vector<DMatch> matches;
for ( int i = 0; i < descriptor_query.rows; i++ )
{
const uchar * p_descriptor = ( descriptor_query.ptr() ) + i * 32;
const uchar * p_trained = descriptor_db.ptr();
int min_dist = 0;
int min_index = -1;
for ( int k = 0; k < descriptor_db.rows; k++ )
{
int dist = binaryDist( p_descriptor, p_trained + ( k * 32 ) );
if( dist > min_dist )
{
min_dist = dist;
min_index = k;
}
}
DMatch m( i, min_index, (float) min_dist );
matches.push_back( m );
}
return matches;
}
void computeDescr( Mat sm_image, Mat img )
{
Mat query = sm_image.clone();
Mat db = img.clone();
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
std::vector<KeyLine> keylines1, keylines2;
bd->detect( query, keylines1 );
bd->detect( db, keylines2 );
/* compute descriptors */
cv::Mat descr1, descr2;
bd->compute( query, keylines1, descr1 );
bd->compute( db, keylines2, descr2 );
std::vector<cv::KeyPoint> keypoints_1;
std::vector<cv::KeyPoint> keypoints_2;
std::vector<std::pair<cv::KeyPoint, int> > v_pair_k1;
std::vector<std::pair<cv::KeyPoint, int> > v_pair_k2;
for ( int i = 0; i < keylines1.size(); i++ )
{
KeyLine l = keylines1[i];
keypoints_1.push_back( cv::KeyPoint( l.startPointX, l.startPointY, 8, l.angle ) );
v_pair_k1.push_back( std::make_pair( cv::KeyPoint( l.startPointX, l.startPointY, 8, l.angle ), i ) );
}
for ( int i = 0; i < keylines2.size(); i++ )
{
KeyLine l = keylines2[i];
keypoints_2.push_back( cv::KeyPoint( l.startPointX, l.startPointY, 8, l.angle ) );
v_pair_k2.push_back( std::make_pair( cv::KeyPoint( l.startPointX, l.startPointY, 8, l.angle ), i ) );
}
// vector<DMatch> matches = ImageFinderFLANN::computeBruteForceSingleImages(purged_descriptor_query, purged_descriptor_db );
std::vector<DMatch> matches = computeBruteForceSingleImages( descr1, descr2 );
Mat img_draw_matches, img_draw_matches_debug;
std::vector<DMatch> good_matches;
int thresh_good = 200;
for ( int i = 0; i < matches.size(); i++ )
{
if( matches[i].distance > thresh_good )
{
good_matches.push_back( matches[i] );
}
}
srand( (unsigned) time( 0 ) );
int lowest = 100, highest = 255;
int range = ( highest - lowest ) + 1;
unsigned int r, g, b;
//DISEGNO MATCHES
std::vector<cv::KeyPoint> fake_k1;
std::vector<cv::KeyPoint> fake_k2;
std::vector<cv::DMatch> fake_match;
drawMatches( sm_image, fake_k1, img, fake_k2, fake_match, img_draw_matches, Scalar::all( -1 ), Scalar::all( -1 ), Mat(),
DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
for ( int i = 0; i < keylines1.size(); i++ )
{
KeyLine line = keylines1[i];
cv::Point startP( line.sPointInOctaveX, line.sPointInOctaveY );
cv::Point endP( line.ePointInOctaveX, line.ePointInOctaveY );
cv::Point midP( ( startP.x + endP.x ) / 2, ( startP.y + endP.y ) / 2 );
//cv::putText(img_draw_matches, std::to_string(i), midP, 1, 1, Scalar(255,0,0), 1 );
cv::line( img_draw_matches, startP, endP, Scalar( 0, 0, 255 ) );
}
for ( int i = 0; i < keylines2.size(); i++ )
{
KeyLine line = keylines2[i];
cv::Point startP( line.sPointInOctaveX + sm_image.cols, line.sPointInOctaveY );
cv::Point endP( line.ePointInOctaveX + sm_image.cols, line.ePointInOctaveY );
cv::Point midP( ( startP.x + endP.x ) / 2, ( startP.y + endP.y ) / 2 );
//cv::putText(img_draw_matches, std::to_string(i), midP, 1, 1, Scalar(255,0,0), 1 );
cv::line( img_draw_matches, startP, endP, Scalar( 0, 0, 255 ) );
}
for ( int i = 0; i < good_matches.size(); i++ )
{
r = lowest + int( rand() % range );
g = lowest + int( rand() % range );
b = lowest + int( rand() % range );
std::pair<cv::KeyPoint, int> tmp_pair_1 = v_pair_k1[good_matches[i].queryIdx];
std::pair<cv::KeyPoint, int> tmp_pair_2 = v_pair_k2[good_matches[i].trainIdx];
cv::KeyPoint tmp_key_1 = tmp_pair_1.first;
cv::KeyPoint tmp_key_2 = tmp_pair_2.first;
KeyLine line1 = keylines1[tmp_pair_1.second];
cv::Point startP1( line1.sPointInOctaveX, line1.sPointInOctaveY );
cv::Point endP1( line1.ePointInOctaveX, line1.ePointInOctaveY );
cv::line( img_draw_matches, startP1, endP1, Scalar( r, g, b ), 2 );
KeyLine line2 = keylines2[tmp_pair_2.second];
cv::Point startP2( line2.sPointInOctaveX + sm_image.cols, line2.sPointInOctaveY );
cv::Point endP2( line2.ePointInOctaveX + sm_image.cols, line2.ePointInOctaveY );
cv::line( img_draw_matches, startP2, endP2, Scalar( r, g, b ), 2 );
cv::Point startP_connect( tmp_key_1.pt.x, tmp_key_1.pt.y );
cv::Point endP_connect( tmp_key_2.pt.x + sm_image.cols, tmp_key_2.pt.y );
cv::line( img_draw_matches, startP_connect, endP_connect, Scalar( r, g, b ), 2 );
}
imshow( "Imshow", img_draw_matches );
waitKey();
}
int main( int argc, char** argv )
{
/* get parameters from comand line */
/* get parameters from command line */
CommandLineParser parser( argc, argv, keys );
String image_path1 = parser.get<String>( 0 );
String image_path2 = parser.get<String>( 1 );
......@@ -308,22 +94,12 @@ int main( int argc, char** argv )
/* create a pointer to a BinaryDescriptor object with default parameters */
Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
/* compute lines */
/* compute lines and descriptors */
std::vector<KeyLine> keylines1, keylines2;
cv::Mat descr1, descr2;
bd->detect( imageMat2, keylines2, mask2 );
bd->detect( imageMat1, keylines1, mask1 );
//compute descriptors
/* cv::Mat descr1, descr2;*/
cv::Mat descr1, descr2;
bd->compute( imageMat1, keylines1, descr1 );
bd->compute( imageMat2, keylines2, descr2 );
//cv::Mat descr1, descr2;
//( *bd )( imageMat1, mask1, keylines1, descr1, true, false );
//( *bd )( imageMat2, mask2, keylines2, descr2, true, false );
( *bd )( imageMat1, mask1, keylines1, descr1, false, false );
( *bd )( imageMat2, mask2, keylines2, descr2, false, false );
/* create a BinaryDescriptorMatcher object */
......@@ -332,21 +108,13 @@ int main( int argc, char** argv )
/* require match */
std::vector<DMatch> matches;
bdm->match( descr1, descr2, matches );
/* Mat newd1, newd2;
loadMat(newd1, "bd_descriptors0");
loadMat(newd2, "bd_descriptors1");*/
//matches = computeBruteForceSingleImages(newd1, newd2);
//matches = computeBruteForceSingleImages( descr1, descr2 );
/* select best matches */
std::vector<DMatch> good_matches;
int thresh_good = 25;
for(int i = 0; i<matches.size(); i++)
for(int i = 0; i<(int)matches.size(); i++)
{
if(matches[i].distance < thresh_good)
{
if(matches[i].distance < MATCHES_DIST_THRESHOLD)
good_matches.push_back(matches[i]);
}
}
/* plot matches */
......@@ -358,31 +126,52 @@ int main( int argc, char** argv )
imshow( "Matches", outImg );
waitKey();
/* create an LSD detector */
Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
/* detect lines */
std::vector<KeyLine> klsd1, klsd2;
Mat lsd_descr1, lsd_descr2;
lsd->detect(imageMat1, klsd1, 2, 2, mask1);
lsd->detect(imageMat2, klsd2, 2, 2, mask2);
bd->compute( imageMat1, klsd1, lsd_descr1 );
bd->compute( imageMat2, klsd2, lsd_descr2 );
/* select lines from first octave */
std::vector<KeyLine> octave0_1, octave0_2;
for(int i = 0; i<(int)klsd1.size(); i++)
{
if(klsd1[i].octave == 0)
octave0_1.push_back(klsd1[i]);
}
for(int j = 0; j<(int)klsd1.size(); j++)
{
if(klsd2[j].octave == 0)
octave0_2.push_back(klsd2[j]);
}
/* compute descriptors for lines from first octave */
bd->compute( imageMat1, octave0_1, lsd_descr1 );
bd->compute( imageMat2, octave0_2, lsd_descr2 );
/* compute matches */
std::vector<DMatch> lsd_matches;
bdm->match( lsd_descr1, lsd_descr2, lsd_matches);
/* select best matches */
good_matches.clear();
for(int i = 0; i<lsd_matches.size(); i++)
for(int i = 0; i<(int)lsd_matches.size(); i++)
{
if(lsd_matches[i].distance < thresh_good)
{
if(lsd_matches[i].distance < MATCHES_DIST_THRESHOLD)
good_matches.push_back(lsd_matches[i]);
}
}
/* plot matches */
cv::Mat lsd_outImg;
std::vector<char> lsd_mask( matches.size(), 1 );
drawLineMatches( imageMat1, klsd1, imageMat2, klsd2, good_matches , lsd_outImg, Scalar::all( -1 ), Scalar::all( -1 ), lsd_mask,
DrawLinesMatchesFlags::DEFAULT );
std::vector<char> lsd_mask( matches.size(), 1 );
drawLineMatches( imageMat1, octave0_1, imageMat2, octave0_2, good_matches , lsd_outImg, Scalar::all( -1 ), Scalar::all( -1 ), lsd_mask,
DrawLinesMatchesFlags::DEFAULT );
imshow("LSD matches", lsd_outImg);
waitKey();
......
......@@ -224,10 +224,8 @@ void BinaryDescriptor::operator()( InputArray image, InputArray mask, CV_OUT std
/* initialize output matrix */
//descriptors.create( Size( 32, (int) keylines.size() ), CV_8UC1 );
/* store reference to output matrix */
//descrMat = descriptors.getMat();
/* compute descriptors */
if( !useProvidedKeyLines )
computeImpl( imageMat, keylines, descrMat, returnFloatDescr, true );
......@@ -235,7 +233,7 @@ void BinaryDescriptor::operator()( InputArray image, InputArray mask, CV_OUT std
else
computeImpl( imageMat, keylines, descrMat, returnFloatDescr, false );
descrMat.copyTo(descriptors);
descrMat.copyTo( descriptors );
}
BinaryDescriptor::~BinaryDescriptor()
......@@ -286,7 +284,7 @@ static inline int get2Pow( int i )
}
/* compute Gaussian pyramids */
void BinaryDescriptor::computeGaussianPyramid( const Mat& image )
void BinaryDescriptor::computeGaussianPyramid( const Mat& image, const int numOctaves )
{
/* clear class fields */
images_sizes.clear();
......@@ -299,7 +297,7 @@ void BinaryDescriptor::computeGaussianPyramid( const Mat& image )
images_sizes.push_back( currentMat.size() );
/* fill Gaussian pyramid */
for ( int pyrCounter = 1; pyrCounter < params.numOfOctave_; pyrCounter++ )
for ( int pyrCounter = 1; pyrCounter < numOctaves; pyrCounter++ )
{
/* compute and store next image in pyramid and its size */
pyrDown( currentMat, currentMat, Size( currentMat.cols / params.reductionRatio, currentMat.rows / params.reductionRatio ) );
......@@ -309,21 +307,21 @@ void BinaryDescriptor::computeGaussianPyramid( const Mat& image )
}
/* compute Sobel's derivatives */
void BinaryDescriptor::computeSobel( const cv::Mat& image )
void BinaryDescriptor::computeSobel( const cv::Mat& image, const int numOctaves )
{
std::cout << "SOBEL" << std::endl;
/* compute Gaussian pyramids */
computeGaussianPyramid( image );
computeGaussianPyramid( image, numOctaves );
/* reinitialize class structures */
dxImg_vector.clear();
dyImg_vector.clear();
dxImg_vector.resize( params.numOfOctave_ );
dyImg_vector.resize( params.numOfOctave_ );
// dxImg_vector.resize( params.numOfOctave_ );
// dyImg_vector.resize( params.numOfOctave_ );
std::cout<<"octaveImages.size(): "<<octaveImages.size()<<std::endl;
dxImg_vector.resize( octaveImages.size() );
dyImg_vector.resize( octaveImages.size() );
/* compute derivatives */
for ( size_t sobelCnt = 0; sobelCnt < octaveImages.size(); sobelCnt++ )
......@@ -377,19 +375,13 @@ void BinaryDescriptor::detect( const std::vector<Mat>& images, std::vector<std::
void BinaryDescriptor::detectImpl( const Mat& imageSrc, std::vector<KeyLine>& keylines, const Mat& mask ) const
{
std::cout<<"n channels imageSRC: "<<imageSrc.channels()<<std::endl;
cv::Mat image;
if( imageSrc.channels() != 1 )
{
std::cout<<"entra1"<<std::endl;
cvtColor( imageSrc, image, COLOR_BGR2GRAY );
}
else
{
std::cout<<"entra2"<<std::endl;
image = imageSrc.clone();
//imageSrc.copyTo(image);
}
/*check whether image depth is different from 0 */
if( image.depth() != 0 )
......@@ -490,21 +482,27 @@ void BinaryDescriptor::computeImpl( const Mat& imageSrc, std::vector<KeyLine>& k
BinaryDescriptor* bd = const_cast<BinaryDescriptor*>( this );
if( !useDetectionData )
bd->computeSobel( image );
/* get maximum class_id */
/* get maximum class_id and octave*/
int numLines = 0;
int octaveIndex = -1;
for ( size_t l = 0; l < keylines.size(); l++ )
{
if( keylines[l].class_id > numLines )
numLines = keylines[l].class_id;
if( keylines[l].octave > octaveIndex )
octaveIndex = keylines[l].octave;
}
if( !useDetectionData )
bd->computeSobel( image, octaveIndex + 1 );
/* create a ScaleLines object */
OctaveSingleLine fictiousOSL;
fictiousOSL.octaveCount = params.numOfOctave_ + 1;
LinesVec lv( params.numOfOctave_, fictiousOSL );
// fictiousOSL.octaveCount = params.numOfOctave_ + 1;
// LinesVec lv( params.numOfOctave_, fictiousOSL );
fictiousOSL.octaveCount = octaveIndex + 1;
LinesVec lv( octaveIndex + 1, fictiousOSL );
ScaleLines sl( numLines + 1, lv );
/* create a map to record association between KeyLines and their position
......@@ -548,7 +546,8 @@ void BinaryDescriptor::computeImpl( const Mat& imageSrc, std::vector<KeyLine>& k
{
for ( size_t j = 0; j < sl[i].size(); j++ )
{
if( (int) ( sl[i][j] ).octaveCount > params.numOfOctave_ )
//if( (int) ( sl[i][j] ).octaveCount > params.numOfOctave_ )
if( (int) ( sl[i][j] ).octaveCount > octaveIndex )
( sl[i] ).erase( ( sl[i] ).begin() + j );
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment