How to find number of pixels from specific color in an image?
So i'm trying to make a program that can find how many pixels of a specific colour it has in it. The images are pictures taken with camera and after that some areas on them has been marked on photoshop, and i need to find the exact number of this pixels. But i have few problems. I'm using getPixel(x,y) but and i'm comparing to the Color.FromArgb(red, green, blue) that i want but... my first problem is that for colors differ a little for example i want to find out the color RGB 116,110,40 but when you draw with this color on photoshop some pixels get a little diferent color like RGB 115,108,38 (and others similar) and i want to include this as well. So i finally came up with this code(but it seems that id does now work right):
public Form1()
{
InitializeComponent();
}
Bitmap image1;
int count=0;
int red, green, blue;
int redt, greent, bluet;
double reshenie;
private void button1_Click(object sender, EventArgs e)
{
try
{
red = int.Parse(textBox1.Text);
green = int.Parse(textBox2.Text);
blue = int.Parse(textBox3.Text);
// Retrieve the image.
image1 = new Bitmap(@"C:\bg-img.jpg", true);
double widht, height, pixel ;
int x, y;
MessageBox.Show(pixel.ToString());
// Loop through the images pixels
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
redt = pixelColor.R;
greent = pixelColor.G;
bluet = pixelColor.B;
if ((red+10>=redt) && (red-10>=redt))//i used +-10 in attempt to resolve the problem that i have writed about the close colours
{
if ((green + 10 >= greent) && (green - 10 >= greent))
{
if ((blue + 10 >= bluet) && (blue - 10 >= bluet))
{
count += 1;
}
}
}
}
}
pictureBox1.Image = image1;
MessageBox.Show("Imashe " + count.ToString());
count = 0;
}
catch (ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}
}
The problem is that i dont get the result that i expect. For example when i have to get like 1000 pixels i get more or less and i cant find where is my mistake. So if someone can give me idea what i'm doing wrong. Thanks for all the help in advance.开发者_如何学编程
Try with this loop instead :
int epsilon = 10;
for (x = 0; x < image1.Width; ++x)
{
for (y = 0; y < image1.Height; ++y)
{
Color pixelColor = image1.GetPixel(x, y);
redt = pixelColor.R;
greent = pixelColor.G;
bluet = pixelColor.B;
if (Math.Abs(redt - red) <= epsilon &&
Math.Abs(greent - green) <= epsilon &&
Math.Abs(bluet - blue) <= epsilon)
{
++ count;
}
}
}
Where epsilon
is the maximal difference between pixel color and targetted color, for each channel.
From your code:
if ((green + 10 >= greent) && (green - 10 >= greent))
If (a - 10 >= b)
, then for sure (a + 10 >= b)
. See if you can understand why.
I think you may have meant
if ((green - 10 <= greent) && (greent <= green + 10))
Ordering the condition like that helps with readability, because greent
has to be between green - 10
and green + 10
, and is also physically located between those expressions.
I think your color comparison is not right. You mixed the <=
and >=
in your attempt to be in a range around the color. Try this:
if ((red+10 >= redt) && (red-10 <= redt)) //i used +-10 in attempt to resolve the problem that i have writed about the close colours
{
if ((green + 10 >= greent) && (green - 10 <= greent))
{
if ((blue + 10 >= bluet) && (blue - 10 <= bluet))
{
count += 1;
}
}
}
精彩评论