Calculate offset/skew/rotation of similar images in C++ -
i have multiple images taken simultaneously pointing @ same direction same starting location. however, there still slight offset because these cameras not in exact same place when picture taking. i'm looking way calculate optimal translation/shear/skew/rotation needed apply match 1 image overlay (almost) perfectly.
the images in .raw format reading in 16 bits @ time.
i have been suggested (by employer not programmer [i'm intern btw]) take portion of source image (not @ edges) , brute-force search same-sized portion high correlation in data values. i'm hoping there less-wasteful algorithm.
here short code want (i use opencv 2.2):
- suppose have 2 images: srcimage,dstimage, , want align them
- the code simple. use basis algorithm.
code:
// detect special points on each image can corresponded ptr<featuredetector> detector = new surffeaturedetector(2000); // detector features vector<keypoint> srcfeatures; // detected key points on first image vector<keypoint> dstfeatures; detector->detect(srcimage,srcfeatures); detector->detect(dstimage,dstfeatures); // extract descriptors of features surfdescriptorextractor extractor; mat projdescriptors, camdescriptors; extractor.compute(srcimage, srcfeatures, srcdescriptors); extractor.compute(dstimage , dstfeatures, dstdescriptors ); // match descriptors of 2 images (find pairs of corresponding points) bruteforcematcher<l2<float>> matcher; // use flannbasedmatcher matcher. better vector<dmatch> matches; matcher.match(srcdescriptors, dstdescriptors, matches); // extract pairs of points vector<int> pairofsrckp(matches.size()), pairofdstkp(matches.size()); for( size_t = 0; < matches.size(); i++ ){ pairofsrckp[i] = matches[i].queryidx; pairofdstkp[i] = matches[i].trainidx; } vector<point2f> spoints; keypoint::convert(srcfeatures, spoints,pairofsrckp); vector<point2f> dpoints; keypoint::convert(dstfeatures, dpoints,pairofdstkp); // matched pairs of 2d points. pairs used calculate homography mat src2dfeatures; mat dst2dfeatures; mat(spoints).copyto(src2dfeatures); mat(dpoints).copyto(dst2dfeatures); // calculate homography vector<uchar> outliermask; mat h; h = findhomography( src2dfeatures, dst2dfeatures, outliermask, ransac, 3); // show result (only debug) if (debug){ mat outimg; drawmatches(srcimage, srcfeatures,dstimage, dstfeatures, matches, outimg, scalar::all(-1), scalar::all(-1), reinterpret_cast<const vector<char>&> (outliermask)); imshow("matches: src image (left) dst (right)", outimg); cvwaitkey(0); } // have resulting homography. mean that: h(srcimage) alligned dstimage. apply h using below code mat alignedsrcimage; warpperspective(srcimage,alignedsrcimage,h,dstimage.size(),inter_linear,border_constant); mat aligneddstimagetosrc; warpperspective(dstimage,aligneddstimagetosrc,h.inv(),srcimage.size(),inter_linear,border_constant);
Comments
Post a Comment