0

我有两张照片,一张是放大的,第二张是大的广角照片。考虑第一个图像是第二个图像的裁剪(尽管它不是)。我从每张照片中选择一个像素,代表两张照片中的相同事物(比如一个男人的鼻子)。我将第一张图片以一定的透明度覆盖在第二张图片的顶部,对齐两个选定的像素?

现在我想缩放第二张图像,直到第一张图像基本上消失,因为第二张图像的比例与第一张图像的比例匹配。

我正在使用 OpenImaj,并使用 MBFImage.overlayInPlace() 成功完成了这项工作。我遇到的问题是,当我使用 ResizeProcessor 缩放第二个图像时,如果我必须将第二个图像缩放太多( >5x ),我会得到一个“OutOfMemoryError: Java Heap Space”。

我已经使用 JDK 12 64bit 将 -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;
}

我可以选择两条前进的道路之一:

  1. 修复 OutOfMemoryError
  2. 叠加两个图像的另一种方法
4

1 回答 1

0

有时一个解决方案是如此明显以至于令人尴尬。

现在想想,我真正的目标是找到两个图像之间的比例因子。我可以缩小 image1,而不是放大 image2。这要少得多的内存密集型。然后关于 image2 我可以反转这个比例因子。

于 2019-05-02T01:30:30.193 回答