How can i add tolerance calculation to the images comparison method ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
This is the comparison method:


<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //*** This function compare images region specific area ( in this case the "radar close" text area ) ***\
<span style="color:Blue; public <span style="color:Blue; bool ImageComparison(Bitmap Image1, Bitmap Image2, Rectangle Rect)
{
<span style="color:Green; //Logger.Write("Rect >>>> " + Rect.ToString());

<span style="color:Blue; int x;
<span style="color:Blue; int y;
<span style="color:Blue; bool different = <span style="color:Blue; false;
textbox3 = Rect.ToString();
<span style="color:Green; //if pictures are not of same size, return that they are different
<span style="color:Blue; if (Image1.Width != Image2.Width || Image1.Height != Image2.Height)
{
Logger.Write(<span style="color:#A31515; "Image1 Width >>>> " + Image1.Width.ToString());
Logger.Write(<span style="color:#A31515; "Image2 Width >>>> " + Image2.Width.ToString());
Logger.Write(<span style="color:#A31515; "Image1 Height >>>> " + Image1.Height.ToString());
Logger.Write(<span style="color:#A31515; "Image2 Height >>>> " + Image2.Height.ToString());
Logger.Write(<span style="color:#A31515; "Returned False images were different size");
MessageBox.Show(<span style="color:#A31515; "Images are different Size");
textbox1 = <span style="color:#A31515; "";
textbox2 = <span style="color:#A31515; "";
textbox3 = <span style="color:#A31515; "";
<span style="color:Blue; return <span style="color:Blue; false;
}
<span style="color:Green; //Iterate over the Rect
<span style="color:Blue; for (x = Rect.X; x < Rect.X + Rect.Width; ++x)
{
<span style="color:Blue; for (y = Rect.Y; y < Rect.Y + Rect.Height; ++y)
{
<span style="color:Green; //check, if x and y are inside the picture
<span style="color:Blue; if (x >= 0 && x < Image1.Width && y >= 0 && y < Image1.Height &&
x < Image2.Width && y < Image2.Height)
{
Color c = Image1.GetPixel(x, y);
textbox1 = c.ToString();
Color c2 = Image2.GetPixel(x, y);
textbox2 = c2.ToString();
<span style="color:Blue; if (c.Equals(c2) == <span style="color:Blue; false)
{
different = <span style="color:Blue; true;
<span style="color:Blue; break;
}
}
<span style="color:Blue; else
{
<span style="color:Blue; throw <span style="color:Blue; new Exception(<span style="color:#A31515; " Hello!! your region is way off!!!.. major bug!");

}
}

<span style="color:Blue; if (different)
<span style="color:Blue; break;
}
<span style="color:Green; //Logger.Write("differenet bool is now >>>> " + different.ToString());
<span style="color:Blue; return !different;

}

<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; bool CheckColorValues(Color c, Color c2)
{
<span style="color:Blue; if (c.A.Equals(c2.A)
&& c.R.Equals(c2.R)
&& c.G.Equals(c2.G)
&& c.B.Equals(c2.B))
{
<span style="color:Blue; return <span style="color:Blue; true;
}
<span style="color:Blue; else
{
<span style="color:Blue; return <span style="color:Blue; false;
}
}
[/code]
<br/>
Instead the bool CheckColorValues i want to add tolerance to the cheking.

Somrthingl ike that:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; for (b = 0; b < filtered_image.Width; b++)
{
<span style="color:Blue; for (c = 0; c < filtered_image.Height; c++)
{
Color Color1 = filtered_image.GetPixel(b, c);
Color originalColor = newimage1.GetPixel(b, c);
Color originalColor1 = newimage2.GetPixel(b, c);

<span style="color:Blue; if (Color1.R == 0 && Color1.G == 0 && Color1.B == 0)
{
}
<span style="color:Blue; else
{
<span style="color:Blue; int Dr = (<span style="color:Blue; int)originalColor.R - originalColor1.R;
<span style="color:Blue; int Dg = (<span style="color:Blue; int)originalColor.G - originalColor1.G;
<span style="color:Blue; int Db = (<span style="color:Blue; int)originalColor.B - originalColor1.B;

color_distance = Math.Sqrt(Dr * Dr + Dg * Dg + Db * Db);

<span style="color:Blue; double[] d = { color_distance };


<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < d.Length; i++)
{


<span style="color:Blue; if (highestDouble < d)
{

highestDouble = d;

}

}
[/code]
<br/>
I want somehow to make so ill use tolerance for the comparison and also that i can change the tolerance value each time if the comparison isnt working good.


Now when im comparing tow images sometimes its working and sometimes not. And i have some images looks the same but they are not.
I tested and tried many things. I guess i need to use somehow tolerance instead just comparing the colors.

Thanks. <hr class="sig danieli

View the full article
 
Back
Top