{"id":1453,"date":"2015-05-30T21:55:18","date_gmt":"2015-05-30T13:55:18","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1453"},"modified":"2015-07-30T14:32:21","modified_gmt":"2015-07-30T06:32:21","slug":"opencv-and-processing-7","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2015\/05\/30\/opencv-and-processing-7\/","title":{"rendered":"OpenCV and Processing 7"},"content":{"rendered":"<p>This example compares the pixel information between a Processing PImage and OpenCV Mat of the same external image file. The dimension of the image in Processing is <strong>width<\/strong> x <strong>height<\/strong> (x, y) while for OpenCV, it is <strong>rows()<\/strong> x <strong>cols()<\/strong> (y, x). A random point is picked and its pixel color is shown both in Processing PImage and OpenCV Mat.<br \/>\n<!--more--><br \/>\nIn Processing<\/p>\n<pre lang=\"java\">\r\ncolor c1 = img.get(x, y);\r\n<\/pre>\n<p>In OpenCV<\/p>\n<pre lang=\"java\">\r\ndouble [] c2 = mat.get(y, x);\r\n<\/pre>\n<p>We can also observe that the color arrangement in Processing is ARGB and OpenCV is BGR.<\/p>\n<p>The codes are:<\/p>\n<pre lang=\"java\">\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.imgcodecs.Imgcodecs;\r\n\r\nPImage img;\r\nMat mat;\r\nMat alpha;\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  img = loadImage(\"sample01.jpg\");\r\n  mat = Imgcodecs.imread(dataPath(\"sample01.jpg\"));\r\n  println(\"PImage size:   \" + img.width + \"x\" + img.height);\r\n  println(\"Mat size:      \" + mat.cols() + \"x\" + mat.rows());\r\n  println(\"PImage format: \" + img.format);\r\n  println(\"Mat type:      \" + mat.type());\r\n  noLoop();\r\n}\r\n\r\nvoid draw() {\r\n  image(img, 0, 0);\r\n  int x = floor(random(img.width));\r\n  int y = floor(random(img.height));\r\n  color c1 = img.get(x, y);\r\n  double [] c2 = mat.get(y, x);\r\n  println(\"PImage color: \" + red(c1) + \",\" + green(c1) + \",\" + blue(c1));\r\n  println(\"Mat color:    \" + c2[0] + \",\" + c2[1] + \",\" + c2[2]);\r\n}\r\n<\/pre>\n<p>The console output will be:<\/p>\n<pre>\r\n3.0.0-rc1\r\nPImage size:   640x480\r\nMat size:      640x480\r\nPImage format: 1\r\nMat type:      16\r\nPImage color: 177.0,184.0,203.0\r\nMat color:    203.0,184.0,177.0\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example compares the pixel information between a Processing PImage and OpenCV Mat of the same external image file. The dimension of the image in Processing is width x height (x, y) while for OpenCV, it is rows() x cols() (y, x). A random point is picked and its pixel color is shown both in [&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":[3,62],"class_list":["post-1453","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\/1453","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=1453"}],"version-history":[{"count":4,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1453\/revisions"}],"predecessor-version":[{"id":1616,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1453\/revisions\/1616"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1453"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}