Java 将图像组件的颜色随机化

Java 将图像组件的颜色随机化,java,image-processing,Java,Image Processing,好的,所以我坚持使用一种方法,在这种方法中,我必须返回图像中的每个分量,但将每个分量的颜色随机化。这就是我到目前为止所做的: public Picture colourComponentImage() { Picture picture = new Picture(fileLocation); int width = picture.width(); int height = picture.height(); // convert to black and w

好的,所以我坚持使用一种方法,在这种方法中,我必须返回图像中的每个分量,但将每个分量的颜色随机化。这就是我到目前为止所做的:

public Picture colourComponentImage()
{
    Picture picture = new Picture(fileLocation);
    int width = picture.width();
    int height = picture.height();

    // convert to black and white
    for (int x = 0; x < width; x++) 
    {
        for (int y = 0; y < height; y++) 
        {
            Color color = picture.get(x, y);
            if (Luminance.lum(color) < threshold)
            {
                picture.set(x, y, Color.BLACK);
            }
            else
            {
                picture.set(x, y, Color.WHITE);
            }
        }
    }

    // union - find data structure
    connected(height, width);
    find(height);
    union(height, width);

    // Randomises the colour of each component

    Random random = new Random();

    float r = random.nextFloat();
    float g = random.nextFloat();
    float b = random.nextFloat();

    Color randomColor = new Color(r, g, b);
    return picture;
}
public Picture colorComponentImage()
{
图片=新图片(文件位置);
int width=picture.width();
int height=picture.height();
//转换成黑白
对于(int x=0;x

有人能告诉我哪里出了问题吗?

我真的不确定这是否是你想要实现的

public Picture colourComponentImage()
{
    Picture picture = new Picture(fileLocation);
    int width = picture.width();
    int height = picture.height();

    // Create two random colours
    Random random = new Random();
    Color randomColourOne = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat());
    Color randomColourTwo = new Color(random.nextFloat(), random.nextFloat(), random.nextFloat());

    // Convert to two tones of any two random colours
    for ( int x = 0; x < width; x++ ) 
    {
        for ( int y = 0; y < height; y++ ) 
        {
            Color color = picture.get(x, y);
            if ( Luminance.lum(color) < threshold )
            {
                picture.set(x, y, randomColourOne);
            }
            else
            {
                picture.set(x, y, randomColourTwo);
            }
        }
    }

    // union - find data structure
    connected(height, width);
    find(height);
    union(height, width);

    return picture;
}
public Picture colorComponentImage()
{
图片=新图片(文件位置);
int width=picture.width();
int height=picture.height();
//创建两个随机颜色
随机=新随机();
Color randomColorOne=新颜色(random.nextFloat(),random.nextFloat(),random.nextFloat());
Color randomColorTwo=新颜色(random.nextFloat(),random.nextFloat(),random.nextFloat());
//转换为任意两种随机颜色的两种色调
对于(int x=0;x
在您当前的方法中,什么不起作用?当我运行程序时,图片不会显示,因为我看不到图片,我不知道颜色随机化是否起作用。您知道如何使用java绘图吗?你需要一个顶级容器(框架)并在其图形上绘制。。。您知道这一点吗?您是否在代码中这样做?如果是,请提交该代码。。。如果没有plaes研究查看您的代码,我很确定随机颜色不起作用-您在方法末尾创建了一个随机颜色,然后再也不使用它!?据我所见,您的最终图像将仅为黑白。@munyul能否告诉我缺少的代码?提前谢谢