Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,169 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,894 people online right now. Registration is fast and FREE... Join Now!




Finding white pixels in an image with black background

 
Reply to this topicStart new topic

Finding white pixels in an image with black background

thejasper
5 Jan, 2008 - 04:44 AM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ProgrammingExercice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap image = new Bitmap("PNG.png");
            Color col = new Color();
            ArrayList wit = new ArrayList();
            pb_image.Image = image;

            int breedte = image.Size.Width;
            int hoogte = image.Size.Height;

            for (int i = 0; i < hoogte; i++)
            {
                for (int j = 0; j < breedte; j++)
                {
                    col = image.GetPixel(j, i);
                    if (col.Equals(Color.White))  
                    {
                        wit.Add(i * 100 + breedte);
                    }
                }
            }

            MessageBox.Show(wit[0].ToString());    //this gives me the error
        }
    }
}



This is the image I use:
IPB Image

When I debug my program it gives me an error when I try to print the first element of arraylist wit. There are nog elements in it hmmm.gif that's weird because there are alot of white pixels.. Is there something wrong with my if statement?

CODE
wit.Add(i * 100 + breedte);

Oh and this is because all my pixels are numbered from 0 to 99 the first row, 100 to 199 second, ...

I would really appreciate some help.
thx, Jasper

edit: should I compare the pixel to the R, G, B values separatly? I'll give it a try, reply for tips plz

This post has been edited by thejasper: 5 Jan, 2008 - 04:57 AM
User is offlineProfile CardPM
+Quote Post

thejasper
RE: Finding White Pixels In An Image With Black Background
5 Jan, 2008 - 05:03 AM
Post #2

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
CODE
if (col == Color.FromArgb(255,255,255))


found it.. I should think more before posting tongue.gif
thx anyway
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Finding White Pixels In An Image With Black Background
5 Jan, 2008 - 07:55 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Excellent! Glad you found an answer.

Quick design note, calling this if (col == Color.FromArgb(255,255,255)) for every iteration of your loop (image.Size.Width * image.Size.Height) will slow you down. You should try to call Color.FromArgb(255,255,255) only once, if you can. It's not something you'll notice normally, but multiply those extra milliseconds by the size of loop and such things can become apparent.

Here's an example.

CODE

ArrayList wit = new ArrayList();

Bitmap image = new Bitmap("PNG.png");
pb_image.Image = image;

int breedte = image.Size.Width;
int hoogte = image.Size.Height;
// we're going to be checking against this value a lot
// better do the calculation once
Color checkColor = Color.FromArgb(255,255,255);

// x,y instead of i,j.  Mostly because it makes sense in GetPixel
for (int y = 0; y < hoogte; y++) {
    for (int x = 0; x < breedte; x++) {
        // in contrast, we don't have to assign the results of
        // GetPixel, because we loot at it only once
        if (image.GetPixel(x, y) == checkColor)
            wit.Add(y * 100 + breedte);
        }
    }
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

thejasper
RE: Finding White Pixels In An Image With Black Background
6 Jan, 2008 - 01:27 AM
Post #4

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 28


My Contributions
yep, you're totally correct tongue.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:27AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month