{"id":1463,"date":"2015-05-31T23:13:01","date_gmt":"2015-05-31T15:13:01","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1463"},"modified":"2015-07-05T22:15:33","modified_gmt":"2015-07-05T14:15:33","slug":"opencv-and-processing-9","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/05\/31\/opencv-and-processing-9\/","title":{"rendered":"OpenCV and Processing 9"},"content":{"rendered":"<p>The example recreates the <a href=\"http:\/\/docs.opencv.org\/modules\/highgui\/doc\/reading_and_writing_images_and_video.html#videocapture\">VideoCapture sample<\/a> from the OpenCV documentation. It uses the Imgproc module to convert the color image into single channel greyscale, apply a blur filter, and find the edges.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1465\" title=\"screen-0401\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/05\/screen-0401.png\" alt=\"\" width=\"640\" height=\"480\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/05\/screen-0401.png 640w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2015\/05\/screen-0401-300x225.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><br \/>\n<!--more--><\/p>\n<pre lang=\"java\">import processing.video.*;\r\n\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.imgproc.Imgproc;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.Size;\r\n\r\nimport java.nio.*;\r\nimport java.util.List;\r\n\r\nCapture cap;\r\nPImage img;\r\nint cnt;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  background(0);\r\n  println(Core.VERSION);\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  cnt = cap.width*cap.height*4;\r\n  img = createImage(cap.width, cap.height, ARGB);\r\n}\r\n\r\nvoid draw() {\r\n  Mat tmp = new Mat(cap.height, cap.width, CvType.CV_8UC4);\r\n  Mat mat = new Mat(cap.height, cap.width, CvType.CV_8UC3);\r\n  Mat grey = new Mat(cap.height, cap.width, CvType.CV_8UC1);\r\n  byte [] bArray = new byte[cnt];\r\n  ByteBuffer bb = ByteBuffer.allocate(cnt);\r\n  bb.asIntBuffer().put(cap.pixels);\r\n\r\n  bb.get(bArray);\r\n  tmp.put(0, 0, bArray);\r\n\r\n  ArrayList ch1 = new ArrayList();\r\n  ArrayList ch2 = new ArrayList();\r\n  ArrayList ch3 = new ArrayList();\r\n  ArrayList ch4 = new ArrayList();\r\n\r\n  Core.split(tmp, ch1);\r\n  ch2.add(ch1.get(3));\r\n  ch2.add(ch1.get(2));\r\n  ch2.add(ch1.get(1));\r\n  Core.merge(ch2, mat);\r\n\r\n  Imgproc.cvtColor(mat, grey, Imgproc.COLOR_BGR2GRAY);\r\n  Imgproc.GaussianBlur(grey, grey, new Size(7, 7), 1.5, 1.5);\r\n  Imgproc.Canny(grey, grey, 0, 30);\r\n\r\n  Core.split(grey, ch3);\r\n  ch4.add(ch1.get(0)); \/\/ alpha\r\n  ch4.add(ch3.get(0));\r\n  ch4.add(ch3.get(0));\r\n  ch4.add(ch3.get(0));\r\n  Core.merge(ch4, tmp);\r\n  tmp.get(0, 0, bArray);\r\n  ByteBuffer.wrap(bArray).asIntBuffer().get(img.pixels);\r\n  img.updatePixels();\r\n  image(img, 0, 0);\r\n  text(\"Frame rate: \" + nf(round(frameRate), 2), 10, 20);\r\n\r\n  grey.release();\r\n  tmp.release();\r\n  mat.release();\r\n  ch1.clear();\r\n  ch2.clear();\r\n  ch3.clear();\r\n  ch4.clear();\r\n}\r\n\r\nvoid captureEvent(Capture c) {\r\n  c.read();\r\n}<\/pre>\n<p>&nbsp;<br \/>\nThe <strong>Imgproc<\/strong> module handles basic and advanced image processing in OpenCV. The <strong>cvtColor<\/strong> converts the color matrix, mat to greyscale, grey with the parameter COLOR_BGR2GRAY. The <strong>GaussianBlur<\/strong> performs the Gaussian Blur filter to the greyscale image. The <strong>Canny<\/strong> searches for edges in the blurred greyscale image using the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Canny_edge_detector\">Canny Edge Detector<\/a>.<\/p>\n<pre lang=\"java\">\r\nImgproc.cvtColor(mat, grey, Imgproc.COLOR_BGR2GRAY);\r\nImgproc.GaussianBlur(grey, grey, new Size(7, 7), 1.5, 1.5);\r\nImgproc.Canny(grey, grey, 0, 30);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The example recreates the VideoCapture sample from the OpenCV documentation. It uses the Imgproc module to convert the color image into single channel greyscale, apply a blur filter, and find the edges.<\/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,62],"class_list":["post-1463","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1463","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=1463"}],"version-history":[{"count":6,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1463\/revisions"}],"predecessor-version":[{"id":1559,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1463\/revisions\/1559"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1463"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}