//cxcore.h
/* Draws 4-connected, 8-connected or antialiased line segment connecting two points */
CVAPI(void) cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
CvScalar color, int thickness CV_DEFAULT(1),
int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
//cxDrawing.cpp
CV_IMPL void
cvLine( CvArr* _img, CvPoint pt1, CvPoint pt2, CvScalar color,
int thickness, int line_type, int shift )
{
cv::Mat img = cv::cvarrToMat(_img);
cv::line( img, pt1, pt2, color, thickness, line_type, shift );
}
//cxDrawing.cpp
/****************************************************************************************\
* External functions *
\****************************************************************************************/
void line( Mat& img, Point pt1, Point pt2, const Scalar& color,
int thickness, int line_type, int shift )
{
if( line_type == CV_AA && img.depth() != CV_8U )
line_type = 8;
CV_Assert( 0 <= thickness && thickness <= 255 );
CV_Assert( 0 <= shift && shift <= XY_SHIFT );
double buf[4];
scalarToRawData( color, buf, img.type(), 0 );
ThickLine( img, pt1, pt2, buf, thickness, line_type, 3, shift );
}
static inline void scalarToRawData(const Scalar& s, void* buf, int type, int unroll_to=0)
{
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
switch(depth)
{
case CV_8U:
s.convertTo((uchar*)buf, cn, unroll_to);
break;
case CV_8S:
s.convertTo((schar*)buf, cn, unroll_to);
break;
case CV_16U:
s.convertTo((ushort*)buf, cn, unroll_to);
break;
case CV_16S:
s.convertTo((short*)buf, cn, unroll_to);
break;
case CV_32S:
s.convertTo((int*)buf, cn, unroll_to);
break;
case CV_32F:
s.convertTo((float*)buf, cn, unroll_to);
break;
case CV_64F:
s.convertTo((double*)buf, cn, unroll_to);
break;
default:
CV_Error(CV_StsUnsupportedFormat,"");
}
}
static void
ThickLine( Mat& img, Point p0, Point p1, const void* color,
int thickness, int line_type, int flags, int shift )
{
static const double INV_XY_ONE = 1./XY_ONE;
p0.x <<= XY_SHIFT - shift;
p0.y <<= XY_SHIFT - shift;
p1.x <<= XY_SHIFT - shift;
p1.y <<= XY_SHIFT - shift;
if( thickness <= 1 )
{
if( line_type < CV_AA )
{
if( line_type == 1 || line_type == 4 || shift == 0 )
{
p0.x = (p0.x + (XY_ONE>>1)) >> XY_SHIFT;
p0.y = (p0.y + (XY_ONE>>1)) >> XY_SHIFT;
p1.x = (p1.x + (XY_ONE>>1)) >> XY_SHIFT;
p1.y = (p1.y + (XY_ONE>>1)) >> XY_SHIFT;
Line( img, p0, p1, color, line_type );
}
else
Line2( img, p0, p1, color );
}
else
LineAA( img, p0, p1, color );
}
else
{
Point pt[4], dp = Point(0,0);
double dx = (p0.x - p1.x)*INV_XY_ONE, dy = (p1.y - p0.y)*INV_XY_ONE;
double r = dx * dx + dy * dy;
int i, oddThickness = thickness & 1;
thickness <<= XY_SHIFT - 1;
if( fabs(r) > DBL_EPSILON )
{
r = (thickness + oddThickness*XY_ONE*0.5)/std::sqrt(r);
dp.x = cvRound( dy * r );
dp.y = cvRound( dx * r );
pt[0].x = p0.x + dp.x;
pt[0].y = p0.y + dp.y;
pt[1].x = p0.x - dp.x;
pt[1].y = p0.y - dp.y;
pt[2].x = p1.x - dp.x;
pt[2].y = p1.y - dp.y;
pt[3].x = p1.x + dp.x;
pt[3].y = p1.y + dp.y;
opencv中cvLine的实现
最新推荐文章于 2025-03-26 21:49:27 发布