您的位置:首页 > 移动开发 > Android开发

在Android中使用OpenCv 来处理图片

2011-04-29 12:02 726 查看
最近在研究在用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的内容如下:
# 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 <jni.h>
#include <cstdlib>
#include <stdexcept>
#include <string>

#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc_c.h>

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 {
    /** Called when the activity is first created. */
	
	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、注意,如果处理大的图片的时候会出现异常。没有响应,不知道是什么问题!求高手解答。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: