OpenCV4Android 特征点提取演示
前面通过两天的时间,也只是熟悉了基本的环境搭建,明确了基本的部件流程,接下来需要熟悉API,进行实际的应用编程。本篇尝试获取图片的SIFT特征点,并学习相应的API及图像处理基本知识。
目标:
- 定义Native method 接口
- Bitmap 和 opencv Mat 之间的转换
- 通过org.opencv.core.Mat.getNativeObjAddr()把地址传递给底层的C++代码处理,然后更新对应地址的对象
关键代码:
Android中的事件处理:
btn_feature_detect.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Mat mRgba = new Mat(height, width, CvType.CV_8U, new Scalar(4));
Mat mGray = new Mat(height, width, CvType.CV_8U, new Scalar(4));
Mat output = new Mat();
Utils.bitmapToMat(bitmap, mRgba);
// do sth
// Converts an image from one color space to another.
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGB2GRAY, 4);
NativeUtil.detectFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr(), output.getNativeObjAddr());
// Then convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(output.cols(),
output.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(output, resultBitmap);
imageFeatureView.setImageBitmap(resultBitmap);
}
});
C++中利用opencv的处理:
JNIEXPORT void JNICALL Java_com_example_test_NativeUtil_detectFeatures(
JNIEnv *env, jclass thiz, jlong mGrayAddr, jlong mRgbaAddr, jlong mOutputAddr) {
Mat* pMatGr=(Mat*)mGrayAddr;
Mat* pMatRgb=(Mat*)mRgbaAddr;
Mat* pMatDesc=(Mat*)mOutputAddr;
vector<KeyPoint> v;
//OrbFeatureDetector detector(50);
OrbFeatureDetector detector;
OrbDescriptorExtractor extractor;
detector.detect(*pMatGr, v);
drawKeypoints(*pMatGr, v, *pMatDesc);
}
运行效果:
点击按钮后,提取该图片的features:
参考:
2.Drawing Function of Keypoints and Matches
3.OpenCV Tutorial 2: Mixed Processing
5.Feature Detection and Description
时间: 2024-10-20 20:11:57