Counting the number of black pixels in an array grid
in
Programming Questions
•
8 months ago
Hi there, I need some help. I'm fairly new to the whole processing thing and am trying to write a script that takes a grid in a 600x600 screen and analyses the number of black pixels there are in each grid square.
The final aim is to take the numerical value and extrude the grid square into 3 dimensions with a height that is dependant on the number of black pixels found in the square.
From looking around the internet I'm guessing that 'blobscanner' is the library to use, however I'm not sure how to apply the pixel count to an array.
Any help would be so very appreciated. The code as it stands is as below....
- import Blobscanner.*;
- Tile [][] grid;
- //number of rows and columns
- int cols;
- int rows;
- PImage img;
- void setup() {
- img = loadImage("scan - 067.png");
- size(600, 600);
- background(img);
- cols=width;
- rows=height;
- //the array dimensions are equal to n.of rows/columns
- grid = new Tile[cols][rows];
- //loop through cols and rows
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- //initiate objects
- grid[i][j] = new Tile (i*15, j*15, 60, 200);
- }
- }
- }
- void draw() {
- noFill();
- for (int i=0; i<cols; i++) {
- for (int j=0; j<rows; j++) {
- //display the grid
- grid[i][j].display();
- }
- }
- }
- void keyPressed(){
- if(key == 'g'){
- grid[4][4].black();
- }
- }
- class Tile {
- float x, y;
- float side;
- int col;
- Tile (float tempX, float tempY, float tempSide, int tempCol) {
- x = tempX;
- y = tempY;
- side = tempSide;
- col = tempCol;
- }
- void display() {
- stroke(150,178,206);
- noFill();
- rect (x, y, side, side);
- }
- void black() {
- col = color(0);
- }
- }
Thanks for even reading this guys!
Ben
1