Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering specific range of numbers from an array

I'm creating an algorithm that will blur the border of a canvas(image). Before applying the blur effect, I am creating an array filtered that includes all pixel values that needs to be blurred.

I've created an example with a 10×10px image.

function compute(w, h, bW) {
  w *= 4;
  var ignored = [];
  for (y = bW; y < (h - bW); y++) {
    for (x = 0; x < (w - (bW * 4 * 2)); x++) {
      ignored.push(w * y + x + bW * 4);
    }
  }
  console.log(ignored);

  function range(limit) {
    return Array.apply(null, Array(limit)).map(function(_, i) {
      return i;
    })
  }
  Array.prototype.diff = function(array) {
    return this.filter(function(elem) {
      return array.indexOf(elem) === -1;
    })
  }
  var filtered = range(w * h).diff(ignored);
  console.log(filtered);

  return filtered;
}

compute(10, 10, 2);

//////////////////////////////////////////////////////////////////
// Below is just to display the numbers that are being filtered //
//    Here, the size is 100 x 100 px with 10px border width     //
//////////////////////////////////////////////////////////////////

var pixels = compute(100, 100, 10);
var c = document.getElementsByTagName('canvas')[0];
var ctx = c.getContext('2d');
var imgD = ctx.createImageData(100, 100);
for (var i = 0; i < imgD.data.length; i += 4) {
  if (pixels.indexOf(i) > 0) {
    imgD.data[i + 0] = 0;
    imgD.data[i + 1] = 0;
    imgD.data[i + 2] = 0;
    imgD.data[i + 3] = 255;
  } else {
    imgD.data[i + 0] = 255;
    imgD.data[i + 1] = 0;
    imgD.data[i + 2] = 0;
    imgD.data[i + 3] = 255;
  }
}
ctx.putImageData(imgD, 10, 10);
<canvas></canvas>

The array filtered contains all the numbers that has the background color dark and ignored contains all the numbers that has the background color light and lighest in the image.

My question is:

How do I change my code so that the array filtered will have the numbers with background color dark and light and not lighest?

enter image description here


An Example on a bit high resolution(65×65px):

enter image description here


like image 970
Weafs.py Avatar asked Nov 11 '14 01:11

Weafs.py


People also ask

How do you filter an array in a range?

Write a function filterRange(arr, a, b) that gets an array arr , looks for elements with values higher or equal to a and lower or equal to b and return a result as an array. The function should not modify the array. It should return the new array.

Which function filters the values of an array?

filter() calls a provided callbackFn function once for each element in an array, and constructs a new array of all the values for which callbackFn returns a value that coerces to true .

How do you filter out all the even numbers in an array?

To filter even numbers of an integer Array in JavaScript, call Array. filter() method on this integer array, and pass a function as argument that returns true for an even number or false otherwise. filter() method returns an array with elements from the original array that returns true for the given callback function.


1 Answers

FIDDLEJS : http://jsfiddle.net/t14gr6pL/2/

Explanation:

It depends on the width of your triangles (the second color). In the 64*64 example, this width is 7.

Let's assume that this width (tw) is calculate like this (you can change) :

var tw = (2 * bW) - 1;

So your code would be:

function compute(w, h, bW) {
    var filtered = [];
    var WIDTH_MULTIPLICATOR = 4;

    var bH = bW;
    w *= WIDTH_MULTIPLICATOR;
    bW *= WIDTH_MULTIPLICATOR;

    var triangleWidth = (2 * bW) - 1;

    for (y = 0; y < h; y++) {
        for (x = 0; x < w; x++) {
            if (
                // Borders
                (Math.min(x, w - x) < bW) ||
                (Math.min(y, h - y) < bH) ||

                // Adding "Triangles"
                (Math.min(x, w - x) < bW + triangleWidth - Math.max(0, Math.min(y, h - y) - bH) * WIDTH_MULTIPLICATOR)

            ) {
                filtered.push(w * y + x);
            }
        }
    }

    return filtered;
}
like image 55
Nicolas Avatar answered Oct 21 '22 22:10

Nicolas