Skip to content

Instantly share code, notes, and snippets.

@thorikawa
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thorikawa/feab607cb4397a6d3901 to your computer and use it in GitHub Desktop.
Save thorikawa/feab607cb4397a6d3901 to your computer and use it in GitHub Desktop.
Test code for ObjectnessBING
#include <opencv2/opencv.hpp>
#include <opencv2/saliency.hpp>
using namespace cv;
using namespace std;
using namespace saliency;
bool myfunction (int i, int j) { return (i<j); }
int main (int argc, const char * argv[]) {
if (argc < 3) {
printf("Usage: bingtest <image_file> <training_path>\n");
printf(" <training_path> should point to opencv_contrib/modules/saliency/samples/ObjectnessTrainedModel or the directory where your own trained models are stored.\n");
exit(-1);
}
Mat image = imread(argv[1]);
string training_path = argv[2];
imshow("original", image);
Ptr<ObjectnessBING> objectnessBING = makePtr<ObjectnessBING>();
objectnessBING->setTrainingPath(training_path);
objectnessBING->setBBResDir(training_path + "/Results");
int a = rand()*100;
vector<Vec4i> objectnessBoundingBox;
if (objectnessBING->computeSaliency(image, objectnessBoundingBox) ) {
vector<float> values = objectnessBING->getobjectnessValues();
printf("detected candidates: %d\n", objectnessBoundingBox.size());
printf("scores: %d\n", values.size());
// The result are sorted by objectness. We uonly use the first 20 boxes here.
for (int i = 0; i < 20; i++) {
Mat clone = image.clone();
Vec4i bb = objectnessBoundingBox[i];
printf("index=%d, value=%f\n", i, values[i]);
rectangle(clone, Point(bb[0], bb[1]), Point(bb[2], bb[3]), Scalar(0, 0, 255), 4);
char label[256];
sprintf(label, "#%d", i+1);
putText(clone, label, Point(bb[0], bb[1]+30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
char filename[256];
sprintf(filename, "bing_%05d.jpg", i);
imwrite(filename, clone);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment