-2

I need a code that reads a 160x160 image of a maze, checks if it has white or black pixels and prints the pixel x and y position with information if it is black or white

Example:

134, 27: w

I need it for every single pixel

Right Now I have the following:

Bitmap maze = new Bitmap ("images/maze.png");
int[][] colors = new int[maze.Width][];
for (int i = 0; i < colors.Length; i++) 
    colors[i] = new int[maze.Height];
for (int x = 0; x < maze.Width; x++) 
    for (int y = 0; y < maze.Height; y++) 
        colors[x][y] = maze.GetPixel (x, y);
user85421
  • 28,957
  • 10
  • 64
  • 87
Sport
  • 55
  • 8

1 Answers1

0

First of all, you can't store a Color as an int, which is why you should switch from int[][] to Color[][].

To output your desired result, simply loop over the array again, formatting the result a bit using string.Format() and some conditional operators.

for (int x = 0; x < maze.Width; x++) 
    for (int y = 0; y < maze.Height; y++) 
        Console.WriteLine(string.Format("{0}, {1}: {2}",
            x, y,
            colors[x][y].ToArgb() == Color.Black.ToArgb() ? "b" : (colors[x][y].ToArgb() == Color.White.ToArgb() ? "w" : "n")
            ));

Note that this code outputs n if the pixel is neither black nor white.

Also note that you can omit the string.Format() method aswell, since the Console.WriteLine() function contains an overload with formatting possibility.

Edit:

If you want to define a color as white if their RGB color is greater than, for example 170, you could add up their RGB values (excluding alpha values here, since reading image data is mostly done without transparency) and check if their sum is greater than 170 * 3.

Color current = colors[x][y];
int rgbSum = current.R + current.G + current.B;
char whiteOrBlack = (170 * 3 < rgbSum) ? 'w' : 'b';
Console.WriteLine(string.Format("{0}, {1}: {2}", x, y, whiteOrBlack));

I just want to point out that 170 is a rather odd number to use here. Maybe it's more likely that you meant 128 (i.e. half a byte)?

Ian H.
  • 3,840
  • 4
  • 30
  • 60
  • 1
    The read colors won't be comparable to the built-in known colors in that way. The comparison should be done with `colors[x][y].ToArgb() == Color.Black.ToArgb()`. Since `ToArgb()` returns an int, there's no need to change the structure of the `colors` array, so long as you're assigning the pixel to it through the `ToArgb()` method. – Jonathon Chase Jan 12 '18 at 22:07
  • @JonathonChase You can't? Doesn't the `Color` class has a `==` operator that compares the encoded color values of the two parameters? I switched it to include `.ToArgb()` anyways, as you said. – Ian H. Jan 12 '18 at 22:19
  • 1
    While it does, the equality operator also compares some state flags that would not get picked up reading the colors from the `GetPixel` call. You can read more about that [in this question](https://stackoverflow.com/questions/7464994/how-to-compare-a-color-by-the-getpixel-method-and-a-color-passed-in-a-method-lik) and [this MSDN](https://msdn.microsoft.com/en-us/library/system.drawing.color.op_equality.aspx) – Jonathon Chase Jan 12 '18 at 22:23
  • 1
    @JonathonChase I just looked it up quickly in the source code myself, it really does additionally compare states and other stuff together with the value comparison, you were absolutely right. – Ian H. Jan 12 '18 at 22:25
  • I'm having about 13 errors Colors doesn't exist Bool To Char Errors caused by errors Current doesn't exist – Sport Jan 12 '18 at 22:38
  • @Sport Mind sharing these errors by editing them into your question? – Ian H. Jan 12 '18 at 22:39
  • @IanH. His issue with Colors doesn't exist is that you're using `Colors` instead of `Color` in your first code block. – Jonathon Chase Jan 12 '18 at 22:42
  • @JonathonChase Ugh, I am used to Unity's color structures, thanks for pointing out my error, fixed it. – Ian H. Jan 12 '18 at 22:44
  • Colors still doesn't exist... Help – Sport Jan 12 '18 at 22:48
  • @Sport You need to add a reference to `System.Drawing` to access the `System.Drawing.Color` type. The `Colors` was just a spelling error by me. – Ian H. Jan 12 '18 at 22:50
  • I can access System.Drawing but I can't access System.Drawing.Color, I've added the reference, helps? – Sport Jan 12 '18 at 22:57
  • I've changed Using to Using Static, Right now the color is: Embedded statement cannot be a declaration or a labeled statement – Sport Jan 12 '18 at 23:00
  • @Sport If your for-loop does not contain a single line of code you need to add the curly brackets `{}` around the loop code block. – Ian H. Jan 12 '18 at 23:04
  • Alright, Half of the errors gone, Name color does not exist Can't convert from bool to char in assigning blackorwhite rgbsum can't be compared – Sport Jan 12 '18 at 23:08
  • @Sport I forgot to add the parenthesis around the operation, check the edited line with `char blackOrWhite`. – Ian H. Jan 12 '18 at 23:11
  • Making that, makes the line below say that WhiteOrBlack doesn't exist, c# is bugged – Sport Jan 12 '18 at 23:15
  • Thanks, I've only got an error left:The name 'color' doesn't exist in the current context – Sport Jan 12 '18 at 23:22
  • @Sport Then you included the reference to the `System.Drawing.Color` incorrectly. – Ian H. Jan 12 '18 at 23:25
  • I'm having trouble as I've added it correctly, Error:The type or namespace 'colors' does not exist in the namespace 'system.Drawing' (are you missing an assembly refference?) – Sport Jan 12 '18 at 23:37
  • @Sport Have you added a reference to the DLL of System.Drawing? – Ian H. Jan 13 '18 at 09:26
  • Doesn't let me do it, outputs an error and asks to use the .net – Sport Jan 13 '18 at 09:47
  • @Sport How did you add the reference and where did you locate the DLL? – Ian H. Jan 13 '18 at 09:51
  • Take this to discord, I don't want to make an extensive reply log Illusion... Maybe?#2824 – Sport Jan 13 '18 at 09:52