Use OpenCv in Android to process images

Source: Internet
Author: User

Recently, I was studying how to use C to process images. Later I went online and found out that some of them used libjpeg and opencv to process images. First, study opencv to process images,

This is a friend on the network to write code http://blogold.chinaunix.net/u1/57901/showart.php? Id = 2512389

 

First compile opencv to get the libandroid-opencv.so library, please refer to the blog http://blog.csdn.net/hellogv/archive/2011/01/21/6157316.aspx of this ox man

 

 

 

1. Create an android project named opencvtest and the package named com. opencvtest

Create a jni folder under the root directory of the project, and then create the Android. mk file and the content of the hello-jni.cpp file Android. mk are as follows: # Date: Summer, 2010 <br/> # author: Ethan Rublee <br/> # contact: ethan.rublee@gmail.com <br/> # <br/> LOCAL_PATH: = $ (call my-dir) </p> <p> include $ (CLEAR_VARS) </p> <p> # OPENCV_ROOT must be defined. <br/> ifndef OPENCV_ROOT <br/> $ (warning Please define OPENCV_ROOT to point to the root folder of opencv, try ndk-build OPENCV_ROOT = .. /.. /opencv) <br/> OPENCV_ROOT = .. /.. /opencv <br/> $ (warning Defaulting to OPENCV_ROOT = $ (OPENCV_ROOT )) <br/> endif </p> <p> # define OPENCV_INCLUDES <br/> include $ (OPENCV_ROOT)/includes. mk <br/> # define OPENCV_LIBS <br/> include $ (OPENCV_ROOT)/libs. mk </p> <p> LOCAL_LDLIBS + = $ (OPENCV_LIBS) </p> <p> LOCAL_C_INCLUDES + = $ (OPENCV_INCLUDES) $ (ANDROID_OPENCV_INCLUDES) </p> <p> LOCAL_MODULE: = hello-jni </p> <p> LOCAL_SRC_FILES: = hello-jni.cpp </p> <p> include $ (BUILD_SHARED_LIBRARY) </p> <p>2. hello-jni.cpp # Include <jni. h> <br/> # include <cstdlib> <br/> # include <stdexcept> <br/> # include <string> </p> <p> # include <opencv2 /core. hpp> <br/> # include <opencv2/features2d/features2d. hpp> <br/> # include <opencv2/highgui. hpp> <br/> # include <opencv2/imgproc. hpp> <br/> # include <opencv2/calib3d/calib3d. hpp> <br/> # include <opencv2/imgproc/imgproc_c.h> </p> <p> static CvScalar colors [] = <br/>{< br/> {{0, 255 }}, <br/>{{ 0,128,255 }}, <br/>{{ 0,255,255 }}, <br/>{{ 0,255, 0 }}, <br/>{{ 255,128, 0 }}, <br/>{{ 255,255, 0 }}, <br/>{{ 255, 0 }}, <br/>{{ 255, 255,255,255 }}, <br/>{{ }}</p> <p> }; </p> <p> extern "C" <br/> jint Java_com_opencv_ActivityMain_getKeypointNum (JNIEnv * env, jobject thiz) <br/>{< br/> IplImage * object = cvLoadImage ("/sdcard/camera.jpg", CV_LOAD_IMAGE_GRAYSCALE); <br/> if (object! = NULL) {<br/> CvSURFParams params = cvSURFParams (500, 1); <br/> CvMemStorage * storage = cvcreatemstorage (0 ); <br/> IplImage * object_color = cvCreateImage (cvGetSize (object), 8, 3); <br/> CvSeq * objectKeypoints = 0; <br/> CvSeq * objectDescriptors = 0; </p> <p> cvCvtColor (object, object_color, CV_GRAY2BGR); <br/> cvExtractSURF (object, 0, & objectKeypoints, & objectDescriptors, storage, params ); </p> <p> for (int I = 0; I <objectKeypoints-> total; I ++) <br/>{< br/> CvSURFPoint * r = (CvSURFPoint *) cvGetSeqElem (objectKeypoints, I); <br/> CvPoint center; <br/> int radius; <br/> center. x = cvRound (r-> pt. x); <br/> center. y = cvRound (r-> pt. y); <br/> radius = cvRound (r-> size * 1.2/9. * 2); <br/> cvCircle (object_color, center, radius, colors [0], 1, 8, 0 ); <br/>}</p> <p> cvSaveImage ("/sdcard/src_surf.jpg", object_color); </p> <p> return objectKeypoints-> total; <br/>}</p> <p> return 0; </p> <p >}</p> <p>3. ActivityMain. java Package com. opencv; </p> <p> import android. app. activity; <br/> import android. OS. bundle; <br/> import android. widget. textView; </p> <p> public class ActivityMain extends Activity {<br/>/** Called when the activity is first created. */</p> <p> private TextView text; </p> <p> @ Override <br/> public void onCreate (Bundle savedInstanceState) {<br/> super. onCreate (savedInstanceState); <br/> setContentView (R. layout. main); </p> <p> text = (TextView) findViewById (R. id. text); <br/> text. setText (Integer. toString (getKeypointNum (); </p> <p >}</p> <p> public native int getKeypointNum (); </p> <p> static <br/> {</p> <p> System. loadLibrary ("hello-jni"); <br/>}</p> <p>}4. compile the project. The project structure is 5. The effect source image is 6. Note that exceptions may occur when processing large images. No response. I don't know what the problem is! Please give answers.
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.