我正在尝试将对象图像找到场景图像中。我使用opencv来检测使用SURF的关键点。我使用matchDescriptors来匹配对象描述符和场景描述符,但这总是在找到的对象中产生。有人可以帮忙解释为什么会这样吗?代码有点乱,因为我尝试了很多我发现的选项,但问题仍然存在。
Mat objectImage = Highgui.imread(Object, Highgui.CV_LOAD_IMAGE_COLOR);
Mat sceneImage = Highgui.imread(Scene, Highgui.CV_LOAD_IMAGE_COLOR);
MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
//System.out.println("Detecting key points...");
featureDetector.detect(objectImage, objectKeyPoints);
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
keypoints_object = objectKeyPoints;
System.out.println("Object key points : "+keypoints_object.total());
Mat objectDescriptors = new Mat();
DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
//System.out.println("Computing descriptors...");
descriptorExtractor.compute(objectImage, objectKeyPoints, objectDescriptors);
// Create the matrix for output image.
Mat outputImage = new Mat(objectImage.rows(), objectImage.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
Scalar newKeypointColor = new Scalar(255, 0, 0);
//Drawing key points on object image
Features2d.drawKeypoints(objectImage, objectKeyPoints, outputImage, newKeypointColor, 0);
MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
//System.out.println("Detecting key points in background image...");
featureDetector.detect(sceneImage, sceneKeyPoints);
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
keypoints_scene = sceneKeyPoints;
System.out.println("Scene Key points : "+keypoints_scene.total());
//System.out.println("Computing descriptors in background image...");
Mat sceneDescriptors = new Mat();
descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);
//SceneImage output
Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
Scalar matchestColor = new Scalar(0, 255, 0);
// Drawing key points on scene image;
Mat sceneImg = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
Scalar sceneColor = new Scalar(0,0, 255);
Features2d.drawKeypoints(sceneImage, sceneKeyPoints, sceneImg, sceneColor, 0);
System.out.println("Matching object and scene images...");
DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
descriptorMatcher.clear();
MatOfDMatch matches = new MatOfDMatch();
descriptorMatcher.match(objectDescriptors, sceneDescriptors, matches);
List<DMatch> matchesList = matches.toList();
//descriptorMatcher.knnMatch(objectDescriptors, sceneDescriptors, matches,3);
//System.out.println("Calculating good match list...");
Double max_dist = 0.0;
Double min_dist = 100.0;
for(int i = 0; i < objectDescriptors.rows(); i++){
Double dist = (double) matchesList.get(i).distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();
for(int i = 0; i < objectDescriptors.rows(); i++){
if(matchesList.get(i).distance < 1.4*min_dist){
goodMatchesList.addLast(matchesList.get(i));
}
}
System.out.println("-- "+goodMatchesList.size());
if (goodMatchesList.size() >= 7) {
System.out.println("Object Found!!!");
}
对象图片:https://i.stack.imgur.com/OmY3o.jpg场景 图片:https://i.stack.imgur.com/3wIuq.jpg结果 图像:https://i.stack.imgur.com/yZcqW.jpg