{"id":1537,"date":"2015-07-05T14:38:02","date_gmt":"2015-07-05T06:38:02","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1537"},"modified":"2015-07-05T22:10:48","modified_gmt":"2015-07-05T14:10:48","slug":"opencv-and-processing-15","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/07\/05\/opencv-and-processing-15\/","title":{"rendered":"OpenCV and Processing 15"},"content":{"rendered":"<p>The coming example will be the sparse optical flow. Before that, we first work on the 2D feature points tracking. The function <strong>goodFeaturesToTrack()<\/strong> belongs to the Imgproc module. It takes in a greyscale image and identifies the feature points (corners) as a matrix of point, <strong>MatOfPoint<\/strong>. The sample code here uses the feature points to render a live graphics of the webcam image.<\/p>\n<p><div id='gallery-1' class='gallery galleryid-1537 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\/photo0432.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0432-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\/photo0819.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0819-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\/photo0878.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0878-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\/photo1858.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo1858-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\/photo2782.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo2782-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\n\r\nimport org.opencv.video.Video;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.TermCriteria;\r\nimport org.opencv.core.MatOfPoint;\r\nimport org.opencv.core.MatOfPoint2f;\r\nimport org.opencv.core.Point;\r\nimport org.opencv.core.Size;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.imgproc.Imgproc;\r\n\r\nfinal int MAX_COUNT = 400;\r\nCapture cap;\r\nCVImage img;\r\nTermCriteria term;\r\nSize subPixWinSize, winSize;\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  term = new TermCriteria(TermCriteria.COUNT | TermCriteria.EPS, 20, 0.03);\r\n  subPixWinSize = new Size(10, 10);\r\n  winSize = new Size(31, 31);\r\n  noStroke();\r\n  smooth();\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  background(0);\r\n  \r\n  Mat grey = img.getGrey();\r\n  MatOfPoint corners = new MatOfPoint();\r\n  Imgproc.goodFeaturesToTrack(grey, corners, MAX_COUNT, 0.01, 10, new Mat(), 3, false, 0.04);\r\n  if (corners.toArray().length>0) {\r\n    MatOfPoint2f points = new MatOfPoint2f(corners.toArray());\r\n    Imgproc.cornerSubPix(grey, points, subPixWinSize, new Size(-1, -1), term);\r\n    if (points.toArray().length>0) {\r\n      for (Point p : points.toArray()) {\r\n        int x = constrain((int)p.x, 0, cap.width-1);\r\n        int y = constrain((int)p.y, 0, cap.height-1);\r\n        color col = cap.pixels[y*cap.width+x];\r\n        fill(red(col), green(col), blue(col), random(100, 220));\r\n        float s = random(5, 40);\r\n        ellipse(x, y, s, s);\r\n      }\r\n    }\r\n  }\r\n  \r\n  fill(255);\r\n  text(\"Frame rate: \" + nf(round(frameRate), 2), 10, 20, 0);\r\n}\r\n\r\nvoid captureEvent(Capture _c) {\r\n  _c.read();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The coming example will be the sparse optical flow. Before that, we first work on the 2D feature points tracking. The function goodFeaturesToTrack() belongs to the Imgproc module. It takes in a greyscale image and identifies the feature points (corners) as a matrix of point, MatOfPoint. The sample code here uses the feature points to [&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":[128,3,62],"class_list":["post-1537","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-image-processing","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1537","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=1537"}],"version-history":[{"count":2,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions"}],"predecessor-version":[{"id":1552,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1537\/revisions\/1552"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1537"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}