{"id":1252,"date":"2014-03-13T14:48:23","date_gmt":"2014-03-13T06:48:23","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1252"},"modified":"2014-03-28T12:48:19","modified_gmt":"2014-03-28T04:48:19","slug":"opencv-features2d-in-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2014\/03\/13\/opencv-features2d-in-processing\/","title":{"rendered":"OpenCV Features2D in Processing"},"content":{"rendered":"<p>The following Processing codes demonstrate the use of the OpenCV Features2D to detect key points from the webcam image. It is the first part of a more complex task to identify 3D motion from a 2D image.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/OpenCVFeatures2D.jpg\" alt=\"\" title=\"OpenCVFeatures2D\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-1253\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/OpenCVFeatures2D.jpg 640w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/OpenCVFeatures2D-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\nimport processing.video.*;\r\n\r\nimport java.util.*;\r\nimport java.nio.*;\r\n\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.imgproc.Imgproc;\r\nimport org.opencv.features2d.Features2d;\r\nimport org.opencv.features2d.FeatureDetector;\r\nimport org.opencv.core.MatOfKeyPoint;\r\nimport org.opencv.features2d.KeyPoint;\r\n\r\nfinal float DELTA = 10.0;\r\n\r\nCapture cap;\r\n\r\nbyte [] bArray;\r\nint [] iArray;\r\nint pixCnt1, pixCnt2;\r\nFeatureDetector detector;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n\r\n  background(0);\r\n  \/\/ Define and initialise the default capture device.\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n\r\n  \/\/ Load the OpenCV native library.\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  println(Core.VERSION);\r\n\r\n  \/\/ pixCnt1 is the number of bytes in the pixel buffer.\r\n  \/\/ pixCnt2 is the number of integers in the PImage pixels buffer.\r\n  pixCnt1 = width*height*4;\r\n  pixCnt2 = width*height;\r\n\r\n  \/\/ bArray is the temporary byte array buffer for OpenCV cv::Mat.\r\n  \/\/ iArray is the temporary integer array buffer for PImage pixels.\r\n  bArray = new byte[pixCnt1];\r\n  iArray = new int[pixCnt2];\r\n  detector = FeatureDetector.create(FeatureDetector.ORB);\r\n  noFill();\r\n  stroke(255, 255, 0, 180);\r\n}\r\n\r\nvoid draw() {\r\n  if (cap.available()) {\r\n    cap.read();\r\n  } \r\n  else {\r\n    return;\r\n  }\r\n  image(cap, 0, 0);\r\n\r\n  \/\/ Copy the webcam image to the temporary integer array iArray.\r\n  arrayCopy(cap.pixels, iArray);\r\n\r\n  \/\/ Define the temporary Java byte and integer buffers. \r\n  \/\/ They share the same storage.\r\n  ByteBuffer bBuf = ByteBuffer.allocate(pixCnt1);\r\n  IntBuffer iBuf = bBuf.asIntBuffer();\r\n\r\n  \/\/ Copy the webcam image to the byte buffer iBuf.\r\n  iBuf.put(iArray);\r\n\r\n  \/\/ Copy the webcam image to the byte array bArray.\r\n  bBuf.get(bArray);\r\n\r\n  \/\/ Create the OpenCV cv::Mat.\r\n  Mat m1 = new Mat(height, width, CvType.CV_8UC4);\r\n\r\n  \/\/ Initialise the matrix m1 with content from bArray.\r\n  m1.put(0, 0, bArray);\r\n  \/\/ Prepare the grayscale matrix.\r\n  Mat m3 = new Mat(height, width, CvType.CV_8UC1);\r\n  Imgproc.cvtColor(m1, m3, Imgproc.COLOR_BGRA2GRAY);\r\n\r\n  MatOfKeyPoint keypoints = new MatOfKeyPoint();\r\n  detector.detect(m3, keypoints);\r\n\r\n  KeyPoint [] points = keypoints.toArray();\r\n  ArrayList<KeyPoint> pList = new ArrayList<KeyPoint>();\r\n  pList.add(points[0]);\r\n  \/\/ Remove the keypoints that are closed together.\r\n  for (int i=1; i<points.length; i++) {\r\n    boolean done = true;\r\n    for (int j=0; j<pList.size(); j++) {\r\n      float d = dist((float)points[i].pt.x, (float)points[i].pt.y, \r\n      (float)pList.get(j).pt.x, (float)pList.get(j).pt.y);\r\n      if (d > DELTA) {\r\n        continue;\r\n      } \r\n      else {\r\n        done = false;\r\n      }\r\n    }\r\n    if (done) {\r\n      pList.add(points[i]);\r\n    }\r\n  }\r\n\r\n  for (int i=0; i<pList.size(); i++) {\r\n    ellipse((float)pList.get(i).pt.x, (float)pList.get(i).pt.y, pList.get(i).size, pList.get(i).size);\r\n    if (pList.get(i).angle != -1) {\r\n      float angle = 360.0 - pList.get(i).angle;\r\n      float tx = (float)pList.get(i).pt.x + pList.get(i).size*cos(radians(angle))\/2.0;\r\n      float ty = (float)pList.get(i).pt.y + pList.get(i).size*sin(radians(angle))\/2.0;\r\n      line(tx, ty, (float)pList.get(i).pt.x, (float)pList.get(i).pt.y);\r\n    }\r\n  }\r\n  keypoints.release();\r\n  text(\"Frame Rate: \" + round(frameRate), 500, 50);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following Processing codes demonstrate the use of the OpenCV Features2D to detect key points from the webcam image. It is the first part of a more complex task to identify 3D motion from a 2D image. &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[125,3,62],"class_list":["post-1252","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-feature-detection","tag-opencv","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1252","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=1252"}],"version-history":[{"count":2,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"predecessor-version":[{"id":1268,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1252\/revisions\/1268"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}