StatCounter

Friday, June 6, 2014

Here's a image processing post. I didn't use OpenCV for this, but it gives a taste of basic image processing. This code does border detection by averaging each pixel's neighbors and reflecting the differece between the neighbors and the original pixel using brightness. If there is more difference in brightness, it means there is a border, so the pixel will be brighter. Brighter pixels mean a border, while dimmer ones have less difference in color and aren't a new border.

This is how the neighboring pixels work:


 ———————---
|__A__|__B__|__C_|
|__D__|Origin |_E__|
|__F__|__G___|_H__|

brightness =  abs( neighboringPixelsAverage - origin)
                           
                                               (A+B+C+D+E+F+G+H)
neighboringPixelsAverage =   ------------------------------
                                                                8

Code: (replace "sample.png" with your image name. To add an image to your code, just drag it to the processing's window until it says "One file added to the sketch".)

//Processing code
//You may use this code for non commercial purposes, such as //research into this topic

PImage img;
float pix_up,pix_down,pix_left,pix_right;
int numOfAdjPixels,extra;

void setup() {
size(472,633);
img = loadImage("sample.png");
}

void draw() {
  loadPixels(); 
  // Since we are going to access the image's pixels too  
  img.loadPixels(); 
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
             // Where are we, pixel-wise?
        
        int loc = y + x*width;
        
        float now_color = get_bw(img.pixels[loc]);
        if(x > 1) {//searches for left pixel
          pix_up = get_bw(img.pixels[loc - width]);
          numOfAdjPixels++;
        } else {
          extra+=pix_up;
        }
        if(x < width-1) {//searches for right pixel
          pix_down = get_bw(img.pixels[loc+width]);
          numOfAdjPixels++;
        } else {
           extra+=pix_down;
        }
        if(y > 1) {//searches for top pixel
          pix_left = get_bw(img.pixels[loc-1]);
          numOfAdjPixels++;
        } else {
          extra+=pix_left;
        }
        if(y < height-1) {//search for bottom pixel                  
          pix_right = get_bw(img.pixels[loc+1]);
          numOfAdjPixels++;
        } else {
          extra+=pix_right;
        }
        
        float average = (pix_up + pix_down + pix_left + pix_right - extra)/numOfAdjPixels;
        extra = 0;
        numOfAdjPixels = 0;
        //stroke(r,g,b);
       pixels[loc] = color(abs(now_color - average)*mouseY/10);
    }
  }
  updatePixels();
}

float get_bw(color c) {//get black + white val from datatype color
  return (red(c) + green(c) + blue(c))/3;
}

Here's my results:

Notice how the borders of the candy is highlighted.



Here's the picture that I used:

2 comments:

  1. I need to learn my Arduino skills to know this.

    ReplyDelete
    Replies
    1. FYI, this isn't arduino stuff. It's called Processing. Check my older processing post to see more info 'bout this software.

      Delete