I am relative new to java and i need help to use matrix with async Task. I am using input frame from onCameraFrame
to analysis in background. I will provide the method and async Task.
public int recognize(Mat inputFrame) {
int detector=0;
Mat gray= inputFrame;
if (startedFrame){
BRISK.detect(inputFrame,keypoints1);
BRISK.compute(inputFrame,keypoints1,deskriptor1);
startedFrame=false;
return 0;
}
BRISK.detect(inputFrame,keypoints2);
BRISK.compute(inputFrame,keypoints2,deskriptor2);
List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();
matcher.knnMatch(deskriptor1, deskriptor2, matches,2);
//Calculating good match list...
LinkedList<DMatch> goodMatchesList = new LinkedList<DMatch>();
for (int i = 0; i < matches.size(); i++) {
MatOfDMatch matofDMatch = matches.get(i);
DMatch[] dmatcharray = matofDMatch.toArray();
DMatch m1 = dmatcharray[0];
DMatch m2 = dmatcharray[1];
if (m1.distance <= m2.distance * nndrRatio) {
goodMatchesList.addLast(m1);
}
}
if (goodMatchesList.size() >= 7) {
List<KeyPoint> controlKeypointlist = keypoints1.toList();
List<KeyPoint> liveKeypointlist = keypoints2.toList();
LinkedList<Point> objectPoints = new LinkedList<>();
LinkedList<Point> scenePoints = new LinkedList<>();
for (int i = 0; i < goodMatchesList.size(); i++) {
objectPoints.addLast(controlKeypointlist.get(goodMatchesList.get(i).queryIdx).pt);
scenePoints.addLast(liveKeypointlist.get(goodMatchesList.get(i).trainIdx).pt);
}
MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();
objMatOfPoint2f.fromList(objectPoints);
MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();
scnMatOfPoint2f.fromList(scenePoints);
detector=1;
keypoints2.release();
deskriptor2.release();
return 2;
}
return 1;
}
class MyTask extends AsyncTask<Mat, Void, Integer> {
@Override
protected Integer doInBackground(Mat... mats) {
return recognize(mats);
}
}
And problem is in return recognize(mats);
,in parametar mats
where it say:recognize (org.opencv.core.Mat) in MainActivity cannot be applied to (org.opencv.core.Mat[]) and i do not know how to solve it?Thanks in advance.
The parameter Mat...
is a varargs parameter and acts like an array. (ie Mat[]
).
To get the first Mat of the arguments use mats[0]
:
@Override
protected Integer doInBackground(Mat... mats) {
return recognize(mats[0]);
}
In Java Mat...
represents an array of Mat
class type. If you look at the error
recognize (org.opencv.core.Mat) in MainActivity cannot be applied to (org.opencv.core.Mat[])
You'll notice that it says exactly the same thing ie you are passing an Array to a method which takes a single instance. So all you need to do is:
return recognize(mats[0]);
get the Mat
at the first index of this array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.