Image Comparison
Hello, I currently develop an OCR program.
I found that compare the image pixel by pixel is very slow. Is that any solution can solve this kind of problem.
Regards, James
Printable View
Image Comparison
Hello, I currently develop an OCR program.
I found that compare the image pixel by pixel is very slow. Is that any solution can solve this kind of problem.
Regards, James
What is your ultimate goal of comparing images?
If the image size is not the same, don't do the pixel by pixel comparision. If they are the same size, then you could take a selected sample of pixels, at various places on your 2 images. Only if these match do you then do the full pixel by pixel comparision.
I too am just thinking about creating an OCR application. I need to be able to scan colums of data (from paper in various table formats) and put the data into a database. Do you have any advice? Are there any 3rd party tools you can recommend?
Uhm... Could you try with jpeg compression? It works with colors... Just compress both images, and now can do a comparison like PeteD said. Doing the compression before, accentuate the difference between the two images, but I also know that compressing the images will also take a lot of time... It's a suggestion.
Xmas79.
If you want to test if the two images are exactly the same first check for the file size, then compare the images MD5 digest. if they are the same then the images are the same. This is a generic method for comparing files. Read this thread .
uuh doesnt C# allow some sort of unmanaged code where you can load the bitmap into memory, create memorystreams and "stuff like that" to make it faster? :rolleyes: (wink wink MrPolite doesnt know what he's saying :) )
I wouldn't compare file sizes because if the images are not the same, they could still be the same size.Quote:
Originally posted by Lunatic3
If you want to test if the two images are exactly the same first check for the file size, then compare the images MD5 digest. if they are the same then the images are the same. This is a generic method for comparing files. Read this thread .
I'd suggest selecting several sections of pixels like what was mentioned and testing them
But i was talking about the other side: if they are not equal in size then they are not the same. If the same size then you should apply a method to compare them, and what i suggest doesnt check for pixels or that sort of thing. It checks if the whole content of image file (or memory stream) is the same, in a very simple way.Quote:
Originally posted by kasracer
I wouldn't compare file sizes because if the images are not the same, they could still be the same size.
If you check it to see if they are NOT the same size, then you have the same exact program, they can still be the same image.Quote:
Originally posted by Lunatic3
But i was talking about the other side: if they are not equal in size then they are not the same. If the same size then you should apply a method to compare them, and what i suggest doesnt check for pixels or that sort of thing. It checks if the whole content of image file (or memory stream) is the same, in a very simple way.
Different compressions format different, programs can save differently making someone larger or smaller
In my opinion if you change the compression of an image then that wont be the same as the original image, although changes may not be visible to us. So comparing two images of different compression even if they origin from one source is useless, they are not the same anyway.Quote:
Originally posted by kasracer
Different compressions format different, programs can save differently making someone larger or smaller
the answer is surely to check the things that are the fastest to check 1st. You could check not only the size of the image (width and height) but also the file size. If these don't match up then you don't have to do any further pixel checking. If file sizes and image sizes are the same, then take a sample of pixels (maybe just 1 pixel in each corner, and one in the middle). If these all match then you could then increase the number of samples if you wanted to before you go and check every pixel. It would take bit longer when you have identical images, but the time taken to check non-idental images would be much much faster.
Thax for your reply. Attach herewith my sample application.
I understand the solution you give to me, but if I do in this method, I need to have a lot of prabability text pattern. Anyway, is that still got any solution?
For my case
First: Grap the text from image "ABC"
Second: After get A, i will change it to dimension 10*10 and compare with my knowledge base.
Function toArt(ByRef initialBitmap As Bitmap, ByRef art(,) As Char)
Dim x, y, r, g, b, col As Integer
Dim gray As Bitmap = initialBitmap
'values used for ascii art
Dim c0, c1, c2, c3, c4 As Char
c0 = "0"
c1 = "1"
c2 = "1"
c3 = "1"
c4 = "1"
Value2.totalPixel = 0
For y = 0 To 9 Step 1
For x = 0 To 9 Step 1
Dim c As Color = initialBitmap.GetPixel(x, y)
r = c.R
g = c.G
b = c.B
col = (r + g + b) / 3
gray.SetPixel(x, y, Color.FromArgb(col, col, col))
If col >= 0 And col <= 70 Then
art(x, y) = c4
Value2.totalPixel += 1
End If
If col > 70 And col <= 255 Then art(x, y) = c0
TextBox1.AppendText(art(x, y))
'Me.PictureBox1.Image = initialBitmap
strBinary = TextBox1.Text
Next
Next
Return art
End Function
Dim fs As FileStream = New FileStream("Data.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim sr As StreamReader = New StreamReader(fs)
Dim datacharTN As String
Dim dataBinary As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim arrayCharData(10, 10) As String
Dim arrayCharGrap(10, 10) As String
Dim countMax As Double = 0
Dim countBestPixel As Double = 0
Dim charDisplay As String
Dim tempData As String
Dim tempGrap As String
Dim countError As Double = 0
Dim countTotalPixel As Double = 0
Dim tmpString As String = ""
While (sr.Peek() > -1)
tmpString = sr.ReadLine
datacharTN = Mid(tmpString, 1, 1)
dataBinary = Mid(tmpString, 3, 100)
For i = 0 To Len(dataBinary) - 1 Step 10
tempData = Mid(dataBinary, i + 1, 10)
tempGrap = Mid(TextBox1.Text, i + 1, 10)
k = 0
For j = 0 To Len(tempData) - 1 Step 1
arrayCharData(k, j) = Mid(tempData, j + 1, 1)
arrayCharGrap(k, j) = Mid(tempGrap, j + 1, 1)
If arrayCharData(k, j) = CStr(1) Then
countTotalPixel += 1
If arrayCharData(k, j) = arrayCharGrap(k, j) Then
countMax += 1
If check_proper = True Then
If CDbl(countMax / ((Value2.totalPixel))) > 0.95 Then
charDisplay = datacharTN
GoTo Line3
End If
End If
ElseIf arrayCharData(k, j) <> arrayCharGrap(k, j) Then
countMax -= 1
countError += 1
If check_proper = True Then
If countError > 40 Then
GoTo Line2
End If
ElseIf check_proper = False Then
If countError > 80 Then
GoTo Line2
End If
End If
End If
'ElseIf arrayCharGrap(k, j) = CStr(1) Then
' countTotalPixel += 1
' If arrayCharData(k, j) = arrayCharGrap(k, j) Then
' countMax += 1
' ElseIf arrayCharData(k, j) <> arrayCharGrap(k, j) Then
' countMax -= 1
' End If
End If
Next
k += 1
Next
Line1:
If CDbl(countMax / countTotalPixel) > 9.5 Then
charDisplay = datacharTN
countBestPixel = countMax
GoTo Line3
ElseIf CDbl(countMax / countTotalPixel) > 0.7 Then
If countBestPixel < countMax Then
charDisplay = datacharTN
countBestPixel = countMax
End If
End If
Line2:
countMax = 0
countTotalPixel = 0
countError = 0
End While:) :)
Still i think you dont have to compare pixels.After initial checks, Just check if the content of image files (those binary craps) are the same, and this is extrelemy simple to do.