Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 77 additions & 54 deletions common/utils/ODFeatureDetector2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,52 @@
#include "ODFeatureDetector2D.h"

using namespace std;

namespace od
{

ODFeatureDetector2D::ODFeatureDetector2D(string type, bool use_gpu)
{
mode_ = SIFT; //by default it is SIFT

if(use_gpu) {
mode_ = SIFT; //by default we set the mode to SIFT

if(use_gpu)
{

//##########GPU VERSION


if(type == "ORB") {
if(type == "ORB")
{
mode_ = ORB_GPU;
feature_detector_ = cv::cuda::ORB::create();
} else if(type == "SIFT") {
}
else if(type == "SIFT")
{
mode_ = SIFT_GPU;
sift_gpu_ = new SiftGPU;
//char * argv[] = {(char *)"-fo", (char *)"-1", (char *)"-v", (char *)"1", (char *)"-cuda", (char *)"0"};
char *argv[] = {(char *) "-fo", (char *) "-1", (char *) "-v", (char *) "1"};
int argc = sizeof(argv) / sizeof(char *);
sift_gpu_->ParseParam(argc, argv);
if(sift_gpu_->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
cout << "FATAL ERROR cannot create SIFTGPU context";
}
} else {
}
else
{

//########CPU VERSIONS


if(type == "SIFT") {
if(type == "SIFT")
{
mode_ = SIFT;
feature_detector_ = cv::xfeatures2d::SIFT::create();
} else if(type == "ORB") {
}
else if(type == "ORB")
{
mode_ = ORB;
feature_detector_ = cv::ORB::create();
} else if(type == "SURF") {
}
else if(type == "SURF")
{
mode_ = SURF;
feature_detector_ = cv::xfeatures2d::SURF::create();
}
Expand All @@ -58,7 +66,6 @@ namespace od
feature_detector_->detect(image, keypoints);
feature_detector_->compute(image, keypoints, descriptors);
}
//viewImage(image, keypoints);
}

void CVMatToSiftGPU(const cv::Mat &image, unsigned char *siftImage, cv::Mat &grey)
Expand All @@ -69,79 +76,86 @@ namespace od

memcpy(siftImage, grey.data, image.rows * image.cols);


// tmp2.convertTo(tmp, CV_8U);
// //viewImage(tmp);
//
// for (int y = 0; y < tmp.rows; ++y) {
// for (int x = 0; x < tmp.cols; ++x) {
// siftImage[y * tmp.cols + x] = tmp.at<unsigned char> (y, x);
// }
// }
return;
}

void ODFeatureDetector2D::findSiftGPUDescriptors1(cv::Mat const &image, cv::Mat &descriptors, vector<cv::KeyPoint> &keypoints)
{
unsigned char *data = image.data;
cv::Mat greyimage;
if(image.type() != CV_8U) {
if(image.type() != CV_8U)
{
cv::cvtColor(image, greyimage, cv::COLOR_BGR2GRAY);
data = greyimage.data;
}
sift_gpu_->RunSIFT(image.cols, image.rows, data, GL_LUMINANCE, GL_UNSIGNED_BYTE);

int nFeat = sift_gpu_->GetFeatureNum();//get feature count
//allocate memory for readback
/** get feature count
*/
int nFeat = sift_gpu_->GetFeatureNum();

/** allocate memory for readback
*/
vector<SiftGPU::SiftKeypoint> keys(nFeat);
//read back keypoints and normalized descritpros
//specify NULL if you don’t need keypoints or descriptors

/** read back keypoints and normalized descritpros
* specify NULL if you don’t need keypoints or descriptors
*/
vector<float> imageDescriptors(128 * nFeat);

sift_gpu_->GetFeatureVector(&keys[0], &imageDescriptors[0]);

sift_gpu_->SaveSIFT("2.sift");

//to opencv format
/** to opencv format
*/
keypoints.clear();
descriptors.create(0, 128, CV_32FC1);
for(int i = 0; i < nFeat; ++i) {

for(int i = 0; i < nFeat; ++i)
{
cv::KeyPoint key(keys[i].x, keys[i].y, keys[i].s, keys[i].o);
keypoints.push_back(key);
cv::Mat descriptor(1, 128, CV_32FC1);

for(int x = 0; x < 128; x++)
descriptor.at<float>(x) = floor(0.5 + (512.0f * imageDescriptors[(i * 128) + x]));
for(int j = 0; j < 128; j++)
descriptor.at<float>(j) = floor(0.5 + (512.0f * imageDescriptors[(i * 128) + j]));

descriptors.push_back(descriptor);
}


//viewImage(image, keypoints);

}

void ODFeatureDetector2D::findSiftGPUDescriptors(cv::Mat const &image, cv::Mat &descriptors, vector<cv::KeyPoint> &keypoints)
{
unsigned char *data = image.data;
cv::Mat greyimage;
if(image.type() != CV_8U) {
cv::Mat tmp;
cv::cvtColor(image, tmp, cv::COLOR_BGR2GRAY);
data = tmp.data;
if(image.type() != CV_8U)
{
cv::cvtColor(image, greyimage, cv::COLOR_BGR2GRAY);
data = greyimage.data;
}
sift_gpu_->RunSIFT(image.cols, image.rows, data, GL_LUMINANCE, GL_UNSIGNED_BYTE);

int nFeat = sift_gpu_->GetFeatureNum();//get feature count
//allocate memory for readback
/** get feature count
*/
int nFeat = sift_gpu_->GetFeatureNum();

/** allocate memory for readback
*/
vector<SiftGPU::SiftKeypoint> keys(nFeat);
//read back keypoints and normalized descritpros
//specify NULL if you don’t need keypoints or descriptors

/** read back keypoints and normalized descritpros
* specify NULL if you don’t need keypoints or descriptors
*/
vector<float> imageDescriptors(128 * nFeat);

sift_gpu_->GetFeatureVector(&keys[0], &imageDescriptors[0]);

sift_gpu_->SaveSIFT("2.sift");

//to opencv format
/** to opencv format
*/
keypoints.clear();
cv::Mat descriptormat = cv::Mat(1, nFeat, CV_32F, &imageDescriptors[0]);
descriptormat.copyTo(descriptors);
Expand All @@ -151,29 +165,35 @@ namespace od
keypoints.push_back(key);
}


//viewImage(image, keypoints);

}

void ODFeatureDetector2D::findSiftGPUDescriptors(char const *image_name, cv::Mat &descriptors, vector<cv::KeyPoint> &keypoints)
{
sift_gpu_->RunSIFT(image_name);

int nFeat = sift_gpu_->GetFeatureNum();//get feature count
//allocate memory for readback
/** get feature count
*/
int nFeat = sift_gpu_->GetFeatureNum();

/** allocate memory for readback
*/
vector<SiftGPU::SiftKeypoint> keys(nFeat);
//read back keypoints and normalized descritpros
//specify NULL if you don’t need keypoints or descriptors

/** read back keypoints and normalized descritpros
* specify NULL if you don’t need keypoints or descriptors
*/
vector<float> imageDescriptors(128 * nFeat);

sift_gpu_->GetFeatureVector(&keys[0], &imageDescriptors[0]);

sift_gpu_->SaveSIFT("1.sift");

//to opencv format
/** to opencv format
*/
keypoints.clear();
descriptors.create(0, 128, CV_32FC1);
for(int i = 0; i < nFeat; ++i) {
for(int i = 0; i < nFeat; ++i)
{
cv::KeyPoint key(keys[i].x, keys[i].y, keys[i].s, keys[i].o);
keypoints.push_back(key);
cv::Mat descriptor(1, 128, CV_32FC1);
Expand All @@ -187,10 +207,13 @@ namespace od
{
cv::Mat descriptors;
vector<cv::KeyPoint> keypoints;
if(mode_ == SIFT_GPU) {
if(mode_ == SIFT_GPU)
{
findSiftGPUDescriptors1(image, descriptors, keypoints);
sift_gpu_->SaveSIFT(path.c_str());
} else {
}
else
{
//DO NOTHING! IMPLEMENT LATER

}
Expand Down
42 changes: 35 additions & 7 deletions common/utils/ODFeatureDetector2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,51 @@ namespace od

mode_ = type;

if(type == SIFT) {
switch(type)
{
case SIFT:
feature_detector_ = cv::xfeatures2d::SIFT::create();
case ORB:
feature_detector_ = cv::ORB::create();
case SURF:
feature_detector_ = cv::xfeatures2d::SURF::create();
case ORB_GPU:
feature_detector_ = cv::cuda::ORB::create();
case SIFT_GPU:
sift_gpu_ = new SiftGPU;
char *argv[] = {(char *) "-fo", (char *) "-1", (char *) "-v", (char *) "3", (char *) "-cuda"};
int argc = sizeof(argv) / sizeof(char *);
sift_gpu_->ParseParam(argc, argv);
if(sift_gpu_->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
std::cout << "FATAL ERROR cannot create SIFTGPU context";
}

/*
if(type == SIFT)
{
feature_detector_ = cv::xfeatures2d::SIFT::create();
} else if(type == ORB) {
}
else if(type == ORB)
{
feature_detector_ = cv::ORB::create();
} else if(type == SURF) {
}
else if(type == SURF)
{
feature_detector_ = cv::xfeatures2d::SURF::create();
} else if(type == ORB_GPU) {
}
else if(type == ORB_GPU)
{
feature_detector_ = cv::cuda::ORB::create();
} else if(type == SIFT_GPU) {
}
else if(type == SIFT_GPU)
{
sift_gpu_ = new SiftGPU;
//char * argv[] = {(char *)"-fo", (char *)"-1", (char *)"-v", (char *)"1"};
char *argv[] = {(char *) "-fo", (char *) "-1", (char *) "-v", (char *) "3", (char *) "-cuda"};
int argc = sizeof(argv) / sizeof(char *);
sift_gpu_->ParseParam(argc, argv);
if(sift_gpu_->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
std::cout << "FATAL ERROR cannot create SIFTGPU context";
}
}*/
}

//always return Opencv type keypoints
Expand Down
20 changes: 11 additions & 9 deletions common/utils/ODFrameGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ namespace od
GENERATOR_TYPE_FILE_LIST, GENERATOR_TYPE_DEVICE
};



/** \brief The FrameGenerator class for capturing and reading Scenes conveniently.
* Templated with two parameters - SceneType identifying a Scene class, and TYPE identifying the type of input. After the instantiation with a correct TYPE, use the function
* getNextFrame() to get an instance of next scene of SceneType. getNextFrame() returns valid scenes until all scenes matched are exhausted - the time when 'isValid()' is false.
*
* \tparam SceneT One of the Scene classes - ODSceneImage or ODScenePointCloud
* \tparam TYPE TYPE can be GENERATOR_TYPE_FILE_LIST which means you provide the list of scene files to be returned by the FrameGenerator in the constructor (for eg. \/home/username/pics/*.jpg)
* Or it can be GENERATOR_TYPE_DEVICE which picks up the webcam or the kinect based on the SceneType.
* \author Kripasindhu Sarkar
*
*/
*
* \author Kripasindhu Sarkar
*
*/

template<typename SceneT, GeneratorType TYPE>
class ODFrameGenerator
{
Expand Down Expand Up @@ -146,7 +146,6 @@ namespace od
};
*/


template<>
class ODFrameGenerator<ODScenePointCloud<pcl::PointXYZRGBA> , GENERATOR_TYPE_DEVICE>
{
Expand All @@ -156,7 +155,8 @@ namespace od
typedef pcl::PointCloud<PointT>::Ptr PointCloudPtr;
typedef pcl::PointCloud<PointT>::ConstPtr PointCloudConstPtr;

/* A simple class for capturing data from an OpenNI camera */
/** A simple class for capturing data from an OpenNI camera
*/
ODFrameGenerator(std::string input = "") : grabber_(input), most_recent_frame_(), frame_counter_(0), active_(true)
{
boost::function<void(const PointCloudConstPtr&)> frame_cb = boost::bind (&ODFrameGenerator<ODScenePointCloud<pcl::PointXYZRGBA> , GENERATOR_TYPE_DEVICE>::onNewFrame, this, _1);
Expand All @@ -167,7 +167,8 @@ namespace od

~ODFrameGenerator()
{
// Stop the grabber when shutting down
/**Stop the grabber when shutting down
*/
grabber_.stop ();
}

Expand All @@ -191,7 +192,8 @@ namespace od
}
void onKeyboardEvent (const pcl::visualization::KeyboardEvent & event)
{
// When the spacebar is pressed, trigger a frame capture
/**When the spacebar is pressed, trigger a frame capture
*/
mutex_.lock ();
if (event.keyDown () && event.getKeySym () == "e")
{
Expand Down
Loading