我目前正在尝试使用 java 和 OpenCV 包装器融合放射学灰度图像。我写了一些代码来在数据库中找到相似的图像并融合它们。融合部分是我挣扎的地方。这是我挣扎的方法:
public BufferedImage registerImages(BufferedImage source, BufferedImage target)
throws RegistrationException{
LinkedList<DMatch> goodMatches = getGoodMatches(source, target);
if(goodMatches.size() >= 7){
List<KeyPoint> sourceKeypoints = sourceKeyPointsMat.toList();
List<KeyPoint> targetKeypoints = targetKeyPointsMat.toList();
LinkedList<Point> sourcePoints = new LinkedList<>();
LinkedList<Point> targetPoints = new LinkedList<>();
for(int i = 0; i < goodMatches.size(); i++) {
sourcePoints.addLast(sourceKeypoints.get(goodMatches.get(i).queryIdx).pt);
targetPoints.addLast(targetKeypoints.get(goodMatches.get(i).trainIdx).pt);
}
MatOfPoint2f sourceMatOfPoint2f = new MatOfPoint2f();
sourceMatOfPoint2f.fromList(sourcePoints);
MatOfPoint2f targetMatOfPoint2f = new MatOfPoint2f();
targetMatOfPoint2f.fromList(targetPoints);
Mat homography = Calib3d.findHomography(sourceMatOfPoint2f, targetMatOfPoint2f);
Mat transformationResult = new Mat(sourceImageMat.rows(), sourceImageMat.cols(), sourceImageMat.type());
Imgproc.warpPerspective(sourceImageMat, transformationResult, homography, transformationResult.size());
Mat resultImage = new Mat();
Core.add(transformationResult, targetImageMat, resultImage);
return mat2BufferedImage(resultImage);
}
else{
throw new RegistrationException();
}
}
mat2BufferedImage()
和 getGoodMatches()
都已经过测试,似乎是有效的。 findHomography()
和 warpPerspective()
似乎有问题,因为这是我查看转换后(未融合)图像时的结果:https://i.imgur.com/8JRQwtG.png
有人知道哪里出了问题吗?提前致谢!
编辑:所以在进一步调查之后,事实证明变换矩阵具有极强的透视变换(值从 400-700)。由于图像非常相同/只有很小的差异,我不明白为什么这是 findHomography()
的结果。这种方法有替代方法吗?
最佳答案
我会使用 drawMatches()
验证 goodMatches
中没有异常值。异常值可以完全搞乱计算的单应性。如果你得到异常值,那么你需要使用 findHomography()
的强大变体。
关于java - 使用java和opencv对灰度图像进行图像融合给出了奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49509502/