Use asift on Android to achieve more robust feature matching for visual changes

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

This evening is Christmas Eve, with manyFour-Eye technology houseIn the same way, we have a holiday with the computer ......

I explained how to use ndk to convert a color image to a grayscale image on Android. Now I can port the asift example of Windows Mobile to Android ....... here, I would like to thank Jean-micel Morel and guoshen Yu for their selfless dedication and respect for knowledge and the Open Source spirit.

Let's take a look at the program running in this article:

 

The picture on the left shows the result with the lowest recognition rate and the picture on the right shows the result with a low recognition rate.

The code for this article can be downloaded here: http://www.pudn.com/downloads314/sourcecode/comm/android/detail1391871.html

Here, asift's ndk code (C ++) is roughly the same as the DLL code in WM, but there are some differences:

1. JNI does not support reference transfer, so some values must be returned through the function, for example:

/** <Br/> * obtain the enlarged/reduced image size <br/> */<br/> jniexport jintarray jnicall java_com_testasift_libasift_getzoomsize (<br/> jnienv * ENV, jobject OBJ) {<br/> jint arrint [2]; <br/> arrint [0] = im_x; <br/> arrint [1] = im_y; <br/> jintarray result = env-> newintarray (2); <br/> env-> setintarrayregion (result, 0, 2, arrint); <br/> return result; <br/>}< br/>/** <br/> * return the size of the matched image. jintarray [0] is width, jintarray [1] is height <br/> */<br/> jniexport jintarray jnicall java_com_testasift_libasift_getmatchedimagesize (<br/> jnienv * ENV, jobject OBJ) {<br/> jint arrint [2]; <br/> arrint [0] = Wo; <br/> arrint [1] = Ho; <br/> jintarray result = env-> newintarray (2); <br/> env-> setintarrayregion (result, 0, 2, arrint); <br/> return result; <br/>}

2. asift receives an 8-bit grayscale image. Before use, it must be converted to an 8-bit grayscale image:

Void pixeltovector (jint * cbuf, int W, int H, STD: vector <float> * ipixels) {<br/> for (INT I = 0; I <p; I ++) {<br/> for (Int J = 0; j <W; j ++) {<br/> // obtain the pixel color <br/> int color = cbuf [w * I + J]; <br/> int Red = (color & 0x00ff0000)> 16); <br/> int Green = (color & 0x0000ff00)> 8 ); <br/> int Blue = color & 0x000000ff; <br/> color = (Red + green + blue)/3; <br/> ipixels-> push_back (color ); // Save the grayscale value <br/>}< br/>

After use, convert the 8bit grayscale image into rgb565:

Jintarray result = env-> newintarray (WO * Ho); <br/> jint * cresult; <br/> cresult = env-> getintarrayelements (result, false ); <br/> int alpha = 0xff <24; <br/> for (INT I = 0; I <Ho; I ++) {<br/> for (Int J = 0; j <wo; j ++) {<br/> // obtain the pixel color <br/> int color = (INT) opixelsasift [wo * I + J]; <br/> color = Alpha | (color <16) | (color <8) | color; <br/> cresult [wo * I + J] = color; <br/>}< br/> env-> releaseintarrayelements (result, cresult, 0 );

The logic code of the main class testasift. Java is as follows:

Public class testasift extends activity {<br/>/** called when the activity is first created. */<br/> imageview imgview; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> This. settitle ("use asift --- hellogv on Android"); <br/> imgview = (imageview) This. findviewbyid (R. id. imageview01); </P> <p> libasift. initzoomsize (320,480); // scale the target size <br/> int [] size = libasift. getzoomsize (); // determines whether the setting is successful <br/> log. E (string. valueof (size [0]), String. valueof (size [1]); </P> <p> bitmap img1 = (bitmapdrawable) getresources (). getdrawable (R. drawable. adam1 )). getbitmap (); <br/> int W1 = img1.getwidth (), H1 = img1.getheight (); <br/> int [] pix1 = new int [W1 * H1]; <br/> img1.getpixels (pix1, 0, W1, 0, 0, W1, H1); <br/> // extract the features of the first image <br/> libasift. initimage1 (pix1, W1, H1, 2); <br/> bitmap img2 = (bitmapdrawable) getresources (). getdrawable (R. drawable. adam2 )). getbitmap (); <br/> int W2 = img2.getwidth (), H2 = img2.getheight (); <br/> int [] pix2 = new int [W2 * H2]; <br/> img2.getpixels (pix2, 0, W2, 0, 0, W2, H2); <br/> int [] imgpixels = libasift. match2imageforimg (pix2, W2, H2, 2); // match the two graphs <br/> int [] imgsize = libasift. getmatchedimagesize (); // size of the matching result graph <br/> bitmap imgresult = bitmap. createbitmap (imgsize [0], imgsize [1], config. rgb_565); <br/> imgresult. setpixels (imgpixels, 0, imgresult. getwidth (), 0, 0, imgresult. getwidth (), imgresult. getheight (); <br/> imgview. setimagebitmap (imgresult); // display the result </P> <p >}< br/>}

Related Article

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.