(6349,17729,'2014-08-10 21:19:28','2014-08-10 21:19:28','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = (ret_val<thresholdValue)*0.0f + 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than( <pre>(ret_val<thresholdValue)*0.0f</pre> ) threshold to 0.0f and the rest( <pre>255.0f*(ret_val>thresholdValue)</pre> ) of them to 255.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    while(true) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            break;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = outwd * 0.0f + (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:19:28','2014-08-10 21:19:28','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6350,17729,'2014-08-10 21:24:57','2014-08-10 21:24:57','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255.\r\n<pre>(ret_val<thresholdValue)*0.0f + 255.0f*(ret_val>thresholdValue)</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    while(true) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            break;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = outwd * 0.0f + (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:24:57','2014-08-10 21:24:57','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6351,17729,'2014-08-10 21:28:30','2014-08-10 21:28:30','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    while(true) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            break;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = outwd * 0.0f + (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:28:30','2014-08-10 21:28:30','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6352,17729,'2014-08-10 21:30:10','2014-08-10 21:30:10','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    while(true) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            break;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = (diff<constnt)*0.f + 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:30:10','2014-08-10 21:30:10','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6353,17729,'2014-08-10 21:31:18','2014-08-10 21:31:18','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    while(true) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            break;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:31:18','2014-08-10 21:31:18','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6354,17729,'2014-08-10 21:33:30','2014-08-10 21:33:30','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    do {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        T = tempT;\r\n    }while(!(abs(tempT-T)<0.01f));\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:33:30','2014-08-10 21:33:30','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6355,17729,'2014-08-10 21:34:58','2014-08-10 21:34:58','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    do {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        T = tempT;\r\n    } while((abs(tempT-T)<0.01f)==false);\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:34:58','2014-08-10 21:34:58','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6356,17729,'2014-08-10 21:37:01','2014-08-10 21:37:01','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference.\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:37:01','2014-08-10 21:37:01','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6357,17729,'2014-08-10 21:45:52','2014-08-10 21:45:52','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n    float sumB = 0.0f;\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n    float mB;\r\n    float mF;\r\n    float max = 0.0f;\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 21:45:52','2014-08-10 21:45:52','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6358,17729,'2014-08-10 22:08:16','2014-08-10 22:08:16','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding ith intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variable to track cumulative intensity frequency value\r\n    // before current intensity value being considered as threshold\r\n    float wB = 0.0f;\r\n\r\n    // variable to track cumulative intensity frequency value\r\n    // after current intensity value being considered as threshold\r\n    float wF = 0.0f;\r\n\r\n    // mean for intensity values before current intensity \r\n    // value being considered as threshold\r\n    float mB;\r\n\r\n    // mean for intensity values after current intensity \r\n    // value being considered as threshold\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 22:08:16','2014-08-10 22:08:16','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6359,17729,'2014-08-10 22:11:24','2014-08-10 22:11:24','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variable to track cumulative intensity frequency value\r\n    // before current intensity value being considered as threshold\r\n    float wB = 0.0f;\r\n\r\n    // variable to track cumulative intensity frequency value\r\n    // after current intensity value being considered as threshold\r\n    float wF = 0.0f;\r\n\r\n    // mean for intensity values before current intensity \r\n    // value being considered as threshold\r\n    float mB;\r\n\r\n    // mean for intensity values after current intensity \r\n    // value being considered as threshold\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 22:11:24','2014-08-10 22:11:24','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6360,17729,'2014-08-10 22:12:46','2014-08-10 22:12:46','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // mean for intensity values before current intensity \r\n    // value being considered as threshold\r\n    float mB;\r\n\r\n    // mean for intensity values after current intensity \r\n    // value being considered as threshold\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 22:12:46','2014-08-10 22:12:46','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6361,17729,'2014-08-10 22:13:45','2014-08-10 22:13:45','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // mean for intensity values before current intensity \r\n    // value being considered as threshold\r\n    float mB;\r\n\r\n    // mean for intensity values after current intensity \r\n    // value being considered as threshold\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n</div>\r\n<br/>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 22:13:45','2014-08-10 22:13:45','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6362,17729,'2014-08-10 22:14:56','2014-08-10 22:14:56','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a threshold value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-10 22:14:56','2014-08-10 22:14:56','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6363,18132,'2014-08-11 10:20:49','2014-08-11 10:20:49','On Debian and variants,\r\n<pre>sudo apt-get install mingw-w64\r\ni686-w64-mingw32-gcc hello.cc -o hello32.exe      # 32-bit\r\nx86_64-w64-mingw32-gcc hello.cc -o hello64.exe    # 64-bit\r\n</pre>\r\nFrom, <a href=\"http://www.tinc-vpn.org/examples/cross-compiling-64-bit-windows-binary/\">cross compiling tinc for windows 64</a>.\r\n\r\n<img class=\"aligncenter\" src=\"http://i.imgur.com/C4buo.gif\" alt=\"blown\" />\r\n\r\nWhy did I not know about this? It\'s like I just discovered the screw driver.\r\n\r\nThe resulting binary will dynamically link to <code>kernel.dll</code> and whatever <code>msvcrt.dll</code>. If your code depends on any other libraries, you\'ll need to build or obtain those for windows, as well. The link, above, goes through the motions of cross compiling several packages for windows from Linux; it\'s a different story for each project.\r\n\r\n<h4>Using a Dependency in Binary Form</h4>\r\n\r\n\r\n\r\n<h4>Cross-Compiling Boost: Building a Dependency</h4>','Cross Compile to Windows From Linux','','inherit','open','open','','6297-revision-v1','','','2014-08-11 10:20:49','2014-08-11 10:20:49','',6297,'http://sandbox.arrayfire.com/6297-revision-v1/',0,'revision','',0),(6364,18132,'2014-08-11 10:36:51','2014-08-11 10:36:51','On Debian and variants,\r\n<pre>sudo apt-get install mingw-w64\r\ni686-w64-mingw32-gcc hello.cc -o hello32.exe      # 32-bit\r\nx86_64-w64-mingw32-gcc hello.cc -o hello64.exe    # 64-bit\r\n</pre>\r\nFrom, <a href=\"http://www.tinc-vpn.org/examples/cross-compiling-64-bit-windows-binary/\">cross compiling tinc for windows 64</a>.\r\n\r\n<img class=\"aligncenter\" src=\"http://i.imgur.com/C4buo.gif\" alt=\"blown\" />\r\n\r\nWhy did I not know about this? It\'s like I just discovered the screw driver.\r\n\r\nThe resulting binary will dynamically link to <code>kernel.dll</code> and <code>msvcrt.dll</code>. Target machine will therefore require an installation of the Microsoft C Runtime.\r\n\r\nIf your code depends on any other libraries, you\'ll need to either build or obtain them for windows. The link, above, goes through the motions of cross compiling several packages for windows from Linux; it\'s a different story for each project.\r\n\r\n<h4>Cross-Compiling Boost: Building a Dependency</h4>\r\n\r\n','Cross Compile to Windows From Linux','','inherit','open','open','','6297-revision-v1','','','2014-08-11 10:36:51','2014-08-11 10:36:51','',6297,'http://sandbox.arrayfire.com/6297-revision-v1/',0,'revision','',0),(6366,17729,'2014-08-11 13:26:23','2014-08-11 13:26:23','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 13:26:23','2014-08-11 13:26:23','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6367,17729,'2014-08-11 16:27:14','2014-08-11 16:27:14','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 16:27:14','2014-08-11 16:27:14','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6368,17729,'2014-08-11 16:46:05','2014-08-11 16:46:05','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation.\r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 16:46:05','2014-08-11 16:46:05','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6369,17729,'2014-08-11 17:01:15','2014-08-11 17:01:15','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only. \r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following three line code stub.\r\n<pre>\r\narray color = loadimage(\"<imagepath>\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style = \"text-align:center\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" seamless frameborder=0 scrolling=no></iframe>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 17:01:15','2014-08-11 17:01:15','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6370,17729,'2014-08-11 17:01:56','2014-08-11 17:01:56','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only. \r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>\r\narray color = loadimage(\"<imagepath>\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style = \"text-align:center\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" seamless frameborder=0 scrolling=no></iframe>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\narray otsu(const array& in)\r\n{\r\n    array gray = colorspace(in,af_gray,af_rgb);\r\n    unsigned total = gray.elements();\r\n    array hist = histogram(gray,256,0.0f,255.0f);\r\n    float *h_hist = hist.host<float>();\r\n\r\n    // sum indicates the expectation of corresponding intensity value\r\n    float sum = 0.0f;\r\n    for (int i = 1; i < 256; ++i)\r\n        sum += i * h_hist[i];\r\n\r\n    // sumB also indicates the expectation of each intensity value\r\n    // but it will readjusted as we move along the intensity scale\r\n    // from 0 to 255 searching for our threshold\r\n    float sumB = 0.0f;\r\n\r\n    // variables to track cumulative intensity frequency values\r\n    float wB = 0.0f;\r\n    float wF = 0.0f;\r\n\r\n    // variables to track mean for intensity value classes\r\n    float mB;\r\n    float mF;\r\n\r\n    float max = 0.0f;\r\n\r\n    // variable used to track between class variance\r\n    float between = 0.0f;\r\n    float threshold1 = 0.0f;\r\n    float threshold2 = 0.0f;\r\n    for (int i = 0; i < 256; ++i) {\r\n        wB += h_hist[i];\r\n        if (wB == 0)\r\n            continue;\r\n        wF = total - wB;\r\n        if (wF == 0)\r\n            break;\r\n        sumB += i * h_hist[i];\r\n        mB = sumB / wB;\r\n        mF = (sum - sumB) / wF;\r\n        between = wB * wF * pow(mB - mF, 2);\r\n        if ( between >= max ) {\r\n            threshold1 = i;\r\n            if ( between > max ) {\r\n                threshold2 = i;\r\n            }\r\n            max = between;\r\n        }\r\n    }\r\n    array::free(h_hist);\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 17:01:56','2014-08-11 17:01:56','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6371,17729,'2014-08-11 18:57:01','2014-08-11 18:57:01','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only. \r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>\r\narray color = loadimage(\"<imagepath>\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style = \"text-align:center\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" seamless frameborder=0 scrolling=no></iframe>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\n/**\r\n * Note:\r\n * suffix B indicates subset of all graylevels before current gray level\r\n * suffix F indicates subset of all gray levels after current gray level\r\n */\r\narray otsu(const array& in)\r\n{\r\n    array gray;\r\n    int channels = in.dims(2);\r\n    if (channels>1)\r\n        gray  = colorspace(in,af_gray,af_rgb);\r\n    else\r\n        gray  = in;\r\n    unsigned total = gray.elements();\r\n    array hist  = histogram(gray,256,0.0f,255.0f);\r\n    array wts   = array(seq(256));\r\n\r\n    array wtB   = accum(hist);\r\n    array wtF   = total - wtB;\r\n    array sumB  = accum(wts*hist);\r\n    array meanB = sumB/wtB;\r\n    array meanF = (sumB(255)-sumB) / wtF;\r\n    array mDiff = meanB-meanF;\r\n\r\n    array interClsVar= wtB * wtF * mDiff * mDiff;\r\n    float max        = af::max<float>(interClsVar);\r\n    float threshold2 = where(interClsVar==max).scalar<float>();\r\n    array threshIdx  = where(interClsVar>=max);\r\n    float threshold1 = threshIdx.elements()>0 ? threshIdx.scalar<float>() : 0.0f;\r\n\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val);\r\n    bool isContinue = true;\r\n    while(isContinue) {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        float tempT = (r1_avg+r2_avg)/2.0f;\r\n        if (abs(tempT-T)<0.01f) {\r\n            isContinue = false;\r\n        }\r\n        T = tempT;\r\n    }\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 18:57:01','2014-08-11 18:57:01','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6372,17729,'2014-08-11 19:20:43','2014-08-11 19:20:43','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only. \r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>\r\narray color = loadimage(\"<imagepath>\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style = \"text-align:center\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" seamless frameborder=0 scrolling=no></iframe>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\n/**\r\n * Note:\r\n * suffix B indicates subset of all graylevels before current gray level\r\n * suffix F indicates subset of all gray levels after current gray level\r\n */\r\narray otsu(const array& in)\r\n{\r\n    array gray;\r\n    int channels = in.dims(2);\r\n    if (channels>1)\r\n        gray  = colorspace(in,af_gray,af_rgb);\r\n    else\r\n        gray  = in;\r\n    unsigned total = gray.elements();\r\n    array hist  = histogram(gray,256,0.0f,255.0f);\r\n    array wts   = array(seq(256));\r\n\r\n    array wtB   = accum(hist);\r\n    array wtF   = total - wtB;\r\n    array sumB  = accum(wts*hist);\r\n    array meanB = sumB/wtB;\r\n    array meanF = (sumB(255)-sumB) / wtF;\r\n    array mDiff = meanB-meanF;\r\n\r\n    array interClsVar= wtB * wtF * mDiff * mDiff;\r\n    float max        = af::max<float>(interClsVar);\r\n    float threshold2 = where(interClsVar==max).scalar<float>();\r\n    array threshIdx  = where(interClsVar>=max);\r\n    float threshold1 = threshIdx.elements()>0 ? threshIdx.scalar<float>() : 0.0f;\r\n\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val), prevT=0.0f;\r\n    do {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        prevT = T;\r\n        T = (r1_avg+r2_avg)/2.0f;        \r\n    } while (abs(T-prevT)<0.01f);\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 19:20:43','2014-08-11 19:20:43','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6373,17729,'2014-08-11 19:21:34','2014-08-11 19:21:34','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n<li>Image Histogram</li>\r\n<li>Simple Binary Theshold</li>\r\n<li>Otsu Threshold</li>\r\n<li>Iterative Threshold</li>\r\n<li>Adaptive Binary Threshold</li>\r\n<li>Emboss Filter</li>\r\n</ul>\r\n\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n\r\n<h4>Image Histogram</h4>\r\nWe have built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only. \r\n<div style = \"text-align:center\">\r\n <div style=\"vertical-align: top\">\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6311\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"vertical-align: bottom\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" seamless frameborder=0 scrolling=no></iframe>\r\n </div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>\r\narray color = loadimage(\"<imagepath>\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style = \"text-align:center\">\r\n<iframe height=371 width=600 src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" seamless frameborder=0 scrolling=no></iframe>\r\n</div>\r\n\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>\r\narray threshold(const array &in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels>1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val>thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLets go through whats going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val>thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6322\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6313\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>\r\n/**\r\n * Note:\r\n * suffix B indicates subset of all graylevels before current gray level\r\n * suffix F indicates subset of all gray levels after current gray level\r\n */\r\narray otsu(const array& in)\r\n{\r\n    array gray;\r\n    int channels = in.dims(2);\r\n    if (channels>1)\r\n        gray  = colorspace(in,af_gray,af_rgb);\r\n    else\r\n        gray  = in;\r\n    unsigned total = gray.elements();\r\n    array hist  = histogram(gray,256,0.0f,255.0f);\r\n    array wts   = array(seq(256));\r\n\r\n    array wtB   = accum(hist);\r\n    array wtF   = total - wtB;\r\n    array sumB  = accum(wts*hist);\r\n    array meanB = sumB/wtB;\r\n    array meanF = (sumB(255)-sumB) / wtF;\r\n    array mDiff = meanB-meanF;\r\n\r\n    array interClsVar= wtB * wtF * mDiff * mDiff;\r\n    float max        = af::max<float>(interClsVar);\r\n    float threshold2 = where(interClsVar==max).scalar<float>();\r\n    array threshIdx  = where(interClsVar>=max);\r\n    float threshold1 = threshIdx.elements()>0 ? threshIdx.scalar<float>() : 0.0f;\r\n\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6312\" /></a> Input image with Bimodal histogram[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6333\" /></a> Simple binary threshold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6332\" /></a> Otsu threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively threshold-ed. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>\r\narray iterativeThreshold(const array &in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean<float>(ret_val), prevT=0.0f;\r\n    do {\r\n        array region1 = (ret_val > T)*ret_val;\r\n        array region2 = (ret_val <= T)*ret_val;\r\n        float r1_avg  = mean<float>(region1);\r\n        float r2_avg  = mean<float>(region2);\r\n        prevT = T;\r\n        T = (r1_avg+r2_avg)/2.0f;        \r\n    } while (abs(T-prevT)>0.01f);\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6326\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6328\" /></a> Otsu threhsold[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" class=\"size-thumbnail wp-image-6327\" /></a> Iterative threshold[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is threshold-ed using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n<li>Convert image to gray scale if required</li>\r\n<li>Convolve the gray scale image using the local operator</li>\r\n<li>Take a difference between the convolved image and gray scale image</li>\r\n<li>Threshold the difference image using constant</li>\r\n<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>\r\ntypedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff>constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6329\" /></a> Input[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6309\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n </div>\r\n</div>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6310\" /></a> Adaptive Threshold with Minimum & Maximum as local operator[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" class=\"size-medium wp-image-6308\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n </div>\r\n</div>\r\n\r\n\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on it\'s three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>\r\n/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth<1 || depth>100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)>1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot < 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6319\" /></a> Emboss with low elevation angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6316\" /></a> Emboss with high elevation value[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6317\" /></a> Emboss with low azimuth angle[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6314\" /></a> Emboss with high azimuth angle[/caption]\r\n </div>\r\n</div>\r\n\r\n<div style = \"text-align:center\">\r\n <div style=\"display: inline-block; margin-right: 5px\">\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6318\" /></a> Emboss with low depth value[/caption]\r\n </div>\r\n <div style=\"display: inline-block\">\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" class=\"size-medium wp-image-6315\" /></a> Emboss with high depth value[/caption]\r\n </div>\r\n</div>\r\n\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 19:21:34','2014-08-11 19:21:34','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6375,7,'2014-08-11 21:11:08','2014-08-11 21:11:08','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\n\nIn this post, we will be looking at the following operations.\n<ul>\n	<li>Image Histogram</li>\n	<li>Simple Binary Theshold</li>\n	<li>Otsu Threshold</li>\n	<li>Iterative Threshold</li>\n	<li>Adaptive Binary Threshold</li>\n	<li>Emboss Filter</li>\n</ul>\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\n<h4>Image Histogram</h4>\nWe have a built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only.\n<div style=\"text-align: center;\">\n<div style=\"vertical-align: top;\">\n\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img class=\"size-medium wp-image-6311\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" /></a> Input[/caption]\n\n</div>\n<div style=\"vertical-align: bottom;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\n</div>\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\n<pre>array color = loadimage(\"\",true); // last parameter is to indicate that you would like \n                                             // array to have all channels of the image\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\n                               // ArrayFire stores channels as independent slices currently.\nhist(span,0) = histogram(color(span,span,0),256,0,255);\nhist(span,1) = histogram(color(span,span,1),256,0,255);\nhist(span,2) = histogram(color(span,span,2),256,0,255);\n</pre>\n<div style=\"text-align: center;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\n<h4>Simple Binary Threshold</h4>\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\n<pre>array threshold(const array &amp;in, float thresholdValue)\n{\n    int channels = in.dims(2);\n    array ret_val= in.copy();\n    if (channels&gt;1)\n        ret_val = colorspace(in,af_gray,af_rgb);\n    ret_val = 255.0f*(ret_val&gt;thresholdValue);\n    return ret_val;\n}\n</pre>\nLet\'s go through what\'s going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that, we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val&gt;thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img class=\"size-medium wp-image-6322\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img class=\"size-medium wp-image-6313\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" /></a> Simple binary threshold[/caption]\n\n</div>\n</div>\n<h4>Otsu Threshold</h4>\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\n<pre>/**\n * Note:\n * suffix B indicates subset of all graylevels before current gray level\n * suffix F indicates subset of all gray levels after current gray level\n */\narray otsu(const array&amp; in)\n{\n    array gray;\n    int channels = in.dims(2);\n    if (channels&gt;1)\n        gray  = colorspace(in,af_gray,af_rgb);\n    else\n        gray  = in;\n    unsigned total = gray.elements();\n    array hist  = histogram(gray,256,0.0f,255.0f);\n    array wts   = array(seq(256));\n\n    array wtB   = accum(hist);\n    array wtF   = total - wtB;\n    array sumB  = accum(wts*hist);\n    array meanB = sumB/wtB;\n    array meanF = (sumB(255)-sumB) / wtF;\n    array mDiff = meanB-meanF;\n\n    array interClsVar= wtB * wtF * mDiff * mDiff;\n    float max        = af::max(interClsVar);\n    float threshold2 = where(interClsVar==max).scalar();\n    array threshIdx  = where(interClsVar&gt;=max);\n    float threshold1 = threshIdx.elements()&gt;0 ? threshIdx.scalar() : 0.0f;\n\n    return threshold(gray,(threshold1+threshold2)/2.0f);\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img class=\"size-thumbnail wp-image-6312\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" /></a> Input image with Bimodal histogram[/caption]\n\n</div>\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img class=\"size-thumbnail wp-image-6333\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" /></a> Simple binary threshold[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img class=\"size-thumbnail wp-image-6332\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" /></a> Otsu threshold[/caption]\n\n</div>\n</div>\n<h4>Iterative Threshold</h4>\nThis is a global threshold method where the input image is repetitively thresholded. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\n<pre>array iterativeThreshold(const array &amp;in)\n{\n    array ret_val = colorspace(in,af_gray,af_rgb);\n    float T = mean(ret_val), prevT=0.0f;\n    do {\n        array region1 = (ret_val &gt; T)*ret_val;\n        array region2 = (ret_val &lt;= T)*ret_val;\n        float r1_avg  = mean(region1);\n        float r2_avg  = mean(region2);\n        prevT = T;\n        T = (r1_avg+r2_avg)/2.0f;        \n    } while (abs(T-prevT)&gt;0.01f);\n    return threshold(ret_val,T);\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img class=\"size-thumbnail wp-image-6326\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img class=\"size-thumbnail wp-image-6328\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" /></a> Otsu threhsold[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img class=\"size-thumbnail wp-image-6327\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" /></a> Iterative threshold[/caption]\n\n</div>\n</div>\n<h4>Adaptive Binary Threshold</h4>\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is thresholded using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\n<ol>\n	<li>Convert image to gray scale if required</li>\n	<li>Convolve the gray scale image using the local operator</li>\n	<li>Take a difference between the convolved image and gray scale image</li>\n	<li>Threshold the difference image using constant</li>\n	<li>Invert the thresholded image and return the result</li>\n</ol>\n<pre>typedef enum {\n    MEAN=0,\n    MEDIAN,\n    MINMAX_AVG\n} LocalThresholdType;\n\narray adaptiveThreshold(const array &amp;in, LocalThresholdType kind, int window_size, int constnt)\n{\n    int wr = window_size;\n    array ret_val = colorspace(in,af_gray,af_rgb);\n    if (kind==MEAN) {\n        array wind = constant(1,wr,wr)/(wr*wr);\n        array mean = convolve(ret_val,wind);\n        array diff = mean - ret_val;\n        ret_val    = 255.f*(diff&gt;constnt);\n    } else if (kind==MEDIAN) {\n        array medf = medfilt(ret_val,wr,wr);\n        array diff = medf - ret_val;\n        ret_val    = 255.f*(diff&gt;constnt);\n    } else if (kind==MINMAX_AVG) {\n        array minf = minfilt(ret_val,wr,wr);\n        array maxf = maxfilt(ret_val,wr,wr);\n        array mean = (minf+maxf)/2.0f;\n        array diff = mean - ret_val;\n        ret_val    = 255.f*(diff&gt;constnt);\n    }\n    ret_val = 255.f - ret_val;\n    return ret_val;\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img class=\"size-medium wp-image-6329\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img class=\"size-medium wp-image-6309\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Median as local operator[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img class=\"size-medium wp-image-6310\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Minimum &amp; Maximum as local operator[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img class=\"size-medium wp-image-6308\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Mean as local operator[/caption]\n\n</div>\n</div>\n<h4>Emboss Filter</h4>\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on its three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\n<pre>/**\n * azimuth range is [0-360]\n * elevation range is [0-180]\n * depth range is [1-100]\n * Note: this function has been tailored after\n * the emboss implementation in GIMP editor\n **/\narray emboss(const array &amp;input, float azimuth, float elevation, float depth)\n{\n    if (depth100) {\n        printf(\"Depth should be in the range of 1-100\");\n        return input;\n    }\n    static float x[3] = {-1,0,1};\n    static array hg(3,x);\n    static array vg = hg.T();\n\n    array in = input;\n    if (in.dims(2)&gt;1)\n        in = colorspace(input,af_gray,af_rgb);\n    else\n        in = input;\n\n    // convert angles to radians\n    float phi   = elevation*af::Pi/180.0f;\n    float theta = azimuth*af::Pi/180.0f;\n\n    // compute light pos in cartesian coordinates\n    // and scale with maximum intensity\n    // phi will effect the amount of we intend to put\n    // on a pixel\n    float pos[3];\n    pos[0] = 255.99f * cos(phi)*cos(theta);\n    pos[1] = 255.99f * cos(phi)*sin(theta);\n    pos[2] = 255.99f * sin(phi);\n\n    // compute gradient vector\n    array gx = filter(in,vg);\n    array gy = filter(in,hg);\n\n    // create a array from depth value which \n    // is of same dimensions as gradient vector array\n    float pxlz  = (6*255.0f)/depth;\n    array zdepth= constant(pxlz,gx.dims());\n    // dot product of light position vector and gradient vector\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\n    // divide dot product by gradient vector magnitude to\n    // calculate scalar projection value\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\n    // compute shading condition\n    array outwd = vdot &lt; 0.0f;\n    // choose shading color based on the above condition\n    array color = (1-outwd) * norm;\n    return color;\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img class=\"size-medium wp-image-6319\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" /></a> Emboss with low elevation angle[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img class=\"size-medium wp-image-6316\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" /></a> Emboss with high elevation value[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img class=\"size-medium wp-image-6317\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with low azimuth angle[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img class=\"size-medium wp-image-6314\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with high azimuth angle[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img class=\"size-medium wp-image-6318\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" /></a> Emboss with low depth value[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img class=\"size-medium wp-image-6315\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" /></a> Emboss with high depth value[/caption]\n\n</div>\n</div>\n<h4>Conclusion</h4>\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-autosave-v1','','','2014-08-11 21:11:08','2014-08-11 21:11:08','',6334,'http://sandbox.arrayfire.com/6334-autosave-v1/',0,'revision','',0),(6376,7,'2014-08-11 21:11:46','2014-08-11 21:11:46','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n	<li>Image Histogram</li>\r\n	<li>Simple Binary Theshold</li>\r\n	<li>Otsu Threshold</li>\r\n	<li>Iterative Threshold</li>\r\n	<li>Adaptive Binary Threshold</li>\r\n	<li>Emboss Filter</li>\r\n</ul>\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n<h4>Image Histogram</h4>\r\nWe have a built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only.\r\n<div style=\"text-align: center;\">\r\n<div style=\"vertical-align: top;\">\r\n\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img class=\"size-medium wp-image-6311\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"vertical-align: bottom;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>array color = loadimage(\"\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style=\"text-align: center;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>array threshold(const array &amp;in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels&gt;1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val&gt;thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLet\'s go through what\'s going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that, we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val&gt;thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img class=\"size-medium wp-image-6322\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img class=\"size-medium wp-image-6313\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" /></a> Simple binary threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>/**\r\n * Note:\r\n * suffix B indicates subset of all graylevels before current gray level\r\n * suffix F indicates subset of all gray levels after current gray level\r\n */\r\narray otsu(const array&amp; in)\r\n{\r\n    array gray;\r\n    int channels = in.dims(2);\r\n    if (channels&gt;1)\r\n        gray  = colorspace(in,af_gray,af_rgb);\r\n    else\r\n        gray  = in;\r\n    unsigned total = gray.elements();\r\n    array hist  = histogram(gray,256,0.0f,255.0f);\r\n    array wts   = array(seq(256));\r\n\r\n    array wtB   = accum(hist);\r\n    array wtF   = total - wtB;\r\n    array sumB  = accum(wts*hist);\r\n    array meanB = sumB/wtB;\r\n    array meanF = (sumB(255)-sumB) / wtF;\r\n    array mDiff = meanB-meanF;\r\n\r\n    array interClsVar= wtB * wtF * mDiff * mDiff;\r\n    float max        = af::max(interClsVar);\r\n    float threshold2 = where(interClsVar==max).scalar();\r\n    array threshIdx  = where(interClsVar&gt;=max);\r\n    float threshold1 = threshIdx.elements()&gt;0 ? threshIdx.scalar() : 0.0f;\r\n\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img class=\"size-thumbnail wp-image-6312\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" /></a> Input image with Bimodal histogram[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img class=\"size-thumbnail wp-image-6333\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" /></a> Simple binary threshold[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img class=\"size-thumbnail wp-image-6332\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" /></a> Otsu threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively thresholded. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>array iterativeThreshold(const array &amp;in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean(ret_val), prevT=0.0f;\r\n    do {\r\n        array region1 = (ret_val &gt; T)*ret_val;\r\n        array region2 = (ret_val &lt;= T)*ret_val;\r\n        float r1_avg  = mean(region1);\r\n        float r2_avg  = mean(region2);\r\n        prevT = T;\r\n        T = (r1_avg+r2_avg)/2.0f;        \r\n    } while (abs(T-prevT)&gt;0.01f);\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img class=\"size-thumbnail wp-image-6326\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img class=\"size-thumbnail wp-image-6328\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" /></a> Otsu threhsold[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img class=\"size-thumbnail wp-image-6327\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" /></a> Iterative threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is thresholded using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially the following main steps\r\n<ol>\r\n	<li>Convert image to gray scale if required</li>\r\n	<li>Convolve the gray scale image using the local operator</li>\r\n	<li>Take a difference between the convolved image and gray scale image</li>\r\n	<li>Threshold the difference image using constant</li>\r\n	<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>typedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &amp;in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img class=\"size-medium wp-image-6329\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img class=\"size-medium wp-image-6309\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img class=\"size-medium wp-image-6310\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Minimum &amp; Maximum as local operator[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img class=\"size-medium wp-image-6308\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on its three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &amp;input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)&gt;1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot &lt; 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img class=\"size-medium wp-image-6319\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" /></a> Emboss with low elevation angle[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img class=\"size-medium wp-image-6316\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" /></a> Emboss with high elevation value[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img class=\"size-medium wp-image-6317\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with low azimuth angle[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img class=\"size-medium wp-image-6314\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with high azimuth angle[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img class=\"size-medium wp-image-6318\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" /></a> Emboss with low depth value[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img class=\"size-medium wp-image-6315\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" /></a> Emboss with high depth value[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 21:11:46','2014-08-11 21:11:46','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6377,17729,'2014-08-11 21:16:43','2014-08-11 21:16:43','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\r\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\r\n\r\nIn this post, we will be looking at the following operations.\r\n<ul>\r\n	<li>Image Histogram</li>\r\n	<li>Simple Binary Theshold</li>\r\n	<li>Otsu Threshold</li>\r\n	<li>Iterative Threshold</li>\r\n	<li>Adaptive Binary Threshold</li>\r\n	<li>Emboss Filter</li>\r\n</ul>\r\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\r\n<h4>Image Histogram</h4>\r\nWe have a built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only.\r\n<div style=\"text-align: center;\">\r\n<div style=\"vertical-align: top;\">\r\n\r\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img class=\"size-medium wp-image-6311\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"vertical-align: bottom;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\r\n</div>\r\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\r\n<pre>array color = loadimage(\"\",true); // last parameter is to indicate that you would like \r\n                                             // array to have all channels of the image\r\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\r\n                               // ArrayFire stores channels as independent slices currently.\r\nhist(span,0) = histogram(color(span,span,0),256,0,255);\r\nhist(span,1) = histogram(color(span,span,1),256,0,255);\r\nhist(span,2) = histogram(color(span,span,2),256,0,255);\r\n</pre>\r\n<div style=\"text-align: center;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\r\n<h4>Simple Binary Threshold</h4>\r\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\r\n<pre>array threshold(const array &amp;in, float thresholdValue)\r\n{\r\n    int channels = in.dims(2);\r\n    array ret_val= in.copy();\r\n    if (channels&gt;1)\r\n        ret_val = colorspace(in,af_gray,af_rgb);\r\n    ret_val = 255.0f*(ret_val&gt;thresholdValue);\r\n    return ret_val;\r\n}\r\n</pre>\r\nLet\'s go through what\'s going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that, we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val&gt;thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img class=\"size-medium wp-image-6322\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img class=\"size-medium wp-image-6313\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" /></a> Simple binary threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Otsu Threshold</h4>\r\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\r\n<pre>/**\r\n * Note:\r\n * suffix B indicates subset of all graylevels before current gray level\r\n * suffix F indicates subset of all gray levels after current gray level\r\n */\r\narray otsu(const array&amp; in)\r\n{\r\n    array gray;\r\n    int channels = in.dims(2);\r\n    if (channels&gt;1)\r\n        gray  = colorspace(in,af_gray,af_rgb);\r\n    else\r\n        gray  = in;\r\n    unsigned total = gray.elements();\r\n    array hist  = histogram(gray,256,0.0f,255.0f);\r\n    array wts   = array(seq(256));\r\n\r\n    array wtB   = accum(hist);\r\n    array wtF   = total - wtB;\r\n    array sumB  = accum(wts*hist);\r\n    array meanB = sumB/wtB;\r\n    array meanF = (sumB(255)-sumB) / wtF;\r\n    array mDiff = meanB-meanF;\r\n\r\n    array interClsVar= wtB * wtF * mDiff * mDiff;\r\n    float max        = af::max(interClsVar);\r\n    float threshold2 = where(interClsVar==max).scalar();\r\n    array threshIdx  = where(interClsVar&gt;=max);\r\n    float threshold1 = threshIdx.elements()&gt;0 ? threshIdx.scalar() : 0.0f;\r\n\r\n    return threshold(gray,(threshold1+threshold2)/2.0f);\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img class=\"size-thumbnail wp-image-6312\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" /></a> Input image with Bimodal histogram[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img class=\"size-thumbnail wp-image-6333\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" /></a> Simple binary threshold[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img class=\"size-thumbnail wp-image-6332\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" /></a> Otsu threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Iterative Threshold</h4>\r\nThis is a global threshold method where the input image is repetitively thresholded. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\r\n<pre>array iterativeThreshold(const array &amp;in)\r\n{\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    float T = mean(ret_val), prevT=0.0f;\r\n    do {\r\n        array region1 = (ret_val &gt; T)*ret_val;\r\n        array region2 = (ret_val &lt;= T)*ret_val;\r\n        float r1_avg  = mean(region1);\r\n        float r2_avg  = mean(region2);\r\n        prevT = T;\r\n        T = (r1_avg+r2_avg)/2.0f;        \r\n    } while (abs(T-prevT)&gt;0.01f);\r\n    return threshold(ret_val,T);\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img class=\"size-thumbnail wp-image-6326\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img class=\"size-thumbnail wp-image-6328\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" /></a> Otsu threhsold[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img class=\"size-thumbnail wp-image-6327\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" /></a> Iterative threshold[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Adaptive Binary Threshold</h4>\r\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is thresholded using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially follows theses main steps\r\n<ol>\r\n	<li>Convert image to gray scale if required</li>\r\n	<li>Convolve the gray scale image using the local operator</li>\r\n	<li>Take a difference between the convolved image and gray scale image</li>\r\n	<li>Threshold the difference image using constant</li>\r\n	<li>Invert the thresholded image and return the result</li>\r\n</ol>\r\n<pre>typedef enum {\r\n    MEAN=0,\r\n    MEDIAN,\r\n    MINMAX_AVG\r\n} LocalThresholdType;\r\n\r\narray adaptiveThreshold(const array &amp;in, LocalThresholdType kind, int window_size, int constnt)\r\n{\r\n    int wr = window_size;\r\n    array ret_val = colorspace(in,af_gray,af_rgb);\r\n    if (kind==MEAN) {\r\n        array wind = constant(1,wr,wr)/(wr*wr);\r\n        array mean = convolve(ret_val,wind);\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    } else if (kind==MEDIAN) {\r\n        array medf = medfilt(ret_val,wr,wr);\r\n        array diff = medf - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    } else if (kind==MINMAX_AVG) {\r\n        array minf = minfilt(ret_val,wr,wr);\r\n        array maxf = maxfilt(ret_val,wr,wr);\r\n        array mean = (minf+maxf)/2.0f;\r\n        array diff = mean - ret_val;\r\n        ret_val    = 255.f*(diff&gt;constnt);\r\n    }\r\n    ret_val = 255.f - ret_val;\r\n    return ret_val;\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img class=\"size-medium wp-image-6329\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img class=\"size-medium wp-image-6309\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Median as local operator[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img class=\"size-medium wp-image-6310\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Minimum &amp; Maximum as local operator[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img class=\"size-medium wp-image-6308\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Mean as local operator[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Emboss Filter</h4>\r\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on its three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\r\n<pre>/**\r\n * azimuth range is [0-360]\r\n * elevation range is [0-180]\r\n * depth range is [1-100]\r\n * Note: this function has been tailored after\r\n * the emboss implementation in GIMP editor\r\n **/\r\narray emboss(const array &amp;input, float azimuth, float elevation, float depth)\r\n{\r\n    if (depth100) {\r\n        printf(\"Depth should be in the range of 1-100\");\r\n        return input;\r\n    }\r\n    static float x[3] = {-1,0,1};\r\n    static array hg(3,x);\r\n    static array vg = hg.T();\r\n\r\n    array in = input;\r\n    if (in.dims(2)&gt;1)\r\n        in = colorspace(input,af_gray,af_rgb);\r\n    else\r\n        in = input;\r\n\r\n    // convert angles to radians\r\n    float phi   = elevation*af::Pi/180.0f;\r\n    float theta = azimuth*af::Pi/180.0f;\r\n\r\n    // compute light pos in cartesian coordinates\r\n    // and scale with maximum intensity\r\n    // phi will effect the amount of we intend to put\r\n    // on a pixel\r\n    float pos[3];\r\n    pos[0] = 255.99f * cos(phi)*cos(theta);\r\n    pos[1] = 255.99f * cos(phi)*sin(theta);\r\n    pos[2] = 255.99f * sin(phi);\r\n\r\n    // compute gradient vector\r\n    array gx = filter(in,vg);\r\n    array gy = filter(in,hg);\r\n\r\n    // create a array from depth value which \r\n    // is of same dimensions as gradient vector array\r\n    float pxlz  = (6*255.0f)/depth;\r\n    array zdepth= constant(pxlz,gx.dims());\r\n    // dot product of light position vector and gradient vector\r\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\r\n    // divide dot product by gradient vector magnitude to\r\n    // calculate scalar projection value\r\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\r\n    // compute shading condition\r\n    array outwd = vdot &lt; 0.0f;\r\n    // choose shading color based on the above condition\r\n    array color = (1-outwd) * norm;\r\n    return color;\r\n}\r\n</pre>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img class=\"size-medium wp-image-6319\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" /></a> Emboss with low elevation angle[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img class=\"size-medium wp-image-6316\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" /></a> Emboss with high elevation value[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img class=\"size-medium wp-image-6317\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with low azimuth angle[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img class=\"size-medium wp-image-6314\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with high azimuth angle[/caption]\r\n\r\n</div>\r\n</div>\r\n<div style=\"text-align: center;\">\r\n<div style=\"display: inline-block; margin-right: 5px;\">\r\n\r\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img class=\"size-medium wp-image-6318\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" /></a> Emboss with low depth value[/caption]\r\n\r\n</div>\r\n<div style=\"display: inline-block;\">\r\n\r\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img class=\"size-medium wp-image-6315\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" /></a> Emboss with high depth value[/caption]\r\n\r\n</div>\r\n</div>\r\n<h4>Conclusion</h4>\r\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-revision-v1','','','2014-08-11 21:16:43','2014-08-11 21:16:43','',6334,'http://sandbox.arrayfire.com/6334-revision-v1/',0,'revision','',0),(6378,17729,'2014-08-13 18:59:28','2014-08-13 18:59:28','Today, we will be doing the third post in our series <em>Image editing using ArrayFire</em>. References to old posts are available below.\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire/\">Part 1</a>\n* <a href=\"http://sandbox.arrayfire.com/image-editing-using-arrayfire-part-2/\">Part 2</a>\n\nIn this post, we will be looking at the following operations.\n<ul>\n	<li>Image Histogram</li>\n	<li>Simple Binary Theshold</li>\n	<li>Otsu Threshold</li>\n	<li>Iterative Threshold</li>\n	<li>Adaptive Binary Threshold</li>\n	<li>Emboss Filter</li>\n</ul>\nToday\'s post will be mostly dominated by different types of threshold operations we can achieve using ArrayFire.\n<h4>Image Histogram</h4>\nWe have a built-in function in ArrayFire that creates a <a href=\"http://www.sandbox.arrayfire.com/docs/group__image__func__cpp__histogram.htm\">histogram</a>. The input image was converted to gray scale before histogram calculation as our histogram implementation works for vector and 2D matrices only.\n<div style=\"text-align: center;\">\n<div style=\"vertical-align: top;\">\n\n[caption id=\"attachment_6311\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow.jpg\"><img class=\"size-medium wp-image-6311\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/arrow-300x168.jpg\" alt=\"Input\" width=\"300\" height=\"168\" /></a> Input[/caption]\n\n</div>\n<div style=\"vertical-align: bottom;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=928171252\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\n</div>\nIn case, you need histogram for all three channels of a color image, you can do it using the following four line code stub.\n<pre>array color = loadimage(\"\",true); // last parameter is to indicate that you would like \n                                             // array to have all channels of the image\narray hist(256,color.dims(2)); // 2nd dimension is our channel dimension\n                               // ArrayFire stores channels as independent slices currently.\nhist(span,0) = histogram(color(span,span,0),256,0,255);\nhist(span,1) = histogram(color(span,span,1),256,0,255);\nhist(span,2) = histogram(color(span,span,2),256,0,255);\n</pre>\n<div style=\"text-align: center;\"><iframe src=\"//docs.google.com/a/sandbox.arrayfire.com/spreadsheets/d/1mWvh5XftcIF4hkcf2gwe_7K0kKWngiayhqA7NOIKAPA/gviz/chartiframe?oid=1893315835\" width=\"600\" height=\"371\" frameborder=\"0\" scrolling=\"no\" seamless=\"\"></iframe></div>\n<h4>Simple Binary Threshold</h4>\nA simple threshold operation can be achieved very easily in ArrayFire using the awesome element wise operations. For this particular use case, we will use the greater than and less than operators.\n<pre>array threshold(const array &in, float thresholdValue)\n{\n    int channels = in.dims(2);\n    array ret_val= in.copy();\n    if (channels>1)\n        ret_val = colorspace(in,af_gray,af_rgb);\n    ret_val = 255.0f*(ret_val>thresholdValue);\n    return ret_val;\n}\n</pre>\nLet\'s go through what\'s going on in this function line by line. We initially check if the image has more than one channel and convert it to gray scale image if required. Following that, we set the pixels that are less than threshold to 0 and the rest of them to 255 <code>255.0f*(ret_val&gt;thresholdValue)</code>. If you are interested in knowing about what other element wise operations you can do on the objects of type array, you can find the details <a href=\"http://www.sandbox.arrayfire.com/docs/group__array__operator.htm\">here</a>.\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6322\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house.jpg\"><img class=\"size-medium wp-image-6322\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/house-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6313\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary.jpg\"><img class=\"size-medium wp-image-6313\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/binary-300x225.jpg\" alt=\"Simple binary threshold\" width=\"300\" height=\"225\" /></a> Simple binary threshold[/caption]\n\n</div>\n</div>\n<h4>Otsu Threshold</h4>\nThe famous Otsu threshold method is very reliable in the case of images with bi-modal histograms. Otsu method calculates a global threshold value using the histogram of the image rather than working on the image itself. Since the histogram is only a array of 256 elements, we chose to do the threshold computation on CPU. ArrayFire is used to calculate the histogram and finally threshold the image using the threshold value computed by Otsu method. The image threshold operation reuses the simple threshold function discussed in the previous section. Below given is a comparison of simple binary threshold and Otsu threshold for your reference.\n<pre>/**\n * Note:\n * suffix B indicates subset of all graylevels before current gray level\n * suffix F indicates subset of all gray levels after current gray level\n */\narray otsu(const array& in)\n{\n    array gray;\n    int channels = in.dims(2);\n    if (channels>1)\n        gray  = colorspace(in,af_gray,af_rgb);\n    else\n        gray  = in;\n    unsigned total = gray.elements();\n    array hist  = histogram(gray,256,0.0f,255.0f);\n    array wts   = array(seq(256));\n\n    array wtB   = accum(hist);\n    array wtF   = total - wtB;\n    array sumB  = accum(wts*hist);\n    array meanB = sumB/wtB;\n    array meanF = (sumB(255)-sumB) / wtF;\n    array mDiff = meanB-meanF;\n\n    array interClsVar= wtB * wtF * mDiff * mDiff;\n    float max        = af::max(interClsVar);\n    float threshold2 = where(interClsVar==max).scalar();\n    array threshIdx  = where(interClsVar>=max);\n    float threshold1 = threshIdx.elements()>0 ? threshIdx.scalar<float>() : 0.0f;\n\n    return threshold(gray,(threshold1+threshold2)/2.0f);\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6312\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal.jpg\"><img class=\"size-thumbnail wp-image-6312\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodal-150x150.jpg\" alt=\"Input image with Bimodal histogram\" width=\"150\" height=\"150\" /></a> Input image with Bimodal histogram[/caption]\n\n</div>\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6333\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary.jpg\"><img class=\"size-thumbnail wp-image-6333\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/simpleBinary-150x150.jpg\" alt=\"Simple binary threshold\" width=\"150\" height=\"150\" /></a> Simple binary threshold[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6332\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary.jpg\"><img class=\"size-thumbnail wp-image-6332\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/bimodalBinary-150x150.jpg\" alt=\"Otsu threshold\" width=\"150\" height=\"150\" /></a> Otsu threshold[/caption]\n\n</div>\n</div>\n<h4>Iterative Threshold</h4>\nThis is a global threshold method where the input image is repetitively thresholded. In each iteration, threshold value is calculated from the previous step\'s result. In the first iteration, the threshold value is selected randomly. When the difference between two successive steps threshold is less than certain value, the algorithm exits the iterative loop and the last iteration\'s threshold value is used to threshold the original input image.\n<pre>array iterativeThreshold(const array &amp;in)\n{\n    array ret_val = colorspace(in,af_gray,af_rgb);\n    float T = mean(ret_val), prevT=0.0f;\n    do {\n        array region1 = (ret_val > T)*ret_val;\n        array region2 = (ret_val <= T)*ret_val;\n        float r1_avg  = mean(region1);\n        float r2_avg  = mean(region2);\n        prevT = T;\n        T = (r1_avg+r2_avg)/2.0f;        \n    } while (abs(T-prevT)>0.01f);\n    return threshold(ret_val,T);\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6326\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider.jpg\"><img class=\"size-thumbnail wp-image-6326\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spider-150x150.jpg\" alt=\"Input\" width=\"150\" height=\"150\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6328\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu.jpg\"><img class=\"size-thumbnail wp-image-6328\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_otsu-150x150.jpg\" alt=\"Otsu threhsold\" width=\"150\" height=\"150\" /></a> Otsu threhsold[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6327\" align=\"alignnone\" width=\"150\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter.jpg\"><img class=\"size-thumbnail wp-image-6327\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/spi_iter-150x150.jpg\" alt=\"Iterative threshold\" width=\"150\" height=\"150\" /></a> Iterative threshold[/caption]\n\n</div>\n</div>\n<h4>Adaptive Binary Threshold</h4>\nThreshold methods are usually categorized into two types: global and variable. Global threshold methods have a single threshold value that helps classify the image pixels into two classes. Variable threshold is where local neighborhood properties of the current pixel are used to threshold that very pixel. Adaptive binary threshold is where every pixel is thresholded using a value that is computed using the pixel intensities from the spatial neighborhood of the current pixel. In the below implementation, we used the local operators such as Mean, Median and Minimum &amp; Maximum to calculate the pixel threshold from local neighborhood. Threshold output for each local operator mentioned above are provided below for your reference. The algorithm essentially follows theses main steps\n<ol>\n	<li>Convert image to gray scale if required</li>\n	<li>Convolve the gray scale image using the local operator</li>\n	<li>Take a difference between the convolved image and gray scale image</li>\n	<li>Threshold the difference image using constant</li>\n	<li>Invert the thresholded image and return the result</li>\n</ol>\n<pre>typedef enum {\n    MEAN=0,\n    MEDIAN,\n    MINMAX_AVG\n} LocalThresholdType;\n\narray adaptiveThreshold(const array &amp;in, LocalThresholdType kind, int window_size, int constnt)\n{\n    int wr = window_size;\n    array ret_val = colorspace(in,af_gray,af_rgb);\n    if (kind==MEAN) {\n        array wind = constant(1,wr,wr)/(wr*wr);\n        array mean = convolve(ret_val,wind);\n        array diff = mean - ret_val;\n        ret_val    = 255.f*(diff>constnt);\n    } else if (kind==MEDIAN) {\n        array medf = medfilt(ret_val,wr,wr);\n        array diff = medf - ret_val;\n        ret_val    = 255.f*(diff>constnt);\n    } else if (kind==MINMAX_AVG) {\n        array minf = minfilt(ret_val,wr,wr);\n        array maxf = maxfilt(ret_val,wr,wr);\n        array mean = (minf+maxf)/2.0f;\n        array diff = mean - ret_val;\n        ret_val    = 255.f*(diff>constnt);\n    }\n    ret_val = 255.f - ret_val;\n    return ret_val;\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6329\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku.jpg\"><img class=\"size-medium wp-image-6329\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/sudoku-300x225.jpg\" alt=\"Input\" width=\"300\" height=\"225\" /></a> Input[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6309\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed.jpg\"><img class=\"size-medium wp-image-6309\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmed-300x225.jpg\" alt=\"Adaptive Threshold with Median as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Median as local operator[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6310\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma.jpg\"><img class=\"size-medium wp-image-6310\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmma-300x225.jpg\" alt=\"Adaptive Threshold with Minimum &amp; Maximum as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Minimum &amp; Maximum as local operator[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6308\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean.jpg\"><img class=\"size-medium wp-image-6308\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/adapmean-300x225.jpg\" alt=\"Adaptive Threshold with Mean as local operator\" width=\"300\" height=\"225\" /></a> Adaptive Threshold with Mean as local operator[/caption]\n\n</div>\n</div>\n<h4>Emboss Filter</h4>\nThe emboss function is tailored after the emboss filter provided in GIMP, an open source image editing software. The final outcome of emboss function is based on its three input parameters: azimuth, elevation &amp; depth. The code and the sample outputs of emboss function are provided below.\n<pre>/**\n * azimuth range is [0-360]\n * elevation range is [0-180]\n * depth range is [1-100]\n * Note: this function has been tailored after\n * the emboss implementation in GIMP editor\n **/\narray emboss(const array &amp;input, float azimuth, float elevation, float depth)\n{\n    if (depth>100 || depth<1) {\n        printf(\"Depth should be in the range of 1-100\");\n        return input;\n    }\n    static float x[3] = {-1,0,1};\n    static array hg(3,x);\n    static array vg = hg.T();\n\n    array in = input;\n    if (in.dims(2)>1)\n        in = colorspace(input,af_gray,af_rgb);\n    else\n        in = input;\n\n    // convert angles to radians\n    float phi   = elevation*af::Pi/180.0f;\n    float theta = azimuth*af::Pi/180.0f;\n\n    // compute light pos in cartesian coordinates\n    // and scale with maximum intensity\n    // phi will effect the amount of we intend to put\n    // on a pixel\n    float pos[3];\n    pos[0] = 255.99f * cos(phi)*cos(theta);\n    pos[1] = 255.99f * cos(phi)*sin(theta);\n    pos[2] = 255.99f * sin(phi);\n\n    // compute gradient vector\n    array gx = filter(in,vg);\n    array gy = filter(in,hg);\n\n    // create a array from depth value which \n    // is of same dimensions as gradient vector array\n    float pxlz  = (6*255.0f)/depth;\n    array zdepth= constant(pxlz,gx.dims());\n    // dot product of light position vector and gradient vector\n    array vdot  = gx*pos[0] + gy*pos[1] + pxlz*pos[2];\n    // divide dot product by gradient vector magnitude to\n    // calculate scalar projection value\n    array norm  = vdot/sqrt(gx*gx+gy*gy+zdepth*zdepth);\n    // compute shading condition\n    array outwd = vdot < 0.0f;\n    // choose shading color based on the above condition\n    array color = (1-outwd) * norm;\n    return color;\n}\n</pre>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6319\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel.jpg\"><img class=\"size-medium wp-image-6319\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_lel-300x168.jpg\" alt=\"Emboss with low elevation angle\" width=\"300\" height=\"168\" /></a> Emboss with low elevation angle[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6316\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel.jpg\"><img class=\"size-medium wp-image-6316\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hel-300x168.jpg\" alt=\"Emboss with high elevation value\" width=\"300\" height=\"168\" /></a> Emboss with high elevation value[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6317\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz.jpg\"><img class=\"size-medium wp-image-6317\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_laz-300x168.jpg\" alt=\"Emboss with low azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with low azimuth angle[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6314\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz.jpg\"><img class=\"size-medium wp-image-6314\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_haz-300x168.jpg\" alt=\"Emboss with high azimuth angle\" width=\"300\" height=\"168\" /></a> Emboss with high azimuth angle[/caption]\n\n</div>\n</div>\n<div style=\"text-align: center;\">\n<div style=\"display: inline-block; margin-right: 5px;\">\n\n[caption id=\"attachment_6318\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth.jpg\"><img class=\"size-medium wp-image-6318\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_ldepth-300x168.jpg\" alt=\"Emboss with low depth value\" width=\"300\" height=\"168\" /></a> Emboss with low depth value[/caption]\n\n</div>\n<div style=\"display: inline-block;\">\n\n[caption id=\"attachment_6315\" align=\"alignnone\" width=\"300\"]<a href=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth.jpg\"><img class=\"size-medium wp-image-6315\" src=\"http://sandbox.arrayfire.com/wp-content/uploads/2014/07/emboss_hdepth-300x168.jpg\" alt=\"Emboss with high depth value\" width=\"300\" height=\"168\" /></a> Emboss with high depth value[/caption]\n\n</div>\n</div>\n<h4>Conclusion</h4>\nAll the functions from this post are updated and available through github repository located <a href=\"https://github.com/9prady9/wildfire\">here</a>.','Image editing using ArrayFire: Part 3','','inherit','open','open','','6334-autosave-v1','','','2014-08-13 18:59:28','2014-08-13 18:59:28','',6334,'http://sandbox.arrayfire.com/6334-autosave-v1/',0,'revision','',0),(6379,17728,'2014-08-18 17:06:33','2014-08-18 17:06:33','At SIGGRAPH 2014 The Khronos Group announced, among other things, the <a href=\"https://www.khronos.org/news/press/khronos-releases-spir-2.0-provisional-specification\">SPIR 2.0</a> provisional specification. This release of SPIR (Standard Portable Intermediate Representation) follows the release of <a href=\"https://www.khronos.org/news/press/khronos-releases-opencl-2.0\">OpenCL 2.0</a> spec last year.\r\n\r\nIn this post, we would like to offer our take on SPIR 2.0 and what it means to OpenCL developers.\r\n<h3>Feature Parity With OpenCL 2.0</h3>\r\nSPIR 1.2 had feature parity with OpenCL 1.2. With 2.0, SPIR has feature parity with OpenCL 2.0. Here are a few new features that we find interesting.\r\n<h5>Generic Address Space</h5>\r\n<blockquote>\"Where functions can be written without specifying a named address space for arguments, especially useful for those arguments that are declared to be a pointer to a type, eliminating the need for multiple functions to be written for each named address space used in an application.\"</blockquote>\r\nThis greatly reduces code redundancy and increases code reusability. Many algorithms perform the same operations on <code>__global</code> memory and <code>__local</code> memory at various points in the algorithm. A great example of an algorithm that benefits from this is parallel reduction algorithm.\r\n<h5>Device Side Kernel Enqueue</h5>\r\n<blockquote>\"Where device kernels can enqueue kernels to the same device with no host interaction, enabling flexible work scheduling paradigms and avoiding the need to transfer execution control and data between the device and host, often significantly offloading host processor bottlenecks.\"</blockquote>\r\nWhile this sounds like a nice feature to have, we are skeptical about the performance benefits. The improvements in performance will be highly dependent on the platform specific implementation of the scheduler and may not provide reliable improvements across the board. This feature can increase the readability of complex algorithms.\r\n<h5>Pipes</h5>\r\n<blockquote>\"memory objects that store data organized as a FIFO. OpenCL 2.0 provides built-in functions for kernels to read from or write to a pipe, providing straightforward programming of pipe data structures that can be highly optimized by OpenCL implementers.\"</blockquote>\r\nPipes are FIFO data structures that enable communication between two kernels being executed concurrently. This feature seems to be designed for architectures that support streaming data and will likely be inefficient on OpenCL capable GPUs.\r\n<h3>SPIR Compiler and Tools</h3>\r\nThe Khronos group also added the <a href=\"https://github.com/KhronosGroup/SPIR\">SPIR compiler</a> and <a href=\"https://github.com/KhronosGroup/SPIR-Tools\">SPIR Tools</a> to their github page.\r\n<blockquote>\r\n<ul>\r\n	<li>Modified Clang which generates SPIR from OpenCL C kernel language device programs;</li>\r\n	<li>SPIR module verifier, written in the form of an LLVM pass;</li>\r\n	<li>SPIR built-ins name mangler, written as a standalone library and executable;</li>\r\n	<li>header file containing definitions for all enumerated values in the SPIR specification.</li>\r\n</ul>\r\n</blockquote>\r\nThe SPIR compiler is a patched version of <code>clang</code> that compiles <code>OpenCL C</code> code into <code>SPIR</code>. <code>SPIR tools</code> repository enables the usage of OpenCL builtin functions in SPIR. We\'ll be exploring the SPIR compiler a lot more in a follow up post.\r\n<h3>Future of SPIR</h3>\r\nWhile it is encouraging to see continued development of SPIR, it is still an optional extension in OpenCL. AMD and Intel have supported SPIR since their OpenCL 1.2 implementations. NVIDIA has NVVM IR that may or may not be compatible with SPIR.\r\n\r\nOpenCL brought the portability across different architectures. Similarily, SPIR will bring portability across multiple programming languages. All the vendors would be doing themselves and the ecosystem a favor if they support SPIR and implemented their compute language of choice on top of it.','OpenCL SPIR 2.0','','publish','open','open','','opencl-spir-2-0','','','2014-08-18 17:06:33','2014-08-18 17:06:33','',0,'http://sandbox.arrayfire.com/?p=6379',0,'post','',1),(6380,18132,'2014-08-12 09:51:03','2014-08-12 09:51:03','<a title=\"theregister\" href=\"http://www.theregister.co.uk/2014/08/11/khronos_unveils_opengl_4_5_and_spir_2/\">Many</a>, <a title=\"phoronix\" href=\"http://www.phoronix.com/scan.php?page=news_item&amp;px=MTc1OTk\">outlets</a> are carrying the news that the Khronos group released the provisional SPIR 2.0 spec for OpenCL this Monday. This is exciting news, but what does the new spec mean for all the current (and potential) OpenCL users out there?\r\n\r\nThe Khronos website lists the following interesting aspects of the new spec:\r\n<ul style=\"color: #222222;\">\r\n	<li>Generic address space – where functions can be written without specifying a named address space for arguments, especially useful for those arguments that are declared to be a pointer to a type, eliminating the need for multiple functions to be written for each named address space used in an application;</li>\r\n	<li>Device side kernel enqueue – where device kernels can enqueue kernels to the same device with no host interaction, enabling flexible work scheduling paradigms and avoiding the need to transfer execution control and data between the device and host, often significantly offloading host processor bottlenecks;</li>\r\n	<li>C++11 atomics – a subset of C11 atomics and synchronization operations to enable assignments in one work-item to be visible to other work-items in a work-group, across work-groups executing on a device or for sharing data between the OpenCL device and host;</li>\r\n	<li>Pipes - memory objects that store data organized as a FIFO. OpenCL 2.0 provides built-in functions for kernels to read from or write to a pipe, providing straightforward programming of pipe data structures that can be highly optimized by OpenCL implementers.</li>\r\n</ul>\r\n@pavan, @umar do you have experience with these? Does Shehzan have good experience? A good post might be to give a good example of each of these with motivation.\r\n\r\n&nbsp;\r\n\r\nThe first commentator on the Khronos thread posts his observations,\r\n<ul>\r\n	<li>SPIR is now based on LLVM 3.4 IR (previously it was 3.2)</li>\r\n	<li>Memory ordering &amp; scope has been added (fences which control where memory changes are visible, needed for SVM)</li>\r\n	<li>Support for device-kernel-enqueing by providing blocks (%opencl.block) and ndrange_t types. The ndrange_t definition is trivial and just as expected. Make sure to look at how blocks are represented/implemented, this is partially implementation-defined with quite a bit of \"high-level\" built-ins to kick-off a block. This is a large addition, and there is a bunch of built-ins to support blocks.</li>\r\n	<li>Loop unrolling metadata. While alone it\'s not too interesting, there\'s now a clean way how such hints can be passed from the front-end into SPIR.</li>\r\n	<li>Pipes. I\'m not quite sure I follow the documentation, but on the SPIR level, pipe read/writes are modelled as function calls to a (runtime provided?) function. I thought there would be a generic built-in instead. I assume the fact that the provided function contains the whole type will allow vendors to optimize the writes, but it still seams a bit weird to me (why not read/write_pipe with a metadata block describing the struct layout, if any?)</li>\r\n</ul>\r\n&nbsp;\r\n\r\nAgain, it would be great to do something as far as examples, here','What it Means for You: OpenCL SPIR 2.0','','inherit','open','open','','6379-revision-v1','','','2014-08-12 09:51:03','2014-08-12 09:51:03','',6379,'http://sandbox.arrayfire.com/6379-revision-v1/',0,'revision','',0),(6383,17796,'2014-08-20 17:23:56','2014-08-20 17:23:56','In this blog we will review the benefit of using element-wise operations for your computations. Element-wise operations are operations that are applied to every element in an array and allow the user to avoid coding loops and nested loops for rudimentary operations. In a simple example of an element-wise operation, we use both the addition (+) and multiply (*)operations:\r\n<pre>array A = randu(1024, 1024), B = randu(1024, 1024);\r\narray C=A*A+B*B;</pre>\r\nAn element-wise operator that is applied to a single element is unary operator. An operator that works on two elements is a binary operator. ArrayFire has implemented a large number element-wise operations that are applied to the elements of an array. These operators can help reduce the programming overhead for an application designer as:\r\n<ul>\r\n	<li>Performance - the application designed need not worry about implementing the operator. As these are implemented in OpenCL (with a focus on achieving high utilization), the programmer does not have deal with the partitioning of the work and load-balancing.</li>\r\n	<li>Completeness - the array data type is no different than any other C scalar data type. This means that you can user operators such as &lt; or &lt;=.</li>\r\n	<li>Readability and maintenance - high performance computing becomes as simple as writing a script.</li>\r\n</ul>\r\nOur element-wise operations also include operations between an array and a scalar value. For example:\r\n<pre>array A=randu(1024, 1024),B; \r\nB=A+1;</pre>\r\nA small subset of our operators include:\r\n<ul>\r\n	<li>sin - computes the sine for each element in the array.</li>\r\n	<li>cosine - computes the sine for each element in the array.</li>\r\n	<li>abs - computes the absolute value for each element in the array.</li>\r\n	<li>trunc - truncates every element in the array.</li>\r\n</ul>\r\nA full list of element-wise operators can also be found in our document under: <a href=\"http://sandbox.arrayfire.com/docs/group__arith.htm\"><span style=\"text-decoration: underline;\">element-wise operators</span></a> and <span style=\"text-decoration: underline;\"><a title=\"operator overloading\" href=\"http://sandbox.arrayfire.com/docs/group__array__operator.htm\" target=\"_blank\">operator-overloading functions</a></span>. These include trigonometric, arithmetic, logical,and utility functions. We will extend the discussion on these in future posts.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;','The Benefits of Array Element-Wise Operations ','','publish','open','open','','the-benefits-of-array-element-wise-operations','','','2014-08-20 17:25:15','2014-08-20 17:25:15','',0,'http://sandbox.arrayfire.com/?p=6383',0,'post','',1),(6384,17796,'2014-08-12 15:16:31','2014-08-12 15:16:31','In this blog we will review the benefit of using element-wise operations for your computations. Element-wise operations are operations that are between two data elements in two arrays. These elements are located in the same place in their respective arrays. The simplest example of an element-wise operation is the addition (+) operation.\r\n\r\n<pre>array A=array(1024,1024), B=array(1024,1024), C=array(1024,1024);\r\n\r\n\r\n</pre>\r\n&nbsp;','The Benefits of Array Element-Wise Operations ','','inherit','open','open','','6383-revision-v1','','','2014-08-12 15:16:31','2014-08-12 15:16:31','',6383,'http://sandbox.arrayfire.com/6383-revision-v1/',0,'revision','',0),(6385,17796,'2014-08-12 15:16:45','2014-08-12 15:16:45','In this blog we will review the benefit of using element-wise operations for your computations. Element-wise operations are operations that are between two data elements in two arrays. These elements are located in the same place in their respective arrays. The simplest example of an element-wise operation is the addition (+) operation.\r\n\r\n<pre>array A=array(1024,1024), B=array(1024,1024), C=array(1024,1024);\r\nC=A+B;\r\n</pre>\r\n&nbsp;','The Benefits of Array Element-Wise Operations ','','inherit','open','open','','6383-revision-v1','','','2014-08-12 15:16:45','2014-08-12 15:16:45','',6383,'http://sandbox.arrayfire.com/6383-revision-v1/',0,'revision','',0),