標籤:opencv orb mattobitma
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:
參考:
1.opencv ORB
2.Drawing Function of Keypoints and Matches
3.OpenCV Tutorial 2: Mixed Processing
4.Feature Description
5.Feature Detection and Description
OpenCV4Android 特徵點提取示範