http://blog.csdn.net/ghd2000/article/details/6371637
#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; }
最近在研究在用C来处理图片,后来再网上找了一下,有的说用libjpeg和opencv来处理图片。先研究一下,opencv来处理图片,
这是网上一位友人写的代码http://blogold.chinaunix.net/u1/57901/showart.php?id=2512389
开始之前,先编译opencv得到 libandroid-opencv.so库
1、创建一个android工程,工程名为opencvtest,包名为com.opencvtest
在工程的根目录下创建一个jni文件夹,然后分别创建Android.mk和hello-jni.cpp两个文件
Android.mk的内容如下:
- #
date: Summer, 2010 - #
author: Ethan Rublee - #
contact: ethan.rublee@gmail.com - #
- LOCAL_PATH
:= $(call my-dir) -
- include
$(CLEAR_VARS) -
- #OPENCV_ROOT
must be defined. - ifndef
OPENCV_ROOT - $(warning
Please define OPENCV_ROOT to point to the root folder of opencv, try ndk-build OPENCV_ROOT=../../opencv) - OPENCV_ROOT=../../opencv
- $(warning
Defaulting to OPENCV_ROOT=$(OPENCV_ROOT)) - endif
-
-
- #define
OPENCV_INCLUDES - include
$(OPENCV_ROOT)/includes.mk - #define
OPENCV_LIBS - include
$(OPENCV_ROOT)/libs.mk -
- LOCAL_LDLIBS
+= $(OPENCV_LIBS) -
- LOCAL_C_INCLUDES
+= $(OPENCV_INCLUDES) $(ANDROID_OPENCV_INCLUDES) -
- LOCAL_MODULE
:= hello-jni -
- LOCAL_SRC_FILES
:= hello-jni.cpp -
- include
$(BUILD_SHARED_LIBRARY)
2、hello-jni.cpp
- #include
- #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
- package
com.opencv; -
- import
android.app.Activity; - import
android.os.Bundle; - import
android.widget.TextView; -
- public
class ActivityMain extends Activity { -
-
-
private TextView text; -
-
@Override -
public void onCreate(Bundle savedInstanceState) { -
super.onCreate(savedInstanceState); -
setContentView(R.layout.main); -
-
text = (TextView) findViewById(R.id.text); -
text.setText(Integer.toString(getKeypointNum())); -
-
} -
-
public native int getKeypointNum(); -
-
static -
{ -
-
System.loadLibrary("hello-jni"); -
} -
-
- }
4、把这个工程编译了,工程结构
5、效果如图:原图
效果图:
6、注意,如果处理大的图片的时候会出现异常。没有响应,不知道是什么问题!求高手解答