Java 插值图像像素

Java 插值图像像素,java,image,interpolation,Java,Image,Interpolation,我想为我的3D引擎采样一个高度贴图。地形上的顶点可能比图像中的像素多。我创建了以下方法: public float interpolateFromImage(buffereImage图像、float x、float y){ 这将获取图像以及x和y坐标。坐标以百分比表示。 我的方法是计算附近像素和给定坐标之间的距离。这很好,但我最终得到了以下结果: 可以清楚地看到,边缘并不像应该的那样光滑 我使用了以下代码: int topLeftX = (int) (x * (image.getWi

我想为我的3D引擎采样一个高度贴图。地形上的顶点可能比图像中的像素多。我创建了以下方法:

public float interpolateFromImage(buffereImage图像、float x、float y){

这将获取图像以及x和y坐标。坐标以百分比表示。 我的方法是计算附近像素和给定坐标之间的距离。这很好,但我最终得到了以下结果:

可以清楚地看到,边缘并不像应该的那样光滑

我使用了以下代码:

    int topLeftX = (int) (x * (image.getWidth()-1));      //index of topLeft pixel
    int topLeftY = (int) (y * (image.getHeight()-1));     //index of topLeft pixel

    float[] distances = new float[4];  
    float total = 0;
    for(int j = topLeftX ; j < topLeftX + 2; j++){           //run through all 4 nearby pixels
        for(int k = topLeftY ; k < topLeftY + 2; k++){
            float dist = (float) Math.sqrt(                    //pythagoras for the distance
                    (x- j/ (float)(image.getWidth()-1)) *
                            (x- j/ (float)(image.getWidth()-1)) +
                            (y- k/ (float)(image.getHeight()-1)) *
                                    (y- k/ (float)(image.getHeight()-1)));
            if(dist < 0.001){
                return new Color(image.getRGB(j,k)).getRed();
            }
            distances[(j-topLeftX) * 2 + (k-topLeftY)] = (1f / image.getWidth()) / (dist * dist);
            total += distances[(j-topLeftX) * 2 + (k-topLeftY)];
        }
    }
    float h = 0;
    for(int j = topLeftX ; j < topLeftX + 2; j++){
        for(int k = topLeftY ; k < topLeftY + 2; k++){
            float p = distances[(j-topLeftX) * 2 + (k-topLeftY)] / total;
            h+= new Color(image.getRGB(j,k)).getRed() * p;
        }
    }
    return h;
inttopleftx=(int)(x*(image.getWidth()-1));//左上角像素的索引
int topLeftY=(int)(y*(image.getHeight()-1));//左上角像素的索引
浮动[]距离=新浮动[4];
浮动总数=0;
对于(int j=topLeftX;j
有人知道我需要如何更改代码吗?
我很高兴得到任何建议和帮助:)

您正在根据所有四个角的平方反比距离对颜色进行加权。这里的问题是:边缘上的像素颜色受交叉角颜色的影响。边缘两侧的像素颜色不同,因为它们是从不同的f集合计算出来的我们的角落


解决方法是使用一些常用的插值,如双线性插值或双三次插值。

Mhm是的……我知道为什么会这样:)我想用我自己做的一些东西。似乎不是最好的主意。谢谢!:)你可以继续实验,这是最好的学习方法。