调整大小过程中发生java OpenImaj OutOfMemoryError
我有两张照片,一张放大了,第二张是大的广角照片。把第一幅图像看成是第二幅的图像(虽然不是)。我从每张照片中选择一个像素,在两张照片中都代表相同的东西(比如一个男人的鼻子)。我将第一张图片覆盖在第二张图片的顶部,使两个选定的像素对齐
现在我想缩放第二个图像,直到第一个图像基本消失,因为第二个图像的比例与第一个图像的比例匹配
我正在使用OpenImaj,并已使用MBFImage成功地完成了这项工作。覆盖位置()。我遇到的问题是,当我使用ResizeProcessor缩放第二个映像时,如果必须将第二个映像缩放太大(>;5x),我会得到一个“OutOfMemoryError:Java堆空间”
我使用JDK 12 64位将-Xmx升级到12G。 我尝试过ResizeProcessor、双线性插值和双三次插值调整器,都得到了相同的结果
这是我最新尝试的方法。根据我给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;
}
我同意两条前进道路中的一条:
- 修复OutOfMemoryError
- 另一种覆盖两幅图像的方法
# 1 楼答案
有时一个解决方案是如此明显,令人尴尬
现在我想起来了,我真正的目标是找到两幅图像之间的比例因子。我可以缩小image1,而不是放大image2。这大大减少了内存占用。然后关于图2,我可以反转这个比例因子