15,880,608 members
2.00/5 (1 vote)
See more:
I'm working on a project using AForge algorithm to filter objects i need in an image and use them in comparison to another image of the same format to see the percentage similarity of the objects. I have the code below but it only gives me the percentage similarity or difference of the whole image. I really don't care about the whole image but my interest is just the objects i filtered in the image. Please friends, how do i go about this in C# or VB.Net? Here is the code below that shows only similarity and difference of both images but not the filtered objects of interest.

VB
```Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim bmp1 As Bitmap = Image.FromFile("C:\Temp\ImageCompare\chill.jpg")
Dim bmp2 As Bitmap = Image.FromFile("C:\Temp\ImageCompare\chill1.jpg")

Dim sameCount As Integer = 0
Dim diffCount As Integer = 0

For x As Integer = 0 To (bmp1.Width) - 1
For y As Integer = 0 To (bmp1.Height) - 1
If bmp1.GetPixel(x, y).Equals(bmp2.GetPixel(x, y)) Then
sameCount += 1
Else
diffCount += 1
End If
Next
Next

Dim total As Integer = sameCount + diffCount

MessageBox.Show(String.Format("Same Percentage: {0}{1}Difference Percentage: {2}", _
(sameCount / total).ToString("p"), Environment.NewLine, (diffCount / total).ToString("p")))
End Sub```
Posted
Sergey Alexandrovich Kryukov 14-Aug-14 18:34pm
Are you able to define the concept of "similarity"? I can define a pair of images which has zero similarity in one-to-one pixel comparison and apparent visual similarity (so it would even be hard to tell the difference).
And I can define a pair of images with apparent visual difference but high similarity in pixels. Isn't it obvious that it is possible?

So, please: similarity criteria, purpose...

—SA

## Solution 1

Please see the comment to the question. It depends on the purpose, of course, but for most, your comparison have too little practical sense. I hope I explained why.

Now, didn't I tell you before that `GetPixel` is prohibitively slow? You have to use `System.Drawing.Image.LockBits`:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx[^].

The help page on first of the two methods shows some code sample sufficient for understanding of the operation.

—SA

Naija Coding 14-Aug-14 19:03pm
Ok let be clearer. The project is to design a system that automatically marks students papers in a bitmap format. This is why there are 2 images, the first is the marking sheet and the second is the student answer sheet. The idea is to use the position and colour of the objects in the marking sheet which represents the right answers to mark the objects of the students sheet.
Sergey Alexandrovich Kryukov 14-Aug-14 22:39pm
Oh, no... :-)
—SA
Naija Coding 14-Aug-14 20:21pm
Bitmap bitmap = new Bitmap(pictureBox3.Image);
BlobCounter blobCounter = new BlobCounter(bitmap);
blobCounter.MinWidth = 5;
blobCounter.MinHeight = 5;
blobCounter.FilterBlobs = true;
blobCounter.ProcessImage(bitmap);
Rectangle[] rects = blobCounter.GetObjectsRectangles();

Bitmap bitmap2 = new Bitmap(pictureBox4.Image);
BlobCounter blobCounter2 = new BlobCounter(bitmap2);
blobCounter2.MinWidth = 5;
blobCounter2.MinHeight = 5;
blobCounter2.FilterBlobs = true;
blobCounter2.ProcessImage(bitmap2);
Rectangle[] rects2 = blobCounter.GetObjectsRectangles();

foreach (Rectangle recs in rects)
foreach (Rectangle recs2 in rects2 )
if (rects.Length > 0)
{
if (rects2 .Length >0)
{
foreach (Rectangle objectRect in rects)
{
foreach (Rectangle objectRect2 in rects2 )
{

for (int x = 0; x < bitmap .Width ; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
for (int x2 = 0; x2 < bitmap2.Width; x2++)
{
for (int y2 = 0; y2 < bitmap2.Height; y2++)
{

if (bitmap .GetPixel (x,y,objectRect) == (bitmap2 .GetPixel (x2,y2,objectRect2 )))
...

this is what am trying out in c#. having errors and i dont know if am on track
Sergey Alexandrovich Kryukov 14-Aug-14 22:40pm
I just told you about GetPixel...
—SA
Naija Coding 21-Aug-14 7:06am
I have no problems with image processing, and yes the get pixel is slow and I will look into the lockbit method too. Thanks. Could it be that what I really need here is OMR aka Form processing? Because I'm already done with image processing using AForge and it works fine. What I want to do now is compare the template to the form and get the percentage of the student's result. Any help?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Rick York 90 Graeme_Grant 50 Richard Deeming 50 Dave Kreskowiak 50 Richard MacCutchan 40
 Pete O'Hanlon 1,820 OriginalGriff 1,355 Graeme_Grant 895 Richard Deeming 798 Dave Kreskowiak 684

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900