Android image comparison (image similarity) code

Source: Internet
Author: User

Statement

These two days in the picture Contrast tool, here will compare the core function code to learn together, what shortcomings and shortcomings please point out, thank you. Lee produced, reproduced please specify the source: http://blog.csdn.net/hnulwt/article/details/43668161

Objective

The way I compare images is through pixel-by-point comparisons. Due to the size limit of Android memory, we may also consider the memory problem when we do the picture comparison, because the larger picture loading may cause oom. Online search, the vast majority of the use of compressed image quality to achieve the purpose.
However, this article does not consider the image size itself, because the current need to compare the picture is not large enough, if you encounter a special large picture and need to compare, then you may consider a certain optimization scheme (at present, if the picture is too large, you can continue to try to split the pixel value of each row, further let int[] Become smaller. But if the picture is too large, get bitmap on Oom, understand that you can get the image size, not directly loading the image into memory, you can try this way, but there is no research, in the sub-right when the discussion, will also write a blog to further record how to optimize.

Code explanation

This section of the code is mainly by line to get bitmap pixel points to compare, and ultimately return the image of the similarity (if the size of the two images are inconsistent, then directly return the size of the inconsistency).

Code
ImportJava.text.DecimalFormat;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.BitmapFactory; Public  class bitmapcompare{    Private Static FinalString different_size ="Differentsize";Private Static FinalString Result_format ="00.0%"; Public StaticStringSimilarity(String url1, String url2)        {Bitmap BM1 = Bitmapfactory.decodefile (URL1); Bitmap bm2 = Bitmapfactory.decodefile (URL2);returnSimilarity (BM1, BM2); } Public StaticStringSimilarity(Bitmap bm1, Bitmap bm2) {Final intBm1width = Bm1.getwidth ();Final intBm2width = Bm2.getwidth ();Final intBmheight = Bm1.getheight ();if(Bmheight! = bm2.getheight () | | Bm1width! = bm2width)returnDifferent_size;int[] Pixels1 =New int[Bm1width];int[] Pixels2 =New int[Bm2width]; Reset (); for(inti =0; i < bmheight; i++) {bm1.getpixels (PIXELS1,0, Bm1width,0, I, Bm1width,1); Bm2.getpixels (PIXELS2,0, Bm2width,0, I, Bm2width,1);        Comparepixels (PIXELS1, PIXELS2, bm1width); }returnPercent (Count.st, COUNT.SF + count.st); }Private Static void Comparepixels(int[] PIXELS1,int[] PIXELS2,intLength) { for(inti =0; i < length; i++) {if(Pixels1[i] = = Pixels2[i])            {count.st++; }Else{count.sf++; }        }    }Private StaticStringpercent(intDivisorintDividend) {Final DoubleValue = divisor *1.0/dividend; DecimalFormat DF =NewDecimalFormat (Result_format);returnDf.format (value); }Private Static void Reset() {count.st =0; COUNT.SF =0; }Private Static  class Count {        Private Static intST;Private Static intSF; }}
End

In the image comparison process, the most violent way is to directly get the whole picture of the pixel value, put into the int[] array, and then to compare, of course, this way shortcomings obvious, not to say. In the above code, the Bitmap.getpixel method is still need to study, if you do not understand, please poke: bitmap.getpixel () method learning, there are other problems can communicate with each other.

Android image comparison (image similarity) code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.