I need a 2D binary representation of the following PNG image:
Can anyone provide a method or a source to convert PNG image above to a 2D array where 1 = lan
It looks like image data returns a single array of rgba components, you just have to break it up into rows. See http://jsfiddle.net/mendesjuan/wjn0dub7/1/
var currentInnerArray;
var zeroesAndOnes = [];
for(var p = 0, len = imdata.length; p < len; p+=4) {
r = imdata[p]
g = imdata[p+1];
b = imdata[p+2];
// Each line is the pixel width * 4, (rgba), start a newline
if (p % imdata.width * 4 === 0) {
currentInnerArray = [];
zeroesAndOnes.push(currentInnerArray);
}
if ((r >= 164 && r <= 255) && (g >= 191 && g <= 229) && (b >= 220 && b <= 255)) {
currentInnerArray.push(0);
// black = water
imdata[p] = 0;
imdata[p+1] = 0;
imdata[p+2] = 0;
} else {
currentInnerArray.push(1);
// white = land
imdata[p] = 255;
imdata[p+1] = 255;
imdata[p+2] = 255;
}
}
// zeroesAndOnes has your 2d array