3D网格表面

时间:2016-01-04 08:06:05

标签: c# bitmap 3d

我有以下代码在2D插值颜色图中显示我的传感器数据。但是,我想将其转换为3D网格表面。我只能找到一些复杂的方法来做到这一点。但我想的是一种更简单的方法,可以根据每个数据阵列上的不同颜色将2D色彩图拉到更高的水平,从而使2D位图像3D一样

2D and 3D bitmap

Bitmap bmp3 = new Bitmap(PicBoxChi.Width, PicBoxChi.Height);
using (Graphics g = Graphics.FromImage(bmp3))
{
    // this loop create a 6X4 map to show my sensor data
    for (int a = 0; a < 6; a++)
    {
        for (int b = 0; b < 4; b++)
        {
            pts[a, b] = new PointC(new PointF(x0 + b*width, y0 + a*height), data[a*4+b+48] ); 
        }
    }


    int colorLength = cmap.GetLength(0);
    // Bilinear interpolation:

    // this loop transfer my 6x4 sensor data map to a color map
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            PointF[] pta = new PointF[4]{pts[i,j].pointf, pts[i+1,j].pointf,
            pts[i+1,j+1].pointf, pts[i,j+1].pointf};
            float[] cdata = new float[4]{pts[i,j].C,pts[i+1,j].C,
            pts[i+1,j+1].C,pts[i,j+1].C};
            Interp(g, pta, cdata, 50);
        }
    }

}

我现在可以通过上面的代码创建2D颜色位图。但我不知道如图所示制作3D。

1 个答案:

答案 0 :(得分:0)

如果像素颜色是RGB格式,从提供的图片我可以推断出你感兴趣的是颜色的纯色调。

要从RGB获得标准[0,360]范围的色调值,您可以执行this

System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
float hue = color.GetHue();

或更可定制的方法(here in Java,也在此处报告):

public int getHue(int red, int green, int blue) {

    float min = Math.min(Math.min(red, green), blue);
    float max = Math.max(Math.max(red, green), blue);

    float hue = 0f;
    if (max == red) {
        hue = (green - blue) / (max - min);

    } else if (max == green) {
        hue = 2f + (blue - red) / (max - min);

    } else {
        hue = 4f + (red - green) / (max - min);
    }

    hue = hue * 60;
    if (hue < 0) hue = hue + 360;

    return Math.round(hue);
}

然后,您可以简单地将结果值用作相应顶点的高度,可以选择通过将其除以/乘以某个常数来微调它。

生成3D网格相对简单,我认为你已经有了代码,无论如何都是伪代码:

float getHeight(img img, int x, int y) {
    float scalefactor = 1.0/360; //any value you like
    return getHue(getRgbColorFromImage(img, x, y)) * scalefactor;
}


int x=0, y=0;
for (y=0; y<imageHeight-1; y++) {
    for (x=0; x<imageWidth-1; x++) {
        new quad = [x, y, getHeight(img, x, y),
                    x+1, y, getHeight(img, x+1, y),
                    x, y+1, getHeight(img, x, y+1),
                    x+1, y+1, getHeight(img, x+1, y+1)];
    }
}

如果你想要三角形:

new triangle = [x, y, getHeight(img, x, y),
                x+1, y, getHeight(img, x+1, y),
                x, y+1, getHeight(img, x, y+1)];
new triangle = [x+1, y, getHeight(img, x+1, y),
                x, y+1, getHeight(img, x, y+1),
                x+1, y+1, getHeight(img, x+1, y+1)];