在Android中使用OpenCv 来处…

http://blog.csdn.net/ghd2000/article/details/6371637

 

最近在研究在用C来处理图片,后来再网上找了一下,有的说用libjpeg和opencv来处理图片。先研究一下,opencv来处理图片,

这是网上一位友人写的代码http://blogold.chinaunix.net/u1/57901/showart.php?id=2512389

 

开始之前,先编译opencv得到 libandroid-opencv.so库 ,请参考这位牛人的博客http://blog.csdn.net/hellogv/archive/2011/01/21/6157316.aspx

 

 

 

1、创建一个android工程,工程名为opencvtest,包名为com.opencvtest

在工程的根目录下创建一个jni文件夹,然后分别创建Android.mk和hello-jni.cpp两个文件
Android.mk的内容如下:
 
  1. date: Summer, 2010    
  2. author: Ethan Rublee   
  3. contact: ethan.rublee@gmail.com   
  4. #   
  5. LOCAL_PATH := $(call my-dir)  
  6.   
  7. include $(CLEAR_VARS)  
  8.   
  9. #OPENCV_ROOT must be defined.   
  10. ifndef OPENCV_ROOT  
  11. $(warning Please define OPENCV_ROOT to point to the root folder of opencv, try ndk-build OPENCV_ROOT=../../opencv)  
  12. OPENCV_ROOT=../../opencv  
  13. $(warning Defaulting to OPENCV_ROOT=$(OPENCV_ROOT))  
  14. endif  
  15.   
  16.   
  17. #define OPENCV_INCLUDES   
  18. include $(OPENCV_ROOT)/includes.mk  
  19. #define OPENCV_LIBS   
  20. include $(OPENCV_ROOT)/libs.mk  
  21.   
  22. LOCAL_LDLIBS += $(OPENCV_LIBS)  
  23.       
  24. LOCAL_C_INCLUDES +=  $(OPENCV_INCLUDES) $(ANDROID_OPENCV_INCLUDES)  
  25.   
  26. LOCAL_MODULE    := hello-jni  
  27.   
  28. LOCAL_SRC_FILES := hello-jni.cpp  
  29.   
  30. include $(BUILD_SHARED_LIBRARY)  
2、hello-jni.cpp
  1. #include    
  2. #include    
  3. #include    
  4. #include    
  5.   
  6. #include    
  7. #include    
  8. #include    
  9. #include    
  10. #include    
  11. #include    
  12.   
  13.   
  14. static CvScalar colors[]=  
  15.  
  16.             {{0,0,255}},  
  17.             {{0,128,255}},  
  18.             {{0,255,255}},  
  19.             {{0,255,0}},  
  20.             {{255,128,0}},  
  21.             {{255,255,0}},  
  22.             {{255,0,0}},  
  23.             {{255,0,255}},  
  24.             {{255,255,255}}  
  25.   
  26. };  
  27.   
  28. extern "C"  
  29. jint Java_com_opencv_ActivityMain_getKeypointNum(JNIEnv* env,jobject thiz)  
  30.  
  31.     IplImage* object cvLoadImage("/sdcard/camera.jpg",CV_LOAD_IMAGE_GRAYSCALE);  
  32.      if(object != NULL){  
  33.             CvSURFParams params cvSURFParams(500, 1);  
  34.             CvMemStorage* storage cvCreateMemStorage(0);  
  35.             IplImage* object_color cvCreateImage(cvGetSize(object), 8, 3);  
  36.             CvSeq *objectKeypoints 0;  
  37.             CvSeq *objectDescriptors 0;  
  38.   
  39.             cvCvtColor( object, object_color, CV_GRAY2BGR );  
  40.             cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params);  
  41.   
  42.          for( int 0; objectKeypoints->total; i++  
  43.           
  44.          CvSURFPoint* (CvSURFPoint*)cvGetSeqElem( objectKeypoints, );  
  45.          CvPoint center;  
  46.          int radius;  
  47.          center.x cvRound(r->pt.x);  
  48.          center.y cvRound(r->pt.y);  
  49.          radius cvRound(r->size*1.2/9.*2);  
  50.          cvCircle( object_color, center, radius, colors[0], 1, 8, );  
  51.           
  52.   
  53.             cvSaveImage( "/sdcard/src_surf.jpg" object_color);  
  54.   
  55.             return objectKeypoints->total;  
  56.          
  57.   
  58.      return 0;  
  59.   
  60.   
  61.  
#include #include #include #include #include #include #include #include #include static CvScalar colors[]= { {​{0,0,255}}, {​{0,128,255}}, {​{0,255,255}}, {​{0,255,0}}, {​{255,128,0}}, {​{255,255,0}}, {​{255,0,0}}, {​{255,0,255}}, {​{255,255,255}} }; extern "C" jint Java_com_opencv_ActivityMain_getKeypointNum(JNIEnv* env,jobject thiz) { IplImage* object = cvLoadImage("/sdcard/camera.jpg",CV_LOAD_IMAGE_GRAYSCALE); if(object != NULL){ CvSURFParams params = cvSURFParams(500, 1); CvMemStorage* storage = cvCreateMemStorage(0); IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3); CvSeq *objectKeypoints = 0; CvSeq *objectDescriptors = 0; cvCvtColor( object, object_color, CV_GRAY2BGR ); cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params); for( int i = 0; i < objectKeypoints->total; i++ ) { CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, i ); CvPoint center; int radius; center.x = cvRound(r->pt.x); center.y = cvRound(r->pt.y); radius = cvRound(r->size*1.2/9.*2); cvCircle( object_color, center, radius, colors[0], 1, 8, 0 ); } cvSaveImage( "/sdcard/src_surf.jpg" , object_color); return objectKeypoints->total; } return 0; }
3、ActivityMain.java
 
  1. package com.opencv;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class ActivityMain extends Activity  
  8.       
  9.       
  10.     private TextView text;  
  11.       
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState)  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         text (TextView) findViewById(R.id.text);  
  18.         text.setText(Integer.toString(getKeypointNum()));  
  19.           
  20.      
  21.       
  22.     public native int getKeypointNum();  
  23.       
  24.     static  
  25.      
  26.           
  27.         System.loadLibrary("hello-jni");  
  28.      
  29.       
  30.       
  31.  
4、把这个工程编译了,工程结构
 
5、效果如图:原图
效果图:
6、注意,如果处理大的图片的时候会出现异常。没有响应,不知道是什么问题!求高手解答
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值