我正在尝试使用camshift方法为Android实现对象跟踪应用程序。到目前为止,我成功实现了camshift方法并在我的Android平板电脑上运行它。不幸的是,它没有按预期工作......
当我启动应用程序时,访问设备的摄像头,并选择一段图像作为跟踪对象。使用Imgproc.calcHist
函数计算对象的直方图。
之后,每次相机给出帧Imgproc.calcBackProject
时,在相机图像上使用Imgproc.calcBackProject
功能,计算对象的新位置。不幸的是,跟踪不是跟踪选定的对象,而是在屏幕的左上角漂移并停留在那里。
这是相关的代码:
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
//http://docs.opencv.org/3.1.0/db/df8/tutorial_py_meanshift.html#gsc.tab=0
//take first frame of the video
mCamera = inputFrame.rgba();
if(objectTaken == false){
// ich erstelle einen Beispielframe
trackingFrame = new Rect(mCamera.cols()/4,mCamera.rows()/4,600,500);
System.out.println("SIZZEEE: "+trackingFrame.size().toString());
System.out.println("Position: "+trackingFrame.x +" / "+ trackingFrame.y);
object = mCamera.submat(trackingFrame.x, trackingFrame.x+trackingFrame.width, trackingFrame.y, trackingFrame.y+trackingFrame.height);
hsvObject = new Mat();
Imgproc.cvtColor(object, hsvObject, Imgproc.COLOR_BGR2HSV);
lHSV = Arrays.asList(hsvObject);
//MatOfInt mChannels = new MatOfInt(0, 1);
mChannels = new MatOfInt(0);
histogram = new Mat();
histSize = new MatOfInt(180);
ranges=new MatOfFloat(0,180);
Imgproc.calcHist(lHSV, mChannels, new Mat(), histogram , histSize , ranges);
//Core.normalize(histogram, histogram, 0, 255, Core.NORM_MINMAX, -1, new Mat());
Core.normalize(histogram, histogram, 0, 255, Core.NORM_MINMAX);
//return mCamera;
objectTaken= true;
}
//set up the ROI for tracking
// grab the ROI for the bounding box and convert it to the HSV color space
//object = new Mat(mCamera,trackingFrame);
backproj = new Mat();
Imgproc.calcBackProject(lHSV, mChannels, histogram, backproj, ranges, 1);
//box = Video.CamShift(backproj, trackingFrame, new TermCriteria(TermCriteria.MAX_ITER|TermCriteria.EPS, 50, 0.001));
box = Video.CamShift(backproj, trackingFrame, new TermCriteria( TermCriteria.EPS | TermCriteria.COUNT, 10, 1 ));
trackingFrame = box.boundingRect();
pt1 =new Point(trackingFrame.x,trackingFrame.y);
pt2 =new Point(trackingFrame.x+trackingFrame.width,trackingFrame.y+trackingFrame.height);
RED = new Scalar(255,0,0);
Imgproc.rectangle(mCamera, pt1, pt2, RED);
return mCamera;
}
有人知道如何进行跟踪工作吗?我看到凸轮轴视频看起来非常有前途,我的应用程序远不及那些:(