Ask Your Question
0

Count Pixel

asked 2013-04-04 21:50:05 -0600

Vuong Bui gravatar image

I load image in openCV , then I draw line Point(x1,y1), Point(x2,y2). How to count pixel on line that. Thank you

The same image description

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
2

answered 2013-04-05 14:21:31 -0600

AlexanderShishkov gravatar image

updated 2013-04-05 14:22:36 -0600

You should use LineIterator class: http://docs.opencv.org/modules/core/doc/drawing_functions.html?highlight=lineiterator#LineIterator

E.g. if you want to get pixel values failing on that line:

LineIterator it(img, pt1, pt2, 8);
LineIterator it2 = it;
vector<Vec3b> buf(it.count);

for(int i = 0; i < it.count; i++, ++it)
    buf[i] = *(const Vec3b)*it;

it.count in this sample is pixels count for the line between pt1 and pt2 if we use 8 neighbors.

edit flag offensive delete link more

Comments

Did not know about this function, but I assume internally it does something like I suggested :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-05 14:31:09 -0600 )edit

Just wondering. The explanation says Class for iterating pixels on a raster line Does this mean that you can only use this on horizontal and vertical lines?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-05 14:34:20 -0600 )edit

No, It doesn't. You can use it for any types of lines.

AlexanderShishkov gravatar imageAlexanderShishkov ( 2013-04-05 14:50:43 -0600 )edit
0

answered 2013-04-05 03:05:59 -0600

I do not think he wants to calculate the euclidean distance, but he wants to define the pixel values falling on that line.

You should first consider how a line defined by 2 points can be represented mathematically. Given point 1 and point 2, the line equation equals:

Point1 = Point(x1,y1); Point2 = Point(x2,y2);   
y = y1 + [(y2 - y1) / (x2 - x1)]·(x - x1);

This gives you the ability to know exactly which pixels correspond to that line. Loop over your image, enter the x coördinate in the equation and look if the output is still the index your are currently in y position. If this check holds, than add the current value of the pixel to a storage container, for example an int that increases in value each time.

edit flag offensive delete link more

Comments

And basically you do not even want the values, then just increase a counter if the location suits the equation :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-05 03:14:12 -0600 )edit

YES, I want to define the pixel values falling on that line. And Do you same when i use type of line (straight horizontal, vertical, diagonal, slanted..) Thanks you very much

Vuong Bui gravatar imageVuong Bui ( 2013-04-05 04:51:40 -0600 )edit

This equation is only for a straight line, but it can have any possible orientation. If you want curved lines, then defining an equation will be alot harder. Accept answers if they suit you :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-05 04:54:55 -0600 )edit
0

answered 2013-04-05 01:13:20 -0600

berak gravatar image

for a straight line, the euklidean distance is:

sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-04-04 21:50:05 -0600

Seen: 962 times

Last updated: Apr 05 '13