I recommend using the image processing infrastructure to focus on algorithms rather than manipulating arrays of values. Some frameworks:
In the case of MarvinJ, you can simply scroll the pixels repeating the columns and the coordinates of the rows. I use getIntComponentX () methods to access the color components.
for(var y=0; y<image.getHeight(); y++){ for(var x=0; x<image.getWidth(); x++){ var red = image.getIntComponent0(x,y); var green = image.getIntComponent1(x,y); var blue = image.getIntComponent2(x,y); } }
Therefore, you do not need to worry about how pixel data is represented. To check if a pixel is white:
// Is white? if(red >= 250 && blue >= 250 && green >= 250){ console.log("Pixel at "+x+","+y+" is white"); }
Runnable Example:
var canvas = document.getElementById("canvas"); image = new MarvinImage(); image.load("https://i.imgur.com/eLZVbQG.png", imageLoaded); function imageLoaded(){ var whitePixels=0; for(var y=0; y<image.getHeight(); y++){ for(var x=0; x<image.getWidth(); x++){ var red = image.getIntComponent0(x,y); var green = image.getIntComponent1(x,y); var blue = image.getIntComponent2(x,y); var alpha = image.getAlphaComponent(x,y);
<script src="https://www.marvinj.org/releases/marvinj-0.7.js"></script> <canvas id="canvas" width="500" height="344"></canvas> <div id="result"></div>
Gabriel Ambrósio Archanjo
source share