{"id":1595,"date":"2015-07-21T17:20:00","date_gmt":"2015-07-21T09:20:00","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1595"},"modified":"2015-07-21T17:25:04","modified_gmt":"2015-07-21T09:25:04","slug":"opencv-and-processing-19","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/07\/21\/opencv-and-processing-19\/","title":{"rendered":"OpenCV and Processing 19"},"content":{"rendered":"<p>Similar to the last Hough Line detection, the following example illustrates the use of the new <strong>LineSegmentDetector<\/strong> class in the<strong> Imgproc<\/strong> module. Instead of using the new command, we have to use the <strong>Imgproc.createLineSegmentDetector() <\/strong>function to create a new instance of the class.<br \/>\n&nbsp;<br \/>\n<div id='gallery-1' class='gallery galleryid-1595 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\/photo0424.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0424-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\/photo0862.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo0862-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\/photo1111.png'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/07\/photo1111-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/div><\/figure>\n\t\t<\/div>\n<br \/>\n<!--more--><br \/>\nThe <strong>detect()<\/strong> function will return an Mat that contains all information of the line segments. I use a <strong>MatOfFloat4<\/strong> data structure to extract the details. Each part is essentially the 2 endpoints of the line segment.<\/p>\n<p><strong>Source codes of the example<\/strong><\/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.core.MatOfFloat4;\r\nimport org.opencv.imgproc.Imgproc;\r\nimport org.opencv.imgproc.LineSegmentDetector;\r\n\r\nCapture cap;\r\nCVImage img;\r\nLineSegmentDetector line;\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  line = Imgproc.createLineSegmentDetector();\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  \/\/  image(img, 0, 0);\r\n  Mat grey = img.getGrey();\r\n  MatOfFloat4 lines = new MatOfFloat4();\r\n  line.detect(grey, lines);\r\n  \r\n  if (lines.rows()>0 && lines.cols()>0) {\r\n    float [] f = lines.toArray();\r\n    int cnt = f.length\/4;\r\n\r\n    for (int i=0; i<cnt; i++) {\r\n      int x1 = round(f[i*4]);\r\n      int y1 = round(f[i*4+1]);\r\n      int x2 = round(f[i*4+2]);\r\n      int y2 = round(f[i*4+3]);\r\n      int mx = constrain((x1 + x2)\/2, 0, img.width-1);\r\n      int my = constrain((y1 + y2)\/2, 0, img.height-1);\r\n      color col = img.pixels[my*img.width+mx];\r\n      stroke(col);\r\n      strokeWeight(random(5));\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  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>Similar to the last Hough Line detection, the following example illustrates the use of the new LineSegmentDetector class in the Imgproc module. Instead of using the new command, we have to use the Imgproc.createLineSegmentDetector() function to create a new instance of the class. &nbsp;<\/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":[],"class_list":["post-1595","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1595","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=1595"}],"version-history":[{"count":4,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1595\/revisions"}],"predecessor-version":[{"id":1600,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1595\/revisions\/1600"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1595"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}