Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int detectMotion(Mat motion, Mat result, Mat result_cropped,
- int x_start, int x_stop, int y_start, int y_stop,
- Scalar color) {
- int number_of_changes = 0;
- int min_x = motion.cols(), max_x = 0;
- int min_y = motion.rows(), max_y = 0;
- long startSysMillis = System.currentTimeMillis();
- // loop over image and detect changes
- for (int j = y_start; j < y_stop; j += 2) { // height
- for (int i = x_start; i < x_stop; i += 2) { // width
- // check if at pixel (j,i) intensity is equal to 255
- // this means that the pixel is different in the sequence
- // of images (prev_frame, current_frame, next_frame)
- int testme = (int) motion.get(j, i)[0];
- if (testme == 255) {
- number_of_changes++;
- if (min_x > i) {
- min_x = i;
- }
- if (max_x < i) {
- max_x = i;
- }
- if (min_y > j) {
- min_y = j;
- }
- if (max_y < j) {
- max_y = j;
- }
- }
- }
- System.out.println((System.currentTimeMillis() - startSysMillis) + " millis");
- }
- if (number_of_changes > 0) {
- //check if not out of bounds
- if (min_x - 10 > 0) {
- min_x -= 10;
- }
- if (min_y - 10 > 0) {
- min_y -= 10;
- }
- if (max_x + 10 < result.cols() - 1) {
- max_x += 10;
- }
- if (max_y + 10 < result.rows() - 1) {
- max_y += 10;
- }
- // draw rectangle round the changed pixel
- Point x = new Point(min_x, min_y);
- Point y = new Point(max_x, max_y);
- Rect rect = new Rect(x, y);
- Mat cropped = new Mat(result, rect);
- cropped.copyTo(result_cropped);
- Imgproc.rectangle(result, x, y, color, 1);
- return number_of_changes;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement