{"id":1590,"date":"2015-07-18T23:45:13","date_gmt":"2015-07-18T15:45:13","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1590"},"modified":"2015-07-18T23:45:13","modified_gmt":"2015-07-18T15:45:13","slug":"opencv-and-processing-18","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/07\/18\/opencv-and-processing-18\/","title":{"rendered":"OpenCV and Processing 18"},"content":{"rendered":"<p>In addition to the <a href=\"http:\/\/www.magicandlove.com\/blog\/2015\/07\/18\/opencv-and-processing-17\/\">Hough circle detection<\/a>, this example works on the Hough line segment detection. It inputs the live webcam image; converts it into greyscale; applies a medianBlur filter; processes the Canny edge detection. The <strong>Imgproc.HoughLinesP()<\/strong> function will finally single out the line segments into a Mat &#8211; <em>lines<\/em> in our example codes.<\/p>\n<p><div id='gallery-1' class='gallery galleryid-1590 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\/photo0076.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0076-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\/photo0282.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0282-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\/photo0717.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0717-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.Mat;\r\nimport org.opencv.imgproc.Imgproc;\r\n\r\nCapture cap;\r\nCVImage img;\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(width, height);\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\r\n  Mat grey = img.getGrey();\r\n  Mat dst = new Mat(grey.size(), grey.type());\r\n  Imgproc.medianBlur(grey, dst, 3);\r\n  Imgproc.Canny(dst, dst, 50, 200, 3, false);\r\n  Mat lines = new Mat();\r\n  Imgproc.HoughLinesP(dst, lines, 1, PI\/180.0, 50, 10, 10);\r\n  background(0);\r\n  \/\/  image(img, 0, 0);\r\n\r\n  if (lines.cols()>0) {\r\n    noFill();\r\n    for (int i=0; i<lines.rows(); i++) {\r\n      double [] v = lines.get(i, 0);\r\n      float x1 = (float) v[0];\r\n      float y1 = (float) v[1];\r\n      float x2 = (float) v[2];\r\n      float y2 = (float) v[3];\r\n      int mx = constrain(round((x1 + x2)\/2.0), 0, cap.width-1);\r\n      int my = constrain(round((y1 + y2)\/2.0), 0, cap.height-1);\r\n      color col = cap.pixels[my*cap.width+mx];\r\n      stroke(col);\r\n      line(x1, y1, x2, y2);\r\n    }\r\n  }\r\n\r\n  fill(0);\r\n  noStroke();\r\n  rect(0, 0, 110, 30);\r\n  fill(255);\r\n  text(\"Frame rate: \" + nf(round(frameRate), 2), 10, 20, 0);\r\n  grey.release();\r\n  dst.release();\r\n  lines.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>In addition to the Hough circle detection, this example works on the Hough line segment detection. It inputs the live webcam image; converts it into greyscale; applies a medianBlur filter; processes the Canny edge detection. The Imgproc.HoughLinesP() function will finally single out the line segments into a Mat &#8211; lines in our example codes.<\/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":[137,3,62],"class_list":["post-1590","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-hough-line","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1590","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=1590"}],"version-history":[{"count":1,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1590\/revisions"}],"predecessor-version":[{"id":1594,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1590\/revisions\/1594"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1590"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}