我尝试比较相机拍摄的纸币是否与资产文件的纸币图片相同。
现在我只能比较一张钞票图像。 compare banknotes
我想像下一个视频一样实现它。
native-lib.cpp
std::vector<Mat> trainImages;
std::vector< std::vector<DMatch> > knn_matches;
std::vector<KeyPoint> keypoints1;
std::vector<KeyPoint> keypoints2;
Mat matImg;
extern "C"
JNIEXPORT void JNICALL
Java_com_example_surfwithflann2_MainActivity_sendImages(JNIEnv *env, jobject instance,
jlongArray tempAddrObj_) {
if(trainImages.size() == 0) {
int length = env->GetArrayLength(tempAddrObj_);
jlong *tempAddrObj = env->GetLongArrayElements(tempAddrObj_, NULL);
for(int i = 0; i < length; i++) {
Mat &tempImage = *(Mat *)tempAddrObj[i];
trainImages.push_back(tempImage);
}
env->ReleaseLongArrayElements(tempAddrObj_, tempAddrObj, 0);
} else {
__android_log_print(ANDROID_LOG_DEBUG, "TAG", "already received from java");
}
}
static void createKeypointsAndDescriptors(const Mat& matInput) {
int minHessian = 400;
cv::Ptr<SURF> detector = SURF::create( minHessian );
std::vector<KeyPoint> temp_keypoints;
std::vector<std::vector<KeyPoint>> temp_keypoints1;
Mat temp_descriptors;
std::vector<Mat> temp_descriptors1;
Mat descriptors2;
std::vector< std::vector<DMatch> > temp_knn_matches;
for(int i = 0; i < 2; i++) {
detector->detectAndCompute( trainImages[i], noArray(), temp_keypoints, temp_descriptors );
temp_keypoints1.push_back(temp_keypoints);
temp_descriptors1.push_back(temp_descriptors);
}
detector->detectAndCompute( matInput, noArray(), keypoints2, descriptors2 );
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
int max = 0;
for(int i = 0; i < 2; i++) {
if(temp_keypoints[i].size >= 2 && keypoints2.size() >= 2)
matcher->knnMatch( temp_descriptors1[i], descriptors2, temp_knn_matches, 2 );
if(max < temp_knn_matches.size()) {
max = temp_knn_matches.size();
keypoints1 = temp_keypoints1[i];
matImg = trainImages[i];
knn_matches = temp_knn_matches;
}
}
}
extern "C"
JNIEXPORT jlong JNICALL
Java_com_example_surfwithflann2_MainActivity_surfWithFlann2(JNIEnv *env,
jobject instance, jlong matAddrInput) {
// TODO
Mat &matInput = *(Mat *)matAddrInput;
createKeypointsAndDescriptors(matInput);
//-- Filter matches using the Lowe's ratio test
const float ratio_thresh = 0.7f;
std::vector<DMatch> good_matches;
for (int i = 0; i < knn_matches.size(); i++)
{
if (knn_matches[i][0].distance < ratio_thresh * knn_matches[i][1].distance)
{
good_matches.push_back(knn_matches[i][0]);
}
}
//-- Draw matches
Mat *img_matches = new Mat();
drawMatches( matImg, keypoints1, matInput, keypoints2, good_matches, *img_matches, Scalar::all(-1),
Scalar::all(-1), std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
matInput.release();
return (jlong)img_matches;
}
MainActivity.java
private Mat matInput;
private Mat matResult;
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
matInput = inputFrame.rgba();
Imgproc.cvtColor(matInput, matInput, Imgproc.COLOR_RGB2GRAY);
long result = surfWithFlann2(matInput.getNativeObjAddr());
matResult = new Mat(result);
Log.d(TAG, "check");
Imgproc.resize(matResult, matResult, matInput.size());
return matResult;
}
两个错误交替发生。
1。 OpenCV错误:断言失败(dsize.area()> 0 ||(inv_scale_x> 0 && inv_scale_y> 0))在无效cv :: resize(cv :: InputArray,cv :: OutputArray,cv :: Size,double,double,int),文件../../modules/imgproc/src/resize.cpp,4045行
2。结果表明,函数createKeypointsAndDescriptors()中没有keypoints2和描述符2