I'm trying to make a small program in c#, that will downsample image to specified amount of colors (using K-means). Then, user will be able to hide\show colored areas(clusters) in image. I've implemented image clustering algorithm and I also have info about main colors, but how to implement show/hide color function? Example below

for this moment program only simplifies image (in this example 5 colors) for this moment program only simplifies image, in this example 5 colors

this is what I want to have

this is what I want to have

  • How did you create that second picture that shows what you want? Could you translate your steps into a program? – amon Mar 27 '15 at 13:48
  • I've created the second image in photoshop// to show exactly what I want. In program I've got only first image(in example) without checkboxes – photostok Mar 27 '15 at 14:08
  • 3
    So you probably selected all yellow areas and substituted them for white? You could surely write a program that copies the image but substitutes each yellow pixel for a white one. Even simpler, you could have one layer for each cluster, with areas not in the cluster being transparent. When you render the result, you just compose all selected layers. – amon Mar 27 '15 at 14:14
  • Are you using System.Drawing or System.Windows.Media.Imaging? Is the image already converted into a paletted image (one that has a Palette property ? – rwong Mar 27 '15 at 14:14
  • yes, Im using System.Drawing class; as the result of k-means function I get Dictionary<string, Cluster>, where Cluster class has 4 fields(R,G,B,Alpha). So in example I've got 5 Cluster objects./*Even simpler, you could have one layer for each cluster*/ Thats a good idea! But how to create these layers? as I understood it will be 5 different images(in example), right? – photostok Mar 27 '15 at 14:21

This function returns image only with selected colors. List colors -list with selected Colors that are converted to int value with ToArgb() function (for better comparing). LockBitmap class you can find here: http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp

public static Bitmap ReDraw(Bitmap bmp, List<int> colors)
    LockBitmap lockBitmap = new LockBitmap(bmp);

    for (int y = 0; y < lockBitmap.Height; y++)
        for (int x = 0; x < lockBitmap.Width; x++)
            int color = lockBitmap.GetPixel(x, y).ToArgb();
            bool flag = colors.Contains(color);
            if (!flag)
                lockBitmap.SetPixel(x, y, Color.White);
    return bmp;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.