OpenCV  3.4.19
Open Source Computer Vision
Public Member Functions | Public Attributes | List of all members
cv::LineIterator Class Reference

Class for iterating over all pixels on a raster line segment. More...

#include <opencv2/imgproc.hpp>

Public Member Functions

 LineIterator (const Mat &img, Point pt1, Point pt2, int connectivity=8, bool leftToRight=false)
 Initializes iterator object for the given line and image. More...
 
ucharoperator* ()
 Returns pointer to the current pixel. More...
 
LineIteratoroperator++ ()
 Moves iterator to the next pixel on the line. More...
 
LineIterator operator++ (int)
 Moves iterator to the next pixel on the line. More...
 
Point pos () const
 Returns coordinates of the current pixel. More...
 

Public Attributes

int count
 
int elemSize
 
int err
 
int minusDelta
 
int minusStep
 
int plusDelta
 
int plusStep
 
ucharptr
 
const ucharptr0
 
int step
 

Detailed Description

Class for iterating over all pixels on a raster line segment.

The class LineIterator is used to get each pixel of a raster line connecting two specified points. It can be treated as a versatile implementation of the Bresenham algorithm where you can stop at each pixel and do some extra processing, for example, grab pixel values along the line or draw a line with an effect (for example, with XOR operation).

The number of pixels along the line is stored in LineIterator::count. The method LineIterator::pos returns the current position in the image:

// grabs pixels along the line (pt1, pt2)
// from 8-bit 3-channel image to the buffer
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;
// alternative way of iterating through the line
for(int i = 0; i < it2.count; i++, ++it2)
{
Vec3b val = img.at<Vec3b>(it2.pos());
CV_Assert(buf[i] == val);
}

Constructor & Destructor Documentation

◆ LineIterator()

cv::LineIterator::LineIterator ( const Mat img,
Point  pt1,
Point  pt2,
int  connectivity = 8,
bool  leftToRight = false 
)

Initializes iterator object for the given line and image.

The returned iterator can be used to traverse all pixels on a line that connects the given two points. The line will be clipped on the image boundaries.

Parameters
imgUnderlying image.
pt1First endpoint of the line.
pt2The other endpoint of the line.
connectivityPixel connectivity of the iterator. Valid values are 4 (iterator can move up, down, left and right) and 8 (iterator can also move diagonally).
leftToRightIf true, the line is traversed from the leftmost endpoint to the rightmost endpoint. Otherwise, the line is traversed from pt1 to pt2.

Member Function Documentation

◆ operator*()

uchar* cv::LineIterator::operator* ( )

Returns pointer to the current pixel.

◆ operator++() [1/2]

LineIterator& cv::LineIterator::operator++ ( )

Moves iterator to the next pixel on the line.

This is the prefix version (++it).

◆ operator++() [2/2]

LineIterator cv::LineIterator::operator++ ( int  )

Moves iterator to the next pixel on the line.

This is the postfix version (it++).

◆ pos()

Point cv::LineIterator::pos ( ) const

Returns coordinates of the current pixel.

Member Data Documentation

◆ count

int cv::LineIterator::count

◆ elemSize

int cv::LineIterator::elemSize

◆ err

int cv::LineIterator::err

◆ minusDelta

int cv::LineIterator::minusDelta

◆ minusStep

int cv::LineIterator::minusStep

◆ plusDelta

int cv::LineIterator::plusDelta

◆ plusStep

int cv::LineIterator::plusStep

◆ ptr

uchar* cv::LineIterator::ptr

◆ ptr0

const uchar* cv::LineIterator::ptr0

◆ step

int cv::LineIterator::step

The documentation for this class was generated from the following file: