c# - 3D网格表面

标签 c# bitmap 3d

我有以下代码在 2D 插值彩色 map 中显示我的传感器数据。但是,我想将其转换为 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);
        }
    }

}

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

最佳答案

如果像素颜色是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)];

关于c# - 3D网格表面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34586919/

相关文章:

c# - 在 iOS 中使用 WKWebView 请求桌面站点

c# - 以 MVVM 模式更新记录以获得最大效率的正确方法

image - 如何从 JavaFX 中的剪贴板正确获取图像;有问题的应用程序和没有问题的应用程序有什么区别?

android - 在 NetworkOnMainThreadException 中将 url 转换为位图

c++ - 倾斜的平截头体不扭曲场景

android - Android OpenGLES 光照问题

c# - EntityFramework 插入速度非常慢,数据量大

c# - 检查 Entity Framework linq 结果中的项目

java - 向位图添加虚线边框

c++ - 使用 gnuplot 制作 3D 曲面图