在 C++ 中计算相似图像的偏移/倾斜/旋转

发布于 2024-11-18 03:13:48 字数 273 浏览 4 评论 0原文

我同时拍摄了多张图像,从同一起始位置指向同一方向。然而,仍然存在轻微的偏移,因为拍摄照片时这些相机并不位于完全相同的位置。我正在寻找一种方法来计算将一个图像与另一个图像匹配所需的最佳平移/剪切/倾斜/旋转,以便它们(几乎)完美地重叠。

这些图像采用 .raw 格式,我一次读取 16 位。

我被建议(由我的不是程序员的雇主[顺便说一句,我是实习生])获取源图像的​​一部分(不在边缘)并强力搜索具有高像素的相同大小的部分。数据值的相关性。我希望有一种更少浪费的算法。

I have multiple images taken simultaneously pointing at the same direction from the same starting location. However, there is still a slight offset because these cameras were not in the exact same place when the picture was taking. I'm looking for a way to calculate the optimal translation/shear/skew/rotation needed to apply to match one image to another so that they overlay (almost) perfectly.

The images are in a .raw format which I am reading in 16 bits at a time.

I have been suggested (by my employer who is not a programmer [I'm an intern btw]) to take a portion of the source image (not at the edges) and brute-force search for a same-sized portion with a high correlation in data values. I'm hoping there is a less-wasteful algorithm.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他不在意 2024-11-25 03:13:48

这是一个简短的代码,可以实现您想要的功能(我使用 openCV 2.2):

  1. 假设您有 2 个图像:srcImage,dstImage,并且您想要对齐它们
  2. 代码非常简单。将其用作算法的基础。

代码:

// Detect special points on each image that can be corresponded    
Ptr<FeatureDetector>  detector = new SurfFeatureDetector(2000);  // Detector for features

vector<KeyPoint> srcFeatures;   // Detected key points on first image
vector<KeyPoint> dstFeatures;
detector->detect(srcImage,srcFeatures);
detector->detect(dstImage,dstFeatures); 

// Extract descriptors of the 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. It is better
vector<DMatch> matches;
matcher.match(srcDescriptors, dstDescriptors, matches);     

// Extract pairs of points
vector<int> pairOfsrcKP(matches.size()), pairOfdstKP(matches.size());
for( size_t i = 0; i < 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. Those pairs will be used to 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 the result (only for 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) to dst (right)", outimg);
   cvWaitKey(0);
}

// Now you have the resulting homography. I mean that:  H(srcImage) is alligned to dstImage. Apply H using the 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);

Here is a short code that does what you want (I use openCV 2.2):

  1. Suppose you have 2 images: srcImage,dstImage, and you want to align them
  2. The code is very simple. Use it as basis for your algorithm.

Code:

// Detect special points on each image that can be corresponded    
Ptr<FeatureDetector>  detector = new SurfFeatureDetector(2000);  // Detector for features

vector<KeyPoint> srcFeatures;   // Detected key points on first image
vector<KeyPoint> dstFeatures;
detector->detect(srcImage,srcFeatures);
detector->detect(dstImage,dstFeatures); 

// Extract descriptors of the 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. It is better
vector<DMatch> matches;
matcher.match(srcDescriptors, dstDescriptors, matches);     

// Extract pairs of points
vector<int> pairOfsrcKP(matches.size()), pairOfdstKP(matches.size());
for( size_t i = 0; i < 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. Those pairs will be used to 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 the result (only for 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) to dst (right)", outimg);
   cvWaitKey(0);
}

// Now you have the resulting homography. I mean that:  H(srcImage) is alligned to dstImage. Apply H using the 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);
孤凫 2024-11-25 03:13:48

图像是否是从相同的位置站立拍摄的,但只是旋转了一点,导致它们没有正确对齐?如果是这样,那么图像通过 同应性 相关 - 即投影变换。给定图像之间的一组对应关系(至少需要 4 对),查找单应性的标准方法是使用 DLT 算法

Are the images taken standing from the same position but you're just rotated a bit so that they're not aligned correctly? If so then the images are related by a homography - i.e. a projective transformation. Given a set of correspondences between the images (you need at least 4 pairs), the standard way to find the homography is to use the DLT algorithm.

时光清浅 2024-11-25 03:13:48

使用以下代码避免链接器错误:

#include "cv.h"
#include "highgui.h"
using namespace cv;

// Directives to linker to include openCV lib files.
#pragma comment(lib, "opencv_core220.lib") 
#pragma comment(lib, "opencv_highgui220.lib") 
#pragma comment(lib, "opencv_contrib220.lib") 
#pragma comment(lib, "opencv_imgproc220.lib") 
#pragma comment(lib, "opencv_gpu220.lib") 
#pragma comment(lib, "opencv_video220.lib") 
#pragma comment(lib, "opencv_legacy220.lib") 

#pragma comment(lib, "opencv_ml220.lib") 
#pragma comment(lib, "opencv_objdetect220.lib") 
#pragma comment(lib, "opencv_ffmpeg220.lib") 

#pragma comment(lib, "opencv_flann220.lib") 
#pragma comment(lib, "opencv_features2d220.lib") 
#pragma comment(lib, "opencv_calib3d220.lib") 

// Your code here...
int main(void){
    Mat B = Mat:eye(3,3,CV_8U);
    return -1;
}

Avoid linker errors using the below code:

#include "cv.h"
#include "highgui.h"
using namespace cv;

// Directives to linker to include openCV lib files.
#pragma comment(lib, "opencv_core220.lib") 
#pragma comment(lib, "opencv_highgui220.lib") 
#pragma comment(lib, "opencv_contrib220.lib") 
#pragma comment(lib, "opencv_imgproc220.lib") 
#pragma comment(lib, "opencv_gpu220.lib") 
#pragma comment(lib, "opencv_video220.lib") 
#pragma comment(lib, "opencv_legacy220.lib") 

#pragma comment(lib, "opencv_ml220.lib") 
#pragma comment(lib, "opencv_objdetect220.lib") 
#pragma comment(lib, "opencv_ffmpeg220.lib") 

#pragma comment(lib, "opencv_flann220.lib") 
#pragma comment(lib, "opencv_features2d220.lib") 
#pragma comment(lib, "opencv_calib3d220.lib") 

// Your code here...
int main(void){
    Mat B = Mat:eye(3,3,CV_8U);
    return -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文