Saturday, 25 May 2013

Help me understand how to index a pixel in an image

Help me understand how to index a pixel in an image

I had a look at the code for the implementation of this paper Graph-Based segmentation by Pedro F. Felzenszwalb
But I didn't understand how in the below code that y * width + x is used to build a graph from an image.
// build graph
edge *edges = new edge[width*height*4];
int num = 0;
for (y = 0; y < height; y++) {
    for ( x = 0; x < width; x++) {
        if (x < width-1) {
             edges[num].a = y * width + x;
             edges[num].b = y * width + (x+1);
             edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
             num++;
         }

         if (y < height-1) {
            edges[num].a = y * width + x;
            edges[num].b = (y+1) * width + x;
            edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
            num++;
         }

         if ((x < width-1) && (y < height-1)) {
            edges[num].a = y * width + x;
            edges[num].b = (y+1) * width + (x+1);
            edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
            num++;
         }

         if ((x < width-1) && (y > 0)) {
            edges[num].a = y * width + x;
            edges[num].b = (y-1) * width + (x+1);
            edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
            num++;
         }
     }
}

No comments:

Post a Comment