我正在使用OpenCV对齐图像,图像对齐正常。我担心的是要花多长时间。之前对齐2张照片需要4.5-5秒。
但是遵循这些人的建议:
cv2.MOTION_EUCLIDEAN for the warp_mode in ECC image alignment method
我平均可以将其降低到3.3秒。在提供的链接中,他可以将时间从6秒减少到3秒。我无法像他那样将速度提高2倍。对于2张图像而言,3.3秒可能看起来并不多,但是我想在尽可能短的时间内对齐更多的图像。
Mat image1 = Imgcodecs.imread(fileImport);
Mat image2 = Imgcodecs.imread(fileImport2);
Mat image1Gray = new Mat();
Mat image2Gray = new Mat();
cvtColor(image1, image1Gray, Imgproc.COLOR_BGR2GRAY);
cvtColor(image2, image2Gray, Imgproc.COLOR_BGR2GRAY);
Mat warpMatrix = Mat.eye(3, 3, CV_32F);
int iterations = 5000;
double terminationEps = 1e-3;
TermCriteria criteria = new TermCriteria(COUNT + EPS, iterations, terminationEps);
int pyramids = 4;
for (int i = 1; i < pyramids; i++){
Mat resizedTemplate = new Mat();
Mat resizedImage = new Mat();
resize( image1Gray,
resizedTemplate,
new Size((double)image1Gray.width()*i/4,(double)image1Gray.height()*i/4),
(double)i/4,
(double)i/4,
INTER_AREA);
resize(image2Gray,
resizedImage,
new Size((double)image2Gray.width()*i/4,(double)image2Gray.height()*i/4),
(double)i/4,
(double)i/4,
INTER_AREA);
findTransformECC(resizedTemplate, resizedImage, warpMatrix, MOTION_HOMOGRAPHY, criteria, resizedImage, 1);
}
findTransformECC(image1Gray, image2Gray, warpMatrix, MOTION_HOMOGRAPHY, criteria, image2Gray, 1);
Mat image2Aligned = new Mat();
warpPerspective(image2,image2Aligned,warpMatrix, image1.size(),INTER_LINEAR + WARP_INVERSE_MAP);
Imgcodecs.imwrite(fileExport, image2Aligned);