我正在尝试使用Android和Opencv制作货币检测应用程序,我正在使用ORB检测器和knnMatcher如下:
detector = FeatureDetector.create(FeatureDetector.ORB); FastFeatureDetector.create(FastFeatureDetector.THRESHOLD,false,FastFeatureDetector.TYPE_9_16);
matcher= BFMatcher.create(BFMatcher.BRUTEFORCE_HAMMING,false);
String fullPath = "/storage/emulated/0/Currency Resources Folder/"+"50-front.jpg";
Mat blured_src = new Mat();
Mat blured_test = new Mat();
Mat resized_test = new Mat();
sizeToScale =1 ;
Imgproc.resize(mGray,resized_test,new Size(mGray.width()*sizeToScale,mGray.height()*sizeToScale));
Imgproc.medianBlur(img_src,blured_src,3);
Imgproc.medianBlur(resized_test,blured_test,3);
Mat canny_src = new Mat();
Mat canny_test = new Mat();
Imgproc.Canny(blured_src,canny_src,50,200);
Imgproc.Canny(blured_test,canny_test,50,200);
MatOfKeyPoint kp_test = new MatOfKeyPoint();
MatOfKeyPoint kp_src = new MatOfKeyPoint();
detector.detect(blured_test,kp_test);
detector.detect(blured_src,kp_src);
Mat desc_src = new Mat();
Mat desc_test = new Mat();
descriptorExtractor.compute(blured_src,kp_src,desc_src);
descriptorExtractor.compute(blured_test,kp_test,desc_test);
List<MatOfDMatch> matches = new ArrayList<MatOfDMatch>();
matcher.knnMatch(desc_test,desc_src,matches,2);
LinkedList<DMatch> good_matches = new LinkedList<>();
for (Iterator<MatOfDMatch> iterator = matches.iterator(); iterator.hasNext();)
{
MatOfDMatch next = iterator.next();
if (next.toArray()[0].distance / next.toArray()[1].distance < .92)
{
good_matches.add(next.toArray()[0]);
}
}
List<Point> pts1 = new ArrayList<Point>();
List<Point> pts2 = new ArrayList<Point>();
for(int i = 0; i<good_matches.size(); i++){
pts1.add(kp_test.toList().get(good_matches.get(i).queryIdx).pt);
pts2.add(kp_src.toList().get(good_matches.get(i).trainIdx).pt);
}
Mat outputMask = new Mat();
MatOfPoint2f pts1Mat = new MatOfPoint2f();
pts1Mat.fromList(pts1);
MatOfPoint2f pts2Mat = new MatOfPoint2f();
pts2Mat.fromList(pts2);
if (pts1Mat.toList().size() != 0 && pts2Mat.toList().size() != 0) {
Mat Homog = Calib3d.findHomography(pts1Mat, pts2Mat, Calib3d.RANSAC, 15, outputMask, 2000, 0.995);
LinkedList<DMatch> better_matches = new LinkedList<DMatch>();
for (int i = 0; i < good_matches.size(); i++) {
if (outputMask.get(i, 0)[0] != 0.0) {
better_matches.add(good_matches.get(i));
}
}
MatOfDMatch matches_mat = new MatOfDMatch();
matches_mat.fromList(better_matches);
MatOfByte mask = new MatOfByte(outputMask);
Features2d.drawMatches(mGray, kp_test, img_src, kp_src, matches_mat, output,new Scalar(255,0,0),new Scalar(0,255,255),mask,Features2d.NOT_DRAW_SINGLE_POINTS);
if (Imgcodecs.imwrite("/storage/emulated/0/Currency Resources Folder/match" + counter + ".jpg", output))
////// if(SaveImage(output,"/storage/emulated/0/Currency Resources Folder/match"+counter+".jpg"))
{
Log.e(tag, "Match image saved");
}
Log.e(tag, "matches = " + better_matches.size());
counter++;
return mRgba ;
}
我正面临着这些问题:
答案 0 :(得分:0)
您需要修改检测器的阈值,以使其对背景噪声不太敏感。通过丢弃远距离匹配项,可以进一步提高检测器的性能。