1 | initial version |
I hope the following code you can help (java OpenCV 2.4.10 NetBeans IDE 7.3)
int Numkadr2 = -1;
Mat Kadr1N = new Mat();
Mat Kadr2N = new Mat();
public Mat detectMotion(Mat src) {
if (Numkadr2 == -1) {
Kadr1N = src.clone();
Kadr2N = src.clone();
}
Numkadr2++;
if (Numkadr2 == 1) {
Kadr1N = src.clone();
}
if (Numkadr2 == 2) {
Kadr2N = src.clone();
Numkadr2 = 0;
}
Point pt1 = new Point();
Point pt2 = new Point();
Mat first_frame = new Mat();
Mat second_frame = new Mat();
Imgproc.cvtColor(Kadr1N, first_frame, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(Kadr2N, second_frame, Imgproc.COLOR_BGR2GRAY);
Mat flow = new Mat(Kadr1N.size(), CvType.CV_32FC2);
Video.calcOpticalFlowFarneback(first_frame, second_frame, flow, 0.5, 3, 15, 3, 5, 1.1, 0);
Mat outMat = frame.clone();
for (int i = 0; i < Kadr1N.size().height; i += 30) {
for (int j = 0; j < Kadr1N.size().width; j += 30) {
pt1.x = j;
pt1.y = i;
pt2.x = j + flow.get(i, j)[0];
pt2.y = i + flow.get(i, j)[1];
Scalar color = new Scalar(255, 255, 0, 255);
Core.line(outMat,
pt1,
pt2,
color, 2, 8, 0);
}
}
return outMat;
}
Источник : http://me10.sblo.jp/archives/20140225-1.html