1

I have devised some code for image comparison. The matching part is still a bit flawed and I would love some assitance. The project can be found at - Github .

I have these two images img1 and img2 .

enter image description here enter image description here

These are the images of same person . When I want to compare these two images , the following code returns false .

bmpimg1 = Bitmap.createScaledBitmap(bmpimg1, 100, 100, true);
                    bmpimg2 = Bitmap.createScaledBitmap(bmpimg2, 100, 100, true);
                    Mat img1 = new Mat();
                    Utils.bitmapToMat(bmpimg1, img1);
                    Mat img2 = new Mat();
                    Utils.bitmapToMat(bmpimg2, img2);
                    Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGBA2GRAY); 
                    Imgproc.cvtColor(img2, img2, Imgproc.COLOR_RGBA2GRAY); 
                    img1.convertTo(img1, CvType.CV_32F);
                    img2.convertTo(img2, CvType.CV_32F);
                    //Log.d("ImageComparator", "img1:"+img1.rows()+"x"+img1.cols()+" img2:"+img2.rows()+"x"+img2.cols());
                    Mat hist1 = new Mat();
                    Mat hist2 = new Mat();
                    MatOfInt histSize = new MatOfInt(180);
                    MatOfInt channels = new MatOfInt(0);
                    ArrayList<Mat> bgr_planes1= new ArrayList<Mat>();
                    ArrayList<Mat> bgr_planes2= new ArrayList<Mat>();
                    Core.split(img1, bgr_planes1);
                    Core.split(img2, bgr_planes2);
                    MatOfFloat histRanges = new MatOfFloat (0f, 180f);              
                    boolean accumulate = false;
                    Imgproc.calcHist(bgr_planes1, channels, new Mat(), hist1, histSize, histRanges, accumulate);
                    Core.normalize(hist1, hist1, 0, hist1.rows(), Core.NORM_MINMAX, -1, new Mat());
                    Imgproc.calcHist(bgr_planes2, channels, new Mat(), hist2, histSize, histRanges, accumulate);
                    Core.normalize(hist2, hist2, 0, hist2.rows(), Core.NORM_MINMAX, -1, new Mat());
                        img1.convertTo(img1, CvType.CV_32F);
                        img2.convertTo(img2, CvType.CV_32F);
                        hist1.convertTo(hist1, CvType.CV_32F);
                        hist2.convertTo(hist2, CvType.CV_32F);

                        double compare = Imgproc.compareHist(hist1, hist2, Imgproc.CV_COMP_CHISQR);
                        Log.d("ImageComparator", "compare: "+compare);
                        if(compare>0 && compare<1500) {
                            Toast.makeText(MainActivity.this, "Images may be possible duplicates, verifying", Toast.LENGTH_LONG).show();
                            new asyncTask(MainActivity.this).execute();
                        }
                        else if(compare==0)
                            Toast.makeText(MainActivity.this, "Images are exact duplicates", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(MainActivity.this, "Images are not duplicates", Toast.LENGTH_LONG).show();

                    startTime = System.currentTimeMillis();

How can I change my code so that on compare these two images , it returns true ? Any advice is of great help .

1 Answers1

0

You seem to only be comparing limited feature vectors, which in this case is only a histogram of the image. The algorithm you have used is not suitable for facial recognition as it only identifies on the image color spectrum.

See this possible duplicate on how to perform facial recognition.

Community
  • 1
  • 1
agomes
  • 331
  • 2
  • 8