gpt4 book ai didi

java - 在 Java 中将彩色图像转换为 3D 冲浪图

转载 作者:行者123 更新时间:2023-12-01 12:50:20 25 4
gpt4 key购买 nike

我有一个图像,它使用暖色和冷色来表示 2D 空间中的深度。我希望能够将其绘制为 3D 曲面图,就像您在 Matlab 中所做的那样。我一直在玩 jzy3d 但我是 java 新手,所以我在做这件事时遇到了麻烦。理想情况下,就像在 Matlab 中一样,最好的方法是将其转换为灰度,然后绘制它,但我不知道如何做,所以任何帮助将不胜感激。

提前谢谢您。

最佳答案

您应该更详细地描述实际困难是什么。

但是,我很好奇(无论如何都想尝试一下 jzy3d)。所以我修改了SurfaceDemo.java用于加载图像并绘制 Hue 的值的示例各个像素。

enter image description here

它基本上可以归结为“更暖”和“更冷”的确切含义(没有双关语),但应该可以从色调值中得出这一点。 (注意:图像中的颜色只是绘图的颜色,与图像中的颜色无关。图像中的颜色仅决定绘图的高度在每个点绘制。)

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.jzy3d.analysis.AbstractAnalysis;
import org.jzy3d.analysis.AnalysisLauncher;
import org.jzy3d.chart.factories.AWTChartComponentFactory;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Builder;
import org.jzy3d.plot3d.builder.Mapper;
import org.jzy3d.plot3d.builder.concrete.OrthonormalGrid;
import org.jzy3d.plot3d.primitives.Shape;
import org.jzy3d.plot3d.rendering.canvas.Quality;

public class BasicJzy3D extends AbstractAnalysis
{
public static void main(String[] args) throws Exception
{
BufferedImage image = ImageIO.read(new File("lena512color.png"));
AnalysisLauncher.open(new BasicJzy3D(image));
}

private final BufferedImage image;

BasicJzy3D(BufferedImage image)
{
this.image = image;
}


/**
* Returns the RGB value in the given image at the specified location,
* which is given in relative coordinates (between 0.0 and 1.0).
* Invalid coordinates will be clamped to the border
*
* @param image The image
* @param x The x coordinate
* @param y The y coordinate
* @return The RGB value
*/
private static int getRGB(BufferedImage image, double x, double y)
{
int w = image.getWidth();
int h = image.getHeight();
int ix = (int)(x * w);
int iy = (int)(y * h);
ix = Math.max(0, Math.min(w-1, ix));
iy = Math.max(0, Math.min(h-1, iy));
int rgb = image.getRGB(ix, iy);
return rgb;
}

/**
* Returns the hue for the given RGB color
* @param rgb The RGB color
* @return The hue
*/
private static float getHue(int rgb)
{
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb >> 0) & 0xFF;
float hsb[] = new float[3];
java.awt.Color.RGBtoHSB(r, g, b, hsb);
float hue = hsb[0];
return hue;
}

static class ImageToValueMapper extends Mapper
{
private final BufferedImage image;

ImageToValueMapper(BufferedImage image)
{
this.image = image;
}

@Override
public double f(double x, double y)
{
int rgb = getRGB(image, x, y);
float hue = getHue(rgb);
return hue;
}
}


@Override
public void init()
{
Mapper mapper = new ImageToValueMapper(image);

// Define range and precision for the function to plot
Range range = new Range(0, 1);
int steps = 80;

// Create the object to represent the function over the given range.
final Shape surface = Builder.buildOrthonormal(
new OrthonormalGrid(range, steps, range, steps), mapper);
surface.setColorMapper(
new ColorMapper(new ColorMapRainbow(),
surface.getBounds().getZmin(),
surface.getBounds().getZmax(),
new Color(1, 1, 1, 1.0f)));
surface.setFaceDisplayed(true);
surface.setWireframeDisplayed(false);

// Create a chart
chart = AWTChartComponentFactory.chart(
Quality.Advanced, getCanvasType());
chart.getScene().getGraph().add(surface);
}
}

关于java - 在 Java 中将彩色图像转换为 3D 冲浪图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24267289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com