Im comparing two images had this before. Sometimes the comparison isnt good and i wonder if i should

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I worked on it with my brother alot and also here in the forum.
For now in most of the time more then 98% its working perfect. But sometimes there is a problem.
The idea in my project is to download images from a website in any X seconds.
Now im comparing specific area in the images by pixels using GetPixel.
When there is text on the image english&hebrew the image should get delete from the temp directory and if its without any text in the middle it should be copied to the download directory.

The problem is sometimes its downloading bad images with text inside. And i cant find where is the problem since all the files same size and i checked the rectangle area wich i compare with the two images and its identical.
The only thing i can think of is that some images with text inside have something changed dont know how and what.
I also converted images with text inside to black and white and compared it with other images with text inside from the website images that wasnt downloaded and copied and it seems that all the pixels are the same in same location.

So i cant figure out why sometimes the images with text inside are the same and sometimes no.
I have in my resources one image with text inside from the website wich im comparing with every images that downloaded.
And sometimes it seems that two images with text inside arent the same. And its strange since it dosent happening so much only every some X time.

I was wondering maybe i should check for tolerate between the images while comparing and see if there is a change or something.
And then maybe to change the tolerate in the image in the resources or to compare tye tolerate between each images.


This is the class of my comparison i have many functions there and only one is for comparing:


<pre>//*** This function compare images region specific area ( in this case the "radar close" text area ) ***\
public bool ImageComparison(Bitmap Image1, Bitmap Image2, Rectangle Rect)
{
int x;
int y;
bool different = false;
textbox3 = Rect.ToString();
//if pictures are not of same size, return that they are different
if (Image1.Width != Image2.Width || Image1.Height != Image2.Height)
{
MessageBox.Show("Images are different Size");
textbox1 = "";
textbox2 = "";
textbox3 = "";
return false;
}
//Iterate over the Rect
for (x = Rect.X; x < Rect.X + Rect.Width; ++x)
{
for (y = Rect.Y; y < Rect.Y + Rect.Height; ++y)
{
//check, if x and y are inside the picture
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();
if (c.Equals(c2) == false)
{
different = true;
break;
}
}
else
{
throw new Exception(" Hello!! your region is way off!!!.. major bug!");

}
}

if (different)
break;
}
return !different;

}

private static bool CheckColorValues(Color c, Color c2)
{
if (c.A.Equals(c2.A)
&& c.R.Equals(c2.R)
&& c.G.Equals(c2.G)
&& c.B.Equals(c2.B))
{
return true;
}
else
{
return false;
}
}[/code]

Now the next code is from form1 where im using this function to making the comparison and decide by the result what to do with the file:
There is a lot of mess in this code so i wonder maybe something is worng wich make the comparison not to be good enough.
But again in most of cases like 98-99% its working good and only in some times from time to time it dosent work good and copy closed radar image wich have text inside wich isnt good.


