3 Replies - 2252 Views - Last Post: 06 January 2008 - 02:27 AM Rate Topic: -----

#1 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Finding white pixels in an image with black background

Post icon  Posted 05 January 2008 - 05:44 AM

 
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:
Posted 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: that's weird because there are alot of white pixels.. Is there something wrong with my if statement?

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: 05 January 2008 - 05:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding white pixels in an image with black background

#2 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Re: Finding white pixels in an image with black background

Posted 05 January 2008 - 06:03 AM

if (col == Color.FromArgb(255,255,255))


found it.. I should think more before posting :P
thx anyway
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 12,803
  • Joined: 16-October 07

Re: Finding white pixels in an image with black background

Posted 05 January 2008 - 08:55 AM

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.

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.
Was This Post Helpful? 0
  • +
  • -

#4 thejasper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 02-November 07

Re: Finding white pixels in an image with black background

Posted 06 January 2008 - 02:27 AM

yep, you're totally correct :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1