How to Compare Two Images Using Byte Arrays

How to compare two images using byte arrays

Using == will compare the object references if not overridden.

Since these are two different byte[] objects, the references are different.

You need to compare the byte[] objects item by item in order to confirm that they are identical. You can use SequenceEquals in this case.

How to compare two images between a PictureBox and a resource data?

Using How to convert image to byte array :

var array1 = ImageToByteArray(pictureBox1.Image);
var array2 = ImageToByteArray(Properties.Resources.diamond);

bool isSame = array1.Length == array2.Length;

if ( isSame )
for ( int index = 0; index < array1.Length; index++)
if ( array1[index] != array2[index] )
{
isSame = false;
break;
}

if ( isSame )
{
...
}

Also : How to compare two images?

Comparing two images problem

http://en.wikipedia.org/wiki/BMP_file_format#Bitmap_file_header

Read 4 bytes at offset 10 to obtain the offset of the pixel data. Read 4 bytes at offset 2 to get the size of the entire thing including header. Subtract the former from the latter to get the size of the pixel data.

best way to compare images for similarity in android

First off, let's correct you. Neither your OpenCV snippet not Android can directly compare if two images are "similar". They can compare if they are exactly the same. That's not the same thing. You'd have to decide if its good enough.

Secondly, OpenCV is overkill for this. If the two images are Bitmaps in memory, just loop over the byte arrays of the two files. If they're on disk, just compare the byte by byte data of the two files.

You said you "got the paths of all images, then converted to Bitmap". Yeah, that would take a ton of memory. Instead, if you want to compare all the files, do this:

val map = mutableMapOf()
fileNames.each {
val hash = hash_file(it)
if (map.contains(hash)) {
//In this case, the two file stored in it and map[hash] are the same
}
else {
map[hash] = it
}
}

Here hash_file is any well known hash function. MD5 would work fine.

Now if you actually want similarity- good luck, you're going to need to learn a lot of AI and machine learning to determine that. Or find someone who already has a model for an appropriate training set.

Comparing two ByteArrays C#

If you want to know why this happens, this is because ASCII encoding can't handle characters above 128. The first four characters are converted to '?'.

Since you have an image in the byte array, you shouldn't try to convert it to text in order to compare the two arrays. For comparison's sake, you should iterate through all bytes and print their values. It would be better to use hex notation for this.

Comparing two byte arrays in .NET

Edit: modern fast way is to use a1.SequenceEquals(a2)

User gil suggested unsafe code which spawned this solution:

// Copyright (c) 2008-2013 Hafthor Stefansson
// Distributed under the MIT/X11 software license
// Ref: http://www.opensource.org/licenses/mit-license.php.
static unsafe bool UnsafeCompare(byte[] a1, byte[] a2) {
unchecked {
if(a1==a2) return true;
if(a1==null || a2==null || a1.Length!=a2.Length)
return false;
fixed (byte* p1=a1, p2=a2) {
byte* x1=p1, x2=p2;
int l = a1.Length;
for (int i=0; i < l/8; i++, x1+=8, x2+=8)
if (*((long*)x1) != *((long*)x2)) return false;
if ((l & 4)!=0) { if (*((int*)x1)!=*((int*)x2)) return false; x1+=4; x2+=4; }
if ((l & 2)!=0) { if (*((short*)x1)!=*((short*)x2)) return false; x1+=2; x2+=2; }
if ((l & 1)!=0) if (*((byte*)x1) != *((byte*)x2)) return false;
return true;
}
}
}

which does 64-bit based comparison for as much of the array as possible. This kind of counts on the fact that the arrays start qword aligned. It'll work if not qword aligned, just not as fast as if it were.

It performs about seven timers faster than the simple `for` loop. Using the J# library performed equivalently to the original `for` loop. Using .SequenceEqual runs around seven times slower; I think just because it is using IEnumerator.MoveNext. I imagine LINQ-based solutions being at least that slow or worse.


Related Topics



Leave a reply



Submit