{"id":1520,"date":"2015-07-03T21:42:12","date_gmt":"2015-07-03T13:42:12","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1520"},"modified":"2015-07-05T22:11:43","modified_gmt":"2015-07-05T14:11:43","slug":"opencv-and-processing-13","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/07\/03\/opencv-and-processing-13\/","title":{"rendered":"OpenCV and Processing 13"},"content":{"rendered":"<p>In this example, we move on to the Video module of OpenCV 3.0.0. The first function we test is the Dense Optical Flow. It demonstrates the use of the <strong>calcOpticalFlowFarneback<\/strong> function. Again it makes use of<a href=\"http:\/\/www.magicandlove.com\/blog\/2015\/06\/21\/opencv-and-processing-10\/\"> the previous CVImage object<\/a> to bridge between the Processing PImage and OpenCV Mat. The example also reduces the size of the video (using the variable <em>factor<\/em>) before sending it for the optical flow processing; otherwise, the process can be lengthy. <\/p>\n<p>The optical flow process will basically compare two consecutive frames (the Mat <em>last<\/em> and <em>grey<\/em>) from the live webcam video. It will try to compute where the current pixels move to in the new frame. <\/p>\n<p>Here are a number of screen shots from the sample run.<br \/>\n<div id='gallery-1' class='gallery galleryid-1520 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\/photo0130.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0130-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\/photo0395.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0395-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\/photo0623.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0623-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.Mat;\r\n\r\nCapture cap;\r\nCVImage img;\r\nMat last;\r\nint w, h;\r\nint factor;\r\n\r\nvoid setup() {\r\n  size(640, 480, P3D);\r\n  background(0);\r\n  factor = 8;\r\n  w = width\/factor;\r\n  h = height\/factor;\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n  img = new CVImage(w, h);\r\n  last = new Mat(h, w, CvType.CV_8UC1);\r\n  fill(255);\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 grey = img.getGrey();\r\n  Mat flow = new Mat(last.size(), CvType.CV_32FC2);\r\n  Video.calcOpticalFlowFarneback(last, grey, flow, 0.5, 3, 10, 2, 7, 1.5, Video.OPTFLOW_FARNEBACK_GAUSSIAN);\r\n  grey.copyTo(last);\r\n  background(0);\r\n  drawFlow(flow);\r\n  text(\"Frame rate: \" + nf(round(frameRate), 2), 10, 20, 0);\r\n  grey.release();\r\n  flow.release();\r\n}\r\n\r\nvoid drawFlow(Mat _m) {\r\n  for (int y=0; y<_m.rows (); y++) {\r\n    for (int x=0; x<_m.cols (); x++) {\r\n      double [] pt = _m.get(y, x);\r\n      double dy = pt[1];\r\n      double dx = pt[0];\r\n      if (dx == 0 &#038;&#038; dy == 0) \r\n        continue;\r\n      int x1 = x*factor;\r\n      int y1 = y*factor;\r\n      int x2 = round((x+(float)dx) * factor);\r\n      int y2 = round((y+(float)dy) * factor);\r\n      x1 = constrain(x1, 0, cap.width-1);\r\n      y1 = constrain(y1, 0, cap.height-1);\r\n      x2 = constrain(x2, 0, cap.width-1);\r\n      y2 = constrain(y2, 0, cap.height-1);\r\n      color col = cap.pixels[y1*cap.width+x1];\r\n      stroke(col);\r\n      line(x1, y1, 0, x2, y2, 0);\r\n    }\r\n  }\r\n}\r\n\r\nvoid captureEvent(Capture _c) {\r\n  _c.read();\r\n}\r\n<\/pre>\n<p>The technical reference for the algorithm is:<\/p>\n<p>Gunnar Farneb\u00e4ck. <em>Two-frame motion estimation based on polynomial expansion.<\/em> In Image Analysis, pages 363\u2013370. Springer, 2003.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we move on to the Video module of OpenCV 3.0.0. The first function we test is the Dense Optical Flow. It demonstrates the use of the calcOpticalFlowFarneback function. Again it makes use of the previous CVImage object to bridge between the Processing PImage and OpenCV Mat. The example also reduces the size [&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":[3,134,62],"class_list":["post-1520","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-opencv","tag-optical-flow","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1520","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=1520"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1520\/revisions"}],"predecessor-version":[{"id":1554,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1520\/revisions\/1554"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1520"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}