Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. private int detectMotion(Mat motion, Mat result, Mat result_cropped,
  2. int x_start, int x_stop, int y_start, int y_stop,
  3.  
  4. Scalar color) {
  5.  
  6. int number_of_changes = 0;
  7. int min_x = motion.cols(), max_x = 0;
  8. int min_y = motion.rows(), max_y = 0;
  9. long startSysMillis = System.currentTimeMillis();
  10. // loop over image and detect changes
  11. for (int j = y_start; j < y_stop; j += 2) { // height
  12. for (int i = x_start; i < x_stop; i += 2) { // width
  13. // check if at pixel (j,i) intensity is equal to 255
  14. // this means that the pixel is different in the sequence
  15. // of images (prev_frame, current_frame, next_frame)
  16.  
  17. int testme = (int) motion.get(j, i)[0];
  18.  
  19.  
  20. if (testme == 255) {
  21. number_of_changes++;
  22. if (min_x > i) {
  23. min_x = i;
  24. }
  25. if (max_x < i) {
  26. max_x = i;
  27. }
  28. if (min_y > j) {
  29. min_y = j;
  30. }
  31. if (max_y < j) {
  32. max_y = j;
  33. }
  34. }
  35. }
  36. System.out.println((System.currentTimeMillis() - startSysMillis) + " millis");
  37. }
  38. if (number_of_changes > 0) {
  39. //check if not out of bounds
  40. if (min_x - 10 > 0) {
  41. min_x -= 10;
  42. }
  43. if (min_y - 10 > 0) {
  44. min_y -= 10;
  45. }
  46. if (max_x + 10 < result.cols() - 1) {
  47. max_x += 10;
  48. }
  49. if (max_y + 10 < result.rows() - 1) {
  50. max_y += 10;
  51. }
  52. // draw rectangle round the changed pixel
  53. Point x = new Point(min_x, min_y);
  54. Point y = new Point(max_x, max_y);
  55. Rect rect = new Rect(x, y);
  56. Mat cropped = new Mat(result, rect);
  57. cropped.copyTo(result_cropped);
  58. Imgproc.rectangle(result, x, y, color, 1);
  59.  
  60.  
  61. return number_of_changes;
  62.  
  63. }
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement