{"id":1530,"date":"2015-07-03T22:47:57","date_gmt":"2015-07-03T14:47:57","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1530"},"modified":"2015-07-05T22:11:18","modified_gmt":"2015-07-05T14:11:18","slug":"opencv-and-processing-14","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/07\/03\/opencv-and-processing-14\/","title":{"rendered":"OpenCV and Processing 14"},"content":{"rendered":"<p>This example continues to explore the Video module in OpenCV. It uses one of the BackgroundSubtractors, the BackgroundSubtractorKNN. It learns the motion in front of the camera and treats the stationary scene as background.<\/p>\n<p>In the code, the important command is <\/p>\n<pre lang=\"java\">\r\nbkg.apply(frame, fgmask);\r\n<\/pre>\n<p>The subtractor object <em>bkg<\/em> takes in the latest <em>frame<\/em> and generates a foreground mask, <em>fgmask<\/em>. We can use the foreground mask to single out the foreground moving object.<\/p>\n<p>Here are a number of screen shots from the testing. The background is the planet Earth image from NASA.<br \/>\n<div id='gallery-1' class='gallery galleryid-1530 gallery-columns-3 gallery-size-thumbnail'><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0506.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0506-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/div><\/figure><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0889.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0889-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/div><\/figure><figure class='gallery-item'>\n\t\t\t<div class='gallery-icon landscape'>\n\t\t\t\t<a href='http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo1188.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo1188-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/div><\/figure>\n\t\t<\/div>\n<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\nimport processing.video.*;\r\nimport org.opencv.video.Video;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.Scalar;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.video.BackgroundSubtractorKNN;\r\n\r\nCapture cap;\r\nCVImage img;\r\nPImage earth;\r\nBackgroundSubtractorKNN bkg;\r\nMat fgimg, fgmask;\r\n\r\nvoid setup() {\r\n  size(640, 480, P3D);\r\n  background(0);\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n  cap.read();\r\n  img = new CVImage(cap.width, cap.height);\r\n  bkg = Video.createBackgroundSubtractorKNN();\r\n  fgimg = new Mat(cap.height, cap.width, CvType.CV_8UC4);\r\n  fgmask = new Mat(fgimg.size(), CvType.CV_8UC1);\r\n  earth = loadImage(\"background.jpg\");\r\n}\r\n\r\nvoid draw() {\r\n  img.copy(cap, 0, 0, cap.width, cap.height, 0, 0, img.width, img.height);\r\n  img.toCV();\r\n  Mat frame = img.getBGRA();\r\n  bkg.apply(frame, fgmask);\r\n  fgimg.setTo(Scalar.all(0));\r\n  frame.copyTo(fgimg, fgmask);\r\n  img.fromCV(fgimg);\r\n\r\n  background(0);\r\n  image(earth, 0, 0);\r\n  image(img, 0, 0);\r\n  text(\"Frame rate: \" + nf(round(frameRate), 2), 10, 20, 0);\r\n  frame.release();\r\n}\r\n\r\nvoid captureEvent(Capture _c) {\r\n  _c.read();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example continues to explore the Video module in OpenCV. It uses one of the BackgroundSubtractors, the BackgroundSubtractorKNN. It learns the motion in front of the camera and treats the stationary scene as background. In the code, the important command is bkg.apply(frame, fgmask); The subtractor object bkg takes in the latest frame and generates a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,79],"tags":[135,3,62],"class_list":["post-1530","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-backgroundsubtractor","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1530"}],"version-history":[{"count":4,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1530\/revisions"}],"predecessor-version":[{"id":1553,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1530\/revisions\/1553"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1530"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}