iOS利⽤opencv库拼接图⽚的另⼀种⽅法
问题说明
在Xcode9中,如果直接将图⽚等⽂件拖拽进项⽬中,可能会识别不到。这时候,我们通过Add Files to xxx的⽅式来进⾏添加。
项⽬⽬录⽂件结构
image
主要代码
⼀、合成代码
#include "opencv2.framework/Headers/opencv.hpp"
#include "opencv2.framework/Headers/legacy/legacy.hpp"
#include "opencv2.framework/Headers/nonfree/nonfree.hpp"
#include <vector>
#include <iostream>
#include <iostream>
普京演讲using namespace std;
vriousing namespace cv;
pandemic
/
/计算原始图像点位在经过矩阵变换后在⽬标图像上对应位置
abstentionPoint2f getTransformPoint(const Point2f originalPoint,const Mat &transformMaxtri){
Mat originelP,targetP;
originelP=(Mat_<double>(3,1)<<originalPoint.x,originalPoint.y,1.0);
targetP=transformMaxtri*originelP;
float x=targetP.at<double>(0,0)/targetP.at<double>(2,0);
float y=targetP.at<double>(1,0)/targetP.at<double>(2,0);担心的英语
return Point2f(x,y);
}
- (UIImage *)compoImage{
NSString *path01 = [[NSBundle mainBundle] pathForResource:@"test01" ofType:@"jpg"]; NSStrin
g *path02 = [[NSBundle mainBundle] pathForResource:@"test02" ofType:@"jpg"]; Mat img01;
Mat img02;
if (path01 == nil && path02 == nil) {
return [UIImage new];
}
el{
img01 = imread([path01 UTF8String]);
img02 = imread([path02 UTF8String]);
//如果没有读取到image
if (!img01.data && !img02.data) {
return [UIImage new];
}悉尼大学官网
//灰度图转换
Mat img_h_01 ,img_h_02;
cvtColor(img01, img_h_01, CV_RGB2GRAY);
cvtColor(img02, img_h_02, CV_RGB2GRAY);
//提取特征点
SiftFeatureDetector siftDetector(800);
vector<KeyPoint> keyPoint1,KeyPoint2;
siftDetector.detect(img_h_01, keyPoint1);
siftDetector.detect(img_h_02, KeyPoint2);
//特征点描述,为下⾯的特征点匹配做准备
SiftDescriptorExtractor siftDescriptor;
Mat img_description_01,img_description_02;
//获得匹配特征点,并提取最优配对
FlannBadMatcher matcher;
vector<DMatch> matchePoints;
matcher.match(img_description_01,img_description_02,matchePoints,Mat());追女孩子打电话技巧
sort(matchePoints.begin(), d());//特征点排序
//获取排在前N个的最优配对
vector<Point2f> imagePoints1,imagePoints2;
pioneer是什么意思
for (int i = 0; i < 10; i++) {
imagePoints1.push_back(keyPoint1[matchePoints[i].queryIdx].pt);
imagePoints2.push_back(KeyPoint2[matchePoints[i].trainIdx].pt);
}
//获取img1到img2的投影映射矩阵,尺⼨为3*3
Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
义乌英语培训Mat adjustMat = (Mat_<double>(3,3)<<1.0,ls,0,1.0,0,0,0,1.0);
Mat adjustHomo = adjustMat * homo;
//获得最强配对点在原始图像和矩阵变换后图像上的对应位置,⽤于图像拼接点的定位
Point2f originalLinkPoint,targetLintPoint,badImagePoint;
originalLinkPoint = keyPoint1[matchePoints[0].queryIdx].pt;
targetLintPoint = getTransformPoint(originalLinkPoint, adjustHomo);
badImagePoint = KeyPoint2[matchePoints[0].trainIdx].pt;