Jar.jar
Jar.jar

Reputation: 45

Counting groups of black pixels in a binary bitmap's row

I'm trying to come up with a way to display the number of black pixels in groups (one or more) separated by white pixels in a black and white bitmap. Each time the loop encounters a white pixel it should save the counter in a label and reset it. This way if I have a group of 5 black pixels, few white and 3 of black again, I'll have 2 of my labels show 5 and 3 in a row.

private void CountInRow(int NumOfRow, Bitmap bmp)
{
    int counter = 0;
    for (int i=9;i>0;i--)
    {
        if(bmp.GetPixel(i,NumOfRow)==Color.Black)
        {
            counter++;
        }
        else
        {
            //write the value of couter in a label and go to the next label
            counter = 0;
        }
    }
}

The problem I have is I cannot come up with a way to jump to the next label so I can write the value of the next group in it. All of the bitmaps will have a 10-pixel width.

Upvotes: 1

Views: 187

Answers (1)

Michael
Michael

Reputation: 1596

So to write your value to the label, replace your comment with these lines:

var name = "lab" + NumOfRow + "x" + i;
((Label)this.Controls[name]).Text = counter.ToString();

This will store the value in the appropriate label.

Upvotes: 3

Related Questions