I am currently trying to fuse radiological greyscale images using java and the OpenCV wrapper. I wrote some code to find alike images in a database and fuse them. The fusion part is where I am struggling.This is the method I am struggling with:
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();
}
}
Both the mat2BufferedImage()
and the getGoodMatches()
have been tested and seem to be valid. There seems to be something wrong with findHomography()
and warpPerspective()
, since this is the result when I view the transformed (not fusioned) image: https://i.imgur.com/8JRQwtG.png
Does someone have an idea what went wrong? Thank you in advance!
EDIT: So after further investigating, it turns out the transformation-matrix has an extremly strong perspective transformation (values from 400-700). Since the images are pretty identical/have only small differences, I don't understand why this is the result of findHomography()
. Are there alternatives to this method?
I'd use drawMatches()
to verify there is no outliers in goodMatches
. The outliers can completely mess up the computed homography. If you get outliers then you need to use the robust variant of findHomography()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.