Count Pixel
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
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
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.
Did not know about this function, but I assume internally it does something like I suggested :)
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?
No, It doesn't. You can use it for any types of lines.
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.
And basically you do not even want the values, then just increase a counter if the location suits the equation :)
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 :)
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) );
Asked: 2013-04-04 21:50:05 -0600
Seen: 1,132 times
Last updated: Apr 05 '13
Extracting a vector of pixel values across multiple frames
Reading pixel values from a frame of a video
How to operate on each pixel of a cv2.cv.Mat with a 3 x 3 matrix?
Accessing pixel value always return same value
extract a vector of pixels from a frame
android ndk level access to camera video stream/pixels