<pre>private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
client_Error_counter = client_Error_counter + 1;
if (client_Error_counter == 5)
{
Logger.Write("Tried to download radar image file " + client_Error_counter + " times without success");
Logger.Write("Error Tried to download radar image file : " + e.Error);
client_Error_counter = 0;
}
else
{

Client.DownloadFileAsync(myUri, Path.Combine(temp_dir + temp_file));
Logger.Write("Times tried to download radar image file: " + client_Error_counter);
Logger.Write("Error Tried to download radar image file : " + e.Error);
}
}
else
{
if (!e.Cancelled)
{
if (!File.Exists(temp_dir + temp_file))
{
}
else
{

temp_backup_files_counter = temp_backup_files_counter + 1;
if (temp_backup_files_counter >= 5)

temp_backup_files_counter = 1;

temp_backup_files_counter = last_temp_untitled_file() + 1;


temp_files = @"temp_untitled_backup_" + temp_backup_files_counter.ToString("D1") + ".bmp";
File.Copy(Path.Combine(temp_dir+ temp_file), Path.Combine(temp_backup_dir+ temp_files), true);

}
Bitmap File1 = null;
string Next_File;
bool file_compare;
int i;
bool bad_file;
if (!Directory.Exists(sf))
{
}
else
{
file_array_dl = Directory.GetFiles(sf, "radar*.jpg");
if (file_array_dl.Length == 0)
{
if (File.Exists(temp_dir + temp_file))
{
File.Copy(temp_dir + temp_file, sf + @"radar000001.jpg");
pictureBox1.Load(temp_dir + temp_file);
}
else
{
}
}
else
{
if (!File.Exists(temp_dir + temp_file))
{
}
else
{
File.Copy(temp_dir + temp_file, bad_file_test_dir + testing_file);
bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);

if (bad_file == true)
{
File.Delete(bad_file_test_dir + testing_file);
}
}
File.Delete(bad_file_test_dir + testing_file);
i = last_image_file();
last_file = sf + @"radar" + i.ToString("D6") + ".jpg";
File.Copy(last_file, bad_file_test_dir + testing_file);
bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);

if (bad_file == true)
{
File.Delete(bad_file_test_dir + testing_file);
if (File.Exists(last_file))
{
file_compare = File_Utility.File_Comparison(temp_dir + temp_file, last_file);
if (file_compare == true)
{
pictureBox1.Load(last_file);
return;
}
File.Copy(temp_dir+temp_file, bad_file_test_dir + testing_file);
bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
if (bad_file == true)
{
File.Delete(bad_file_test_dir + testing_file);
File1 = new Bitmap(temp_dir + temp_file);
Bitmap file2;
file2 = Properties.Resources.RadarImageClose;
if (ImagesComparion1.ImageComparison(File1, file2, image_scan_text_rect) == true)


{
pictureBox1.Load(last_file);
File1.Dispose();
Properties.Resources.RadarImageClose.Dispose();
return;
}
else
{
Color c2;
int t, j;
tempBmp = new Bitmap(last_file);
for (t = image_scan_text_x; t < image_scan_text_x1; t++)
for (j = image_scan_text_y; j < image_scan_text_y1; j++)
{
c1 = tempBmp.GetPixel(t, j);
c2 = Color.FromArgb(c1.R + 50, c1.G, c1.B);
tempBmp.SetPixel(t, j, c2);
}
pictureBox1.Image = tempBmp;
pictureBox1.Invalidate();*/
// return;

File1.Dispose();
Properties.Resources.RadarImageClose.Dispose();
}
}





if (File.Exists(bad_file_test_dir + testing_file))
{
File.Delete(bad_file_test_dir + testing_file);
}
File.Copy(temp_dir + temp_file, bad_file_test_dir + testing_file);
bad_file = Bad_File_Testing(bad_file_test_dir + testing_file);
if (bad_file == true)
{
File.Delete(bad_file_test_dir + testing_file);
i = last_image_file() + 1;
Next_File = sf + @"radar" + i.ToString("D6") + ".jpg";
File.Copy(temp_dir + temp_file, Next_File);
FileInfo myinf;
myinf = new FileInfo(Next_File);
Logger.Write("Last Downloaded file is :" + Next_File);
pictureBox1.Load(Next_File);
pb1_fs.picturebox1_lastFile(Next_File); // to check why pb1_fs was null in at least one case \
if (settingsmenu.EnableVoices.Checked == true)
{
settingsmenu.speaker.SpeakAsync("Last Downloaded File Was" + Path.GetFileName(Next_File));
settingsmenu.speaker.SpeakAsync("At" + (myinf.CreationTime));
}
else
{
}
}
else
{
File.Delete(temp_dir + temp_file);
File.Delete(bad_file_test_dir + testing_file);
if (file_array_dl.Length > 0)
{
i = last_image_file();
last_file = sf + @"radar" + i.ToString("D6") + ".jpg";
pictureBox1.Load(last_file);
}
}
}
else
{
File.Delete(temp_dir + temp_file);
File.Delete(bad_file_test_dir + testing_file);
if (file_array_dl.Length > 0)
{
i = last_image_file();
last_file = sf + @"radar" + i.ToString("D6") + ".jpg";
pictureBox1.Load(last_file);
}
}
}
}
}
}
}
}[/code]


In the end i think the code it self is ok. A lot of mess but the code i think is ok of the comparison.
The problem is maybe i should use tolerate here and how to do it ?



And if its needed i can upload somewhere the complete project.


Thanks.




<hr class="sig danieli

View the full article
 
Back
Top