All pixels are 0
in
Contributed Library Questions
•
2 years ago
Hi, there's a lot of code here but don't let that scare you, the problem is quite small.
Everything worked quite well before untill the offscreen buffer with the glgraphics library.
there is this part in draw:
// why is every pixel 0?
detectBlobs(offscreen.getTexture(), color(0, 0, 100)); // hsb!
in the function detectBlob i have this line:
if(img.pixels[count] != 0) println(img.pixels[count]);
It never prints anything.
In draw there is also:
// here it's not!
image(offscreen.getTexture(), 0, 0);
here i draw the offscreen buffer to the window, and there i can see it's not just a solid surface.
I did add a img.updatePixels() in the detectBlobs, it doesn't help (and i didn't need it before).
So why doesn't it recognise the pixels?
An alternative blob library is not an option atm.
Class Nerf
Everything worked quite well before untill the offscreen buffer with the glgraphics library.
there is this part in draw:
// why is every pixel 0?
detectBlobs(offscreen.getTexture(), color(0, 0, 100)); // hsb!
in the function detectBlob i have this line:
if(img.pixels[count] != 0) println(img.pixels[count]);
It never prints anything.
In draw there is also:
// here it's not!
image(offscreen.getTexture(), 0, 0);
here i draw the offscreen buffer to the window, and there i can see it's not just a solid surface.
I did add a img.updatePixels() in the detectBlobs, it doesn't help (and i didn't need it before).
So why doesn't it recognise the pixels?
An alternative blob library is not an option atm.
- import processing.opengl.*;
import codeanticode.glgraphics.*;
Nerf nerf;
// max kleine vertakkingen
int maxDepth = 2;
ArrayList blobs = new ArrayList();
float thresValue = 0.50;
GLGraphicsOffScreen offscreen;
void setup() {
size(800, 800, GLConstants.GLGRAPHICS);
offscreen = new GLGraphicsOffScreen(this, width, height);
colorMode(HSB, 360, 100, 100);
// smooth();
frameRate(30);
int x1 = int(random(0, width));
int x2 = int(random(0, width));
nerf = new Nerf(x1, 0, x2, height);
}
void draw() {
offscreen.beginDraw();
offscreen.colorMode(HSB, 360, 100, 100);
offscreen.background(0, 0, 100); // hsb! wit
nerf.draw(offscreen);
offscreen.endDraw();
// why is every pixel white?
detectBlobs(offscreen.getTexture(), color(0, 0, 100)); // hsb!
// here it's not!
image(offscreen.getTexture(), 0, 0);
// PImage b = copy(offscreen.getTexture(),
// PImage b = get(0, 0, width, height);
// detectBlobs(b, color(0, 0, 100)); // hsb!
//removeSmallBlobs(50); // >= 50 pixels keep.
calcBlobCentres();
showBlobs();
listBlobs();
stroke(0);
//nerf.draw();
// update for next frame
nerf.update();
if (nerf.updateCount > 8) {
int x1 = int(random(0, width));
int x2 = int(random(0, width));
nerf = new Nerf(x1, 0, x2, height);
}
// text(frameCount, 20, 20);
}
// ------
void removeSmallBlobs(int minSize) {
ArrayList big_blobs = new ArrayList();
Blob b;
for (int i = 0; i < blobs.size(); i++) {
b = ((Blob)blobs.get(i));
if (b.nbrPixels() >= minSize)
big_blobs.add(b);
}
blobs = big_blobs;
}
void calcBlobCentres() {
for (int i = 0; i < blobs.size(); i++) {
((Blob)blobs.get(i)).calcCentre();
}
}
// This is the main method for detecting blob shapes
void detectBlobs(PImage img, int col) {
img.loadPixels();
blobs.clear();
int x = 0, y = 0;
int count = 0;
LineBlob lb;
for (y = 0; y < img.height; y++) {
if(img.pixels[count] != 0) println(img.pixels[count]);
x = 0;
while (x < img.width) {
if (img.pixels[count] == col) {
lb = new LineBlob();
lb.y = y; //stores y row
lb.sx = x; //stores startPoint
x++;
count++;
while (x < img.width && img.pixels[count] == col) {
x++;
count++;
}
lb.ex = x-1; //stores startPoint
addToAndMergeBlobs(lb);
}
else {
x++;
count++;
}
}// end x while
}//end y for
}
// Do not call this method directly
void addToAndMergeBlobs(LineBlob lb) {
ArrayList blobsToMergeWith = new ArrayList();
for (int i = 0; i < blobs.size(); i++) {
Blob b = ((Blob)blobs.get(i));
if (b.canMergeLineBlob(lb))
blobsToMergeWith.add(b);
}
if (blobsToMergeWith.size() == 0) {
blobs.add(new Blob(lb));
}
else {
Blob b0 = ((Blob)blobsToMergeWith.get(0));
b0.mergeLineBlob(lb);
for (int i = 1; i < blobsToMergeWith.size(); i++) {
Blob bmerge = ((Blob)blobsToMergeWith.get(i));
b0.mergeBlob(bmerge);
blobs.remove(bmerge);
}
}
}
void showBlobs() {
int showCol = color(255, 0, 0);
for (int i = 0; i < blobs.size(); i++) {
//showCol = color(128 + random(128), 128 + random(128), 128 + random(128));
float h = random(30, 60);
float s = random(60, 100);
float b = random(30, 80);
showCol = color(h, s, b);
((Blob)blobs.get(i)).showBlob(showCol);
}
}
void listBlobs() {
Blob b;
println("Detected "+ blobs.size() + " blobs");
for (int i = 0; i < blobs.size(); i++) {
b = ((Blob)blobs.get(i));
println(" Blob "+ i + "\t has " + b.nbrPixels() + " pixels");
}
}
// This class represents a continuous area of a particular colour
class Blob {
ArrayList lineBlobs = new ArrayList();
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;
int centreX;
int centreY;
Blob(LineBlob lb) {
lineBlobs.add(lb);
}
void calcCentre() {
LineBlob lb;
for (int i = 0; i < lineBlobs.size(); i++) {
lb = (LineBlob)lineBlobs.get(i);
minX = (lb.sx < minX) ? lb.sx : minX;
maxX = (lb.ex < maxX) ? lb.ex : maxX;
minY = (lb.y < minY) ? lb.y : minY;
maxY = (lb.y > maxY) ? lb.y : maxY;
}
centreX = (minX + maxX)/2;
centreY = (minY + maxY)/2;
}
// Sees whether the position x,y is part of the blob
// by seeing if it
boolean contains(int x, int y) {
boolean result = false;
for (int i = 0; i < lineBlobs.size(); i++) {
if (((LineBlob)lineBlobs.get(i)).contains(x, y)) {
result = true;
}
}
return result;
}
// Sees whether a lineblob is adjacent to this blob but does
// NOT merge them
boolean canMergeLineBlob(LineBlob lb) {
boolean result = false;
for (int i = 0; i < lineBlobs.size(); i++) {
if (((LineBlob)lineBlobs.get(i)).hasVertOverlap(lb)) {
result = true;
}
}
return result;
}
// This merges a LineBlob to this Blob
void mergeLineBlob(LineBlob lb) {
lineBlobs.add(lb);
}
void mergeBlob(Blob b) {
lineBlobs.addAll(b.lineBlobs);
}
void showBlob(int showCol) {
stroke(showCol);
for (int i = 0; i < lineBlobs.size() ; i++) {
((LineBlob)lineBlobs.get(i)).showLineBlob(showCol);
}
}
int nbrPixels() {
int n = 0;
for (int i = 0; i < lineBlobs.size(); i++)
n += ((LineBlob)lineBlobs.get(i)).nbrPixels();
return n;
}
}
// Supporting class to Blob
class LineBlob {
int y;
int sx;
int ex;
LineBlob() {
}
LineBlob(int y, int sx, int ex) {
this.y = y;
this.sx = sx;
this.ex = ex;
}
boolean contains(int px, int py) {
if (py == y && px >= sx && px <= ex)
return true;
else
return false;
}
boolean hasVertOverlap(LineBlob b) {
if (Math.abs(b.y - y) == 1) {
if ( (b.sx >= sx && b.sx <= ex) || (sx >= b.sx && sx <= b.ex) ) {
return true;
}
}
return false;
}
void showLineBlob(int showCol) {
line(sx, y, ex, y);
}
int nbrPixels() {
return ex - sx + 1;
}
}
Class Nerf
- class Nerf {
int x1, y1, x2, y2;
ArrayList<Vertakking> vertakkingen = new ArrayList<Vertakking>();
int maxVertakkingen = 5;
int vertakkingenCount = 0;
int updateCount = 0;
Nerf(int x1, int y1, int x2, int y2 ) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
// - - - - - - - - - - - - - - - -
void update() {
if (vertakkingenCount < maxVertakkingen) {
maakVertakking();
vertakkingenCount++;
}
else {
for (Vertakking vt : vertakkingen) {
vt.update();
}
}
updateCount++;
}
// - - - - - - - - - - - - - - - -
void draw(GLGraphicsOffScreen offscreen) {
// hoofdnerf
offscreen.strokeWeight(15);
offscreen.line(x1, y1, x2, y2);
for (Vertakking vt : vertakkingen) {
vt.draw(offscreen);
}
}
// - - - - - - - - - - - - - - - -
void draw() {
// hoofdnerf
strokeWeight(15);
line(x1, y1, x2, y2);
for (Vertakking vt : vertakkingen) {
vt.draw();
}
}
// - - - - - - - - - - - - - - - -
void maakVertakking() {
int x1, y1, x2, y2;
float amt = random(1);
x1 = int(lerp(this.x1, this.x2, amt));
y1 = int(lerp(this.y1, this.y2, amt));
int depth = 0;
float r = random(1);
// links
if (r > 0.5) { // aan linker
//y2 is tussen 0 en y1 in
y2 = int(random(0, y1));
x2 = 0;
vertakkingen.add(new Vertakking(x1, y1, x2, y2, depth, 2));
}
else { // aan bovenkant
y2 = 0;
x2 = int(random(0, x1));
vertakkingen.add(new Vertakking(x1, y1, x2, y2, depth, 2));
}
// rechts
if (r > 0.5) { // aan linker
//y2 is tussen 0 en y1 in
y2 = int(random(0, y1));
x2 = width;
vertakkingen.add(new Vertakking(x1, y1, x2, y2, depth, 2));
}
else { // aan bovenkant
y2 = 0;
x2 = int(random(width, x1));
vertakkingen.add(new Vertakking(x1, y1, x2, y2, depth, 2));
}
}
}
- class Vertakking {
int depth;
int x1, y1, x2, y2;
ArrayList<Vertakking> vertakkingen = new ArrayList<Vertakking>();
int maxVertakkingen;
int vertakkingenCount = 0;
Vertakking(int x1, int y1, int x2, int y2, int depth, int maxVertakkingen) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.depth = depth;
this.maxVertakkingen = maxVertakkingen;
}
// - - - - - - - - - - - - - - - -
void update() {
// println("depth "+depth);
//println("maxDepth "+maxDepth);
if (depth < maxDepth) {
if (vertakkingenCount < maxVertakkingen) {
maakVertakking();
vertakkingenCount++;
}
for (Vertakking vt : vertakkingen) {
vt.update();
}
}
}
// - - - - - - - - - - - - - - - -
void draw( GLGraphicsOffScreen offscreen) {
// kleinere vertakkingen
if(depth == 0)offscreen.strokeWeight(7.5);
if(depth == 1)offscreen.strokeWeight(3);
if(depth > 1)offscreen.strokeWeight(1);
offscreen.line(x1, y1, x2, y2);
for (Vertakking vt : vertakkingen) {
vt.draw(offscreen);
}
}
// - - - - - - - - - - - - - - - -
void draw() {
// kleinere vertakkingen
if(depth == 0)strokeWeight(7.5);
if(depth == 1)strokeWeight(3);
if(depth > 1)strokeWeight(1);
line(x1, y1, x2, y2);
for (Vertakking vt : vertakkingen) {
vt.draw();
}
}
// - - - - - - - - - - - - - - - -
void maakVertakking() {
int x1, y1, x2, y2;
float amt = random(1);
x1 = int(lerp(this.x1, this.x2, amt));
y1 = int(lerp(this.y1, this.y2, amt));
float r = random(1);
int d = this.depth;
d++;
// links
if (r > 0.5) { // aan linker
//y2 is tussen 0 en y1 in
y2 = int(random(0, y1));
x2 = 0;
vertakkingen.add(new Vertakking(x1, y1, x2, y2, d, maxVertakkingen-1));
}
else { // aan bovenkant
y2 = 0;
x2 = int(random(0, x1));
vertakkingen.add(new Vertakking(x1, y1, x2, y2, d, maxVertakkingen-1));
}
// rechts
if (r > 0.5) { // aan linker
//y2 is tussen 0 en y1 in
y2 = int(random(0, y1));
x2 = width;
vertakkingen.add(new Vertakking(x1, y1, x2, y2, d, maxVertakkingen-1));
}
else { // aan bovenkant
y2 = 0;
x2 = int(random(width, x1));
vertakkingen.add(new Vertakking(x1, y1, x2, y2, d, maxVertakkingen-1));
}
}
}
1