Pregunta

I have a black and white image, and I'm only interested in finding the x-position of the furthest right black pixel, but I'm not sure how to proceed. Any help would be appreciated. Oh, and I'm using CImg and VC2008.

Alright, I feel pretty dumb since I didn't realize for loops could be iterated backwards. Anyways, here is what I have now.

int right_edge(CImg<unsigned char> bw)
{
    int width = bw.width();
    int height = bw.height();
    for( int i=height; i>0; i-- ){
        for( int j=width; j>0; j-- ){
            if( bw[j,i] == (0,0,0) )     //I know this line is the problem
                cout << j << endl;
            return 0;
        }
    }
}    

The code compiles, but doesn't output as expected. I know the line with the if statement is formatted wrong. I've tried a whole bunch of Google results, but nothing has seemed to work (ie I'm probably messing up)

¿Fue útil?

Solución

Simple psuedocode algorithm:

for each column of pixels (starting from the rightmost column, moving left)
    for each row
        if this pixel is black
            return x coordinate of column

I am deliberately leaving specifics out, as this seems to be a homework question and no effort has been shown on your part. But, this should be enough to get you started.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top