随机化图像组件的颜色

时间:2014-10-09 09:00:43

标签: java image-processing

好吧所以我坚持一个方法,我必须返回图像中的每个组件,但每个组件的颜色随机化。这就是我到目前为止所做的:

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;
}

谁能告诉我哪里出错?

1 个答案:

答案 0 :(得分:0)

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

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;
}