Ask Your Question
0

first white pixel in binary image

asked 2013-08-13 12:04:25 -0600

abhi1237 gravatar image

updated 2013-08-13 15:48:56 -0600

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

edit retag flag offensive close merge delete

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 ( 2013-08-13 13:07:53 -0600 )edit
1

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

Guanta gravatar imageGuanta ( 2013-08-13 13:46:40 -0600 )edit

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

Moster gravatar imageMoster ( 2013-08-13 14:08:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-08-14 01:50:28 -0600

updated 2013-08-14 01:52:22 -0600

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++;
    } 
}
edit flag offensive delete link more

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 ( 2013-08-14 02:05:46 -0600 )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 ( 2013-08-14 02:57:16 -0600 )edit

Question Tools

Stats

Asked: 2013-08-13 12:04:25 -0600

Seen: 3,420 times

Last updated: Aug 14 '13