Home > Mobile >  best way to compare images for similarity in android
best way to compare images for similarity in android

Time:05-16

how to compare two images, to know are they similar for 100%? I was getting path of all images from mediastore, then converted to bitmap and compared using bitmap.sameAs(bitmapToCompare), but it takes to much memory and got outofmemory exepcetion Now i am trying to use OpenCv library as:

  val img1: Mat = Imgcodecs.imread(it)
                    val img2: Mat = Imgcodecs.imread(it1)
                    val result = Mat()
                    Core.compare(img1, img2, result, Core.CMP_EQ)
     val ines = Core.countNonZero(result)
                        if(ines==0){
//similar images

}

but get an error in Core.countNonZero as following:

cv::Exception: OpenCV(4.5.3) /home/quickbirdstudios/opencv/releases/opencv-4.5.3/modules/core/src/count_non_zero.dispatch.cpp:128: error: (-215:Assertion failed) cn == 1 in function 'countNonZero'

so what is best way to compare two images?

CodePudding user response:

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.

  • Related