Skip to content

Instantly share code, notes, and snippets.

@panovr
Created March 29, 2014 03:54
Show Gist options
  • Save panovr/9848040 to your computer and use it in GitHub Desktop.
Save panovr/9848040 to your computer and use it in GitHub Desktop.
ReadImages
void ReadImages(vector<PanoImage> *pano_images);
int main(int argc, char **argv)
{
vector <PanoImage> pano_images;
ReadImages(&pano_images);
// Some error checking
for (size_t i = 0; i < pano_images.size(); ++i)
{
if (pano_images[i].img.empty())
{
fprintf(stderr, "Can't read image %lu\n", i+1);
return 1;
}
}
void ReadImages(vector<PanoImage> *pano_images)
{
FILE* f = fopen("sample/info.txt", "r");
printf("Reading input image info ...\n");
int nImages;
int width, height;
fscanf(f, "%d%d%d", &width, &height, &nImages);
printf("\t# of input images: %d\n\tsize: %d x %d\n", nImages, width, height);
// Initialise some camera intrinsics for each image
// We'll just assume the optical centre is at the centre of the image.
// This works good enough for me in practice.
G_CX = width / 2;
G_CY = height / 2;
for (int i = 0; i < nImages; ++i)
{
char name[50];
double focalLength;
fscanf(f, "%s%lf", name, &focalLength);
printf("\tinput image: %s (f: %f)\n", name, focalLength);
PanoImage pimg;
pimg.img = imread(name);
pimg.focal = focalLength;
pimg.cx = G_CX;
pimg.cy = G_CY;
pano_images->push_back(pimg);
}
fclose(f);
}
void FindMatches(const Mat &img1, const Mat &img2, vector<MatchPair> *ret_matches)
{
Mat grey1, grey2;
cvtColor(img1, grey1, CV_BGR2GRAY);
cvtColor(img2, grey2, CV_BGR2GRAY);
// Detecting keypoints
Ptr<FeatureDetector> detector = FeatureDetector::create("SURF");
Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor::create("SURF");
Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create("FlannBased");
vector<KeyPoint> keypoints1, keypoints2;
detector->detect(grey1, keypoints1);
detector->detect(grey2, keypoints2);
// Computing descriptors
Mat descriptors1, descriptors2;
descriptorExtractor->compute(grey1, keypoints1, descriptors1);
descriptorExtractor->compute(grey2, keypoints2, descriptors2);
// Matching descriptors
vector<DMatch> matches;
descriptorMatcher->match(descriptors1, descriptors2, matches);
ret_matches->clear();
for (size_t i = 0; i < matches.size(); ++i)
{
int i1 = matches[i].queryIdx;
int i2 = matches[i].trainIdx;
const KeyPoint &kp1 = keypoints1[i1];
const KeyPoint &kp2 = keypoints2[i2];
// This is ad-hoc, adjust to suit your need
if (matches[i].distance < 0.20)
{
ret_matches->push_back(MatchPair(kp1.pt, kp2.pt));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment