Ask Your Question
0

first white pixel in binary image

asked Aug 13 '13

abhi1237 gravatar image

updated Aug 13 '13

How to find first white pixel in binary image with opencv c++???

Preview: (hide)

Comments

4

Create two nested for-loops, one that goes from 0 to width and the other one from 0 to height (change the order if necessary, first loop should have the smaller side). Then you can check if the pixel is white with: if (image.at<uchar>(i,j) != 0 ) This is not the fastest way though, since .at might be quite slow.

Moster gravatar imageMoster (Aug 13 '13)edit
1

@Moster: don't hesitate to write your comments as answers :)

Guanta gravatar imageGuanta (Aug 13 '13)edit

Yes, you are right, since I could format the code then

Moster gravatar imageMoster (Aug 13 '13)edit

1 answer

Sort by » oldest newest most voted
4

answered Aug 14 '13

updated Aug 14 '13

The simple way is:

for (int row=0;row<image.height;row++) 
{ 
   for (int col=0;col<image.width;col++) 
    {
          if(image.at<uchar>(i,j) != 0)//do what you want; 
    } 
}

that pixel access isn't the fastest,,, if you need more speed:

for (int row=0;row<image.height;row++) 
{ 
    unsigned char data = image.ptr(row); 
    for (int col=0;col<image.width;col++) 
    { 
          // then use *data for the pixel value
          if(*data==1)//do what you want; 
          data++;
    } 
}
Preview: (hide)

Comments

1

In addition to this nice answer, I would like to point out the documents page that discusses the internal image structure and show different ways of accessing it's data. Look here!

StevenPuttemans gravatar imageStevenPuttemans (Aug 14 '13)edit
6

Instead of if(*data==1) I prefer if ( *data != 0 ) like you did in the first example, since binary images may be 0/255 or 0/1.

Guanta gravatar imageGuanta (Aug 14 '13)edit

Question Tools

Stats

Asked: Aug 13 '13

Seen: 3,443 times

Last updated: Aug 14 '13