Search code examples
opencvdrawingpixel

OPENCV - Pixel count


I load an image in openCV. Then I draw line from Point(x1,y1) to Point(x2,y2). How to count the pixels on that line?


Solution

  • Try to use

    int cvInitLineIterator(const CvArr* image, CvPoint pt1, CvPoint pt2, CvLineIterator* line_iterator, int connectivity=8, int left_to_right=0)
    

    You can find its description here

    "The function initializes the line iterator and returns the number of pixels between the two end points. Both points must be inside the image. After the iterator has been initialized, all the points on the raster line that connects the two ending points may be retrieved by successive calls of CV_NEXT_LINE_POINT point. The points on the line are calculated one by one using a 4-connected or 8-connected Bresenham algorithm."