We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I am trying to write a code which detects contours and then blurs ALL of the area outside of the contour. I am attaching my ENTIRE code so that it can be easily debugged.
import gab.opencv.*;
Blurrer b;
OpenCV opencv;
PImage src, canny, scharr, sobel;
void setup() {
src = loadImage("20.jpg");
size(src.width, src.height);
opencv = new OpenCV(this, src);
opencv.findCannyEdges(150,200);
canny = opencv.getSnapshot();
image(canny,0,0);
loadPixels();
for (int i = 0; i < width*height; i++) {
int red = (0xff0000&pixels[i])>>16; // Get red
int green = (0xff00&pixels[i])>>8; // Get green
int blue = 0xff&pixels[i]; // Get blue
if(red>200&&green>200&&blue>200){ pixels[i] = 0xff0000;
int y = i/width;
int x = i%width;
if(x<width/2&&y<height/2) {pixels[i] = 0xff0000; b.blurtopleft(i);}
else if (x>width/2&& y<height/2) {pixels[i]=0x00ff00;}// blurtopright(i);}
else if (x<width/2 && y>height/2) {pixels[i] = 0xff;}// blurbottomleft(i);}
else if (x>width/2 && y>height/2) {pixels[i] = 0xffffff;}// blurbottomright(i);}
} }
updatePixels();
opencv.loadImage(src);
opencv.findCannyEdges(100,200);
//opencv.findScharrEdges(OpenCV.VERTICAL);
//opencv.findScharrEdges(OpenCV.VERTICAL);
scharr = opencv.getSnapshot();
}
void draw() {
pushMatrix();
scale(0.5);
//image(src, 0, 0);
// image(canny, src.width, 0);
//image(scharr, 0, src.height);
// image(sobel, src.width, src.height);
popMatrix();
text("Source", 10, 25);
text("Canny", src.width/2 + 10, 25);
text("Scharr", 10, src.height/2 + 25);
text("Sobel", src.width/2 + 10, src.height/2 + 25);
}
class Blurrer {
int pix;
PImage src = loadImage("20.jpg");
Blurrer(int p) {
pix = p;
}
void blurtopleft(int pix) {
image(src,0,0);
loadPixels();
int xmax = pix%width;
int ymax = pix/width;
for(int i=0; i<pix%width; i++) // in x direction
{
for(int j=0; j<pix/width; j++) // in y direction
{
if(i==0 && j==0) pixels[j*width+i] = (pixels[((j+1)*width+i)] + pixels[j*width+i+1] + pixels[(j+1)*width+i+1])/3; // topleft corner pixels, average of only 3 surrounding pixels
else if (i==0 && j>0 && j<ymax) pixels[j*width+i] = (pixels[((j+1)*width+i)] + pixels[j*width+i+1] + pixels[(j+1)*width+i+1] + pixels[(j-1)*width+i] + pixels[(j-1)*width+i+1])/5; // left edge pixels, average of 5 surrounding pixels
else if (i>0 && j==0 && i<xmax) pixels[j*width+i] = (pixels[((j+1)*width+i)] + pixels[j*width+i+1] + pixels[(j+1)*width+i+1] + pixels[j*width+i-1] + pixels[(j+1)*width+i-1])/5; //top edge pixels, average of 5 surrounding pixels
else if (i>0 && j>0 && i<xmax && j<ymax) pixels[j*width+i] = (pixels[((j+1)*width+i)] + pixels[j*width+i+1] + pixels[(j+1)*width+i+1] + pixels[j*width+i-1] + pixels[(j+1)*width+i-1] + pixels[(j-1)*width+i-1] + pixels[(j-1)*width+i] + pixels[(j-1)*width+i+1])/8; // center pixels, average of all 8 surrounding pixels
}
}
updatePixels();
}
}
So broadly what I am doing is, dividing the entire region into 4 parts and blurring out the topleft region outside of the edge in 1st quadrant, topright to the edge in second quadrant and so on.. I have not written the blurring code for any other quadrant but 1st. I tried to test it but I am getting a null pointer exception.
Can somebody please suggest where I went wrong and what more do I need to declare to get rid of this error. For more insight I am also attaching the image I am working on to ensure we are exactly on the same page. Although I know it is not necessary.
Answers
which line is the error?
which line is yellow when you get the error
I had to remove 2(!)
}
....................won't run under processing 3 because of vars in
size()
i get a nullpointerexception in line 26 when I try to call b.blurtopleft(i);
U can simply declare size(1280,720) It is running fine if I dont access the class functions. When I run the blurrer function blurtopleft that is when I get an error. Else it is running fine until then
So I believe there's an issue in the way I have declared the function or the class somewhere. Can you have a look at my syntax and let me know if there are any issues with that.
In a fast look I did not find a line creating
b
, likeb = new Blurrer(x)
.So b is null when you try to access it in line 26...
yes, as _vk suggests
is not enough. it defines what type of thing b is, but doesn't create that thing.
this does.
Note that the constructor defined expects an int to be passed so.
without any value between parenthesis will give you a:
constructor blah is not defined error
it is unnecessary though that the constr has a param because it is passed to the field
pix
which is then never used (since it is overshadowed by the parampix
in the methodblurtopleft
and not used elsewhere).Therefore
so this
can be
(
src
is also a global image so unnecessary here as well)