Blur logic but i don't think its working );

edited February 2017 in Library Questions
            import processing.video.*;

            Capture video;
            PImage frog;
            public static final int[][] mask = {{1, 1, 1},
                                                   {1, 1, 1},
                                                   {1, 1, 1} };
            void setup()
            {
            size(640,480);
            video = new Capture(this,640,480);
            video.start();
            frog = loadImage("frog.jpg");

            }

            void captureEvent(Capture video){

              video.read();
            }


            void draw()
            {

              loadPixels();
              video.loadPixels();
              frog.loadPixels();
              for(int x=1;x+1<video.width;x++)
              {
               for(int y=1;y+1<video.height;y++)
               {
                 int loc  =  x+y*video.width;



                 color value  = applyBlur(video,x,y,mask);

                 pixels[loc] = value;


               }
              }
              updatePixels();
              }


              color applyBlur(Capture video,int x,int y,int[][] mask)
              {
                int red=0,green=0,blue=0;
                for(int j=-1;j<=1;j++)
                {
                 for(int k=-1;k<=1;k++)
                 {

                    red  += mask[1+j][1+k]*(red(video.get(x+k,y+j))); 
                    green +=mask[1+j][1+k]*(green(video.get(x+k,y+j)));
                    blue  += mask[1+j][1+k]*(blue(video.get(x+k,y+j)));
                 } 
                }
                   red = red/findSum(mask);
                   green = green/findSum(mask);
                   blue = blue/findSum(mask);

                   color value  = color(red,green,blue);
                   return value;

              }

              int  findSum(int[][] mask)
              {
                int sum=0;
                for(int y=0;y<mask.length;y++)
                {
                   for(int x=0;x<mask[y].length;x++)
                   {
                     sum += mask[y][x];
                   }
                }
                  return sum;

              }

Answers

  • Your convolution matrix, mask, is all ones...

  • Oh, OK, my mistake.

    https://en.m.wikipedia.org/wiki/Kernel_(image_processing)

  • This doesn't look right

    red  += mask[1+j][1+k]*(red(video.get(x+k,y+j))); 
    

    The way you use j first in mask and the k first in the get, but in reality all you've done is flipped the convolution matrix, which is symmetrical anyway, so it isn't a problem here.

    Will have a better look when I'm not on my phone

  • Possible integer division problem in lines 61-63

  • I don't think it's working.

    What exactly is the problem occurring?

  • edited February 2017

    ok, that is generally sound, but the effect is very subtle and hard to see because of the fact it's video. if you use a still image then it's easier to see, so i've changed your code to do just one image.

    PImage frog;
    public static final int[][] mask = {
      {1, 1, 1}, 
      {1, 1, 1}, 
      {1, 1, 1}
    };
    int factor;
    String filename = "https://" + "pbs.twimg.com/profile_images/717404632260091904/4-snvNTU.jpg";
    
    void setup() {
      size(256, 512);  // frog image is 256x256, this is twice that height
      frog = loadImage(filename);
      factor = findSum(mask);
    }
    
    void draw() {
    
      image(frog, 0, 0);
      loadPixels();
      for (int x = 1; x + 1 < frog.width; x++) {
        for (int y = 1; y + 1 < frog.height; y++) {
          int loc = x + y * frog.width;
          color value = applyBlur(frog, x, y, mask);
          pixels[loc + frog.width * frog.height] = value;
        }
      }
      updatePixels();
      noLoop();
    }
    
    color applyBlur(PImage video, int x, int y, int[][] mask) {
      int r = 0, g = 0, b = 0;
      for (int j = -1; j <= 1; j++) {
        for (int k = -1; k <= 1; k++) {
          int pixel = video.get(x + k, y + j);
          r += mask[1 + j][1 + k] * red(pixel); // TODO bitshifts
          g += mask[1 + j][1 + k] * green(pixel);
          b += mask[1 + j][1 + k] * blue(pixel);
        }
      }
      r /= factor;
      g /= factor;
      b /= factor;
    
      color value  = color(r, g, b);
      return value;
    }
    
    int  findSum(int[][] mask) {
      int sum = 0;
      for (int y = 0; y < mask.length; y++) {
        for (int x = 0; x < mask[y].length; x++) {
          sum += mask[y][x];
        }
      }
      return sum;
    }
    

    the applyBlur does exactly the same thing as yours does but i've optimised it a bit, (but not completely) - there's no point in calling findsum() 3 times for ever pixel because it doesn't change (call it once and store it in a variable - factor). and i've renamed your red, green, blue variables because they clash with the names of methods.

    it's faster but still very slow.

Sign In or Register to comment.