我有两张照片,一张是放大照片,第二张是大广角照片。将第一幅图像视为第二幅图像的裁剪(尽管事实并非如此)。我从每张照片中选择一个像素,该像素代表两张照片中的相同事物(例如一个男人的 Nose )。我将第一张图片(具有一定的透明度)叠加在第二张图片的顶部,对齐两个选定的像素?
现在我想缩放第二个图像,直到第一个图像基本上消失,因为第二个图像的比例与第一个图像的比例相匹配。
我正在使用 OpenImaj 并已使用 MBFImage.overlayInPlace() 成功完成此操作。我遇到的问题是,当我使用 ResizeProcessor 缩放第二个图像时,如果我必须将第二个图像缩放太多(>5x),则会收到“OutOfMemoryError:Java Heap Space”。
我已使用 JDK 12 64 位将 -Xmx 提高到 12G。 我尝试过 ResizeProcessor、BilinearInterpolation 和 BicubicInterpolation 调整器,结果都相同。
这是我最近尝试的方法。根据我给 JVM 分配的内存量,它有时会在调整大小时失败,有时会在 toRGB() 上失败:
private MBFImage scaleImage2(float scaleFactor) throws IOException {
FImage img2Flat = image2.flatten();
int width = (int)Math.round(img2Flat.getWidth() * scaleFactor);
int height = (int)Math.round(img2Flat.getHeight() * scaleFactor);
BilinearInterpolation resizer = new BilinearInterpolation(width, height, 1/scaleFactor);
resizer.processImage(img2Flat);
return img2Flat.toRGB();
}
这是我叠加图像的位置:
private MBFImage createScaledOverlay() {
MBFImage scaledImg2 = null;
if(scaledLastCrop == null) {
try {
scaledLastCrop = scaleLastCrop();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
scaledImg2 = scaleImage2(image2ScaleFactor);
} catch (IOException e) {
e.printStackTrace();
}
int img2ScaledPixelx = Math.round(image2SelectedPixel.x * image2ScaleFactor);
int img2ScaledPixely = Math.round(image2SelectedPixel.y * image2ScaleFactor);
int lastCropScaledPixelx = (int)Math.round(lastCropSelectedPixel.x * lastCropScaleFactor);
int lastCropScaledPixely = (int)Math.round(lastCropSelectedPixel.y * lastCropScaleFactor);
scaledImg2.overlayInplace(scaledLastCrop, img2ScaledPixelx - lastCropScaledPixelx, img2ScaledPixely - lastCropScaledPixely);
return scaledImg2;
}
我同意两条前进道路之一:
- 修复 OutOfMemory 错误
- 另一种叠加两个图像的方法
最佳答案
有时,解决方案是如此明显,以至于令人尴尬。
现在想想,我真正的目标是找到两个图像之间的比例因子。我可以缩小 image1,而不是放大 image2。这对内存的消耗要小得多。然后对于 image2 我可以反转这个比例因子。
关于java - 调整大小过程中 OpenImaj OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912759/