You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
4.6 KiB
C++
182 lines
4.6 KiB
C++
|
5 years ago
|
#include "cvdrawutils.h"
|
||
|
|
#include "CVUtils.h"
|
||
|
|
|
||
|
|
cv::Mat drawLines(const Mat& img, const vector<PointfPair>& segPointVec)
|
||
|
|
{
|
||
|
|
#ifdef LP_DISABLE_DRAW
|
||
|
|
return Mat();
|
||
|
|
#else
|
||
|
|
|
||
|
|
Mat canvas = normCanvas(img);
|
||
|
|
for (size_t i = 0; i < segPointVec.size(); ++i)
|
||
|
|
{
|
||
|
|
const PointfPair& pointPair = segPointVec[i];
|
||
|
|
cv::line(canvas, pointPair.first, pointPair.second, Scalar(255, 255, 255, 0.5));
|
||
|
|
}
|
||
|
|
return canvas;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
cv::Mat drawLines(const Mat& img, const vector<PointPair>& segPointVec)
|
||
|
|
{
|
||
|
|
#ifdef LP_DISABLE_DRAW
|
||
|
|
return Mat();
|
||
|
|
#else
|
||
|
|
|
||
|
|
Mat canvas = normCanvas(img);
|
||
|
|
for (size_t i = 0; i < segPointVec.size(); ++i)
|
||
|
|
{
|
||
|
|
const PointPair& pointPair = segPointVec[i];
|
||
|
|
cv::line(canvas, pointPair.first, pointPair.second, Scalar(255, 255, 255, 0.5));
|
||
|
|
}
|
||
|
|
return canvas;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
void drawLine(Mat& canvas, const Vec4f& l, const Scalar& color)
|
||
|
|
{
|
||
|
|
line(canvas, Point(l.val[0], l.val[1]), Point(l.val[2], l.val[3]), color);
|
||
|
|
}
|
||
|
|
|
||
|
|
void drawPoint(Mat& canvas, int x, int y, const Scalar& color, int size)
|
||
|
|
{
|
||
|
|
int halfSize = size / 2;
|
||
|
|
Rect rect(x - halfSize, y - halfSize, size, size);
|
||
|
|
#if (CV_MAJOR_VERSION >= 3)
|
||
|
|
rectangle(canvas, rect, color, FILLED);
|
||
|
|
#else
|
||
|
|
rectangle(canvas, rect, color, CV_FILLED);
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
Scalar getRandomColor()
|
||
|
|
{
|
||
|
|
RNG& r = cv::theRNG();
|
||
|
|
return Scalar(r.next() % 256, r.next() % 256, r.next() % 256, 255);
|
||
|
|
}
|
||
|
|
|
||
|
|
Mat getColorCanvas(const Mat& img, float resizeFactor)
|
||
|
|
{
|
||
|
|
Mat canvas;
|
||
|
|
if (resizeFactor == 1.) canvas = img.clone();
|
||
|
|
else resize(img, canvas, Size(), resizeFactor, resizeFactor);
|
||
|
|
if (canvas.channels() == 1) cvtColor(canvas, canvas, CV_GRAY2BGR);
|
||
|
|
return canvas;
|
||
|
|
}
|
||
|
|
|
||
|
|
cv::Mat drawPoints(const Mat& img, const vector<Point>& pointVec, int grayVal /*= 255*/, int xRadius /*= 0*/, int yRadius /*= 0*/)
|
||
|
|
{
|
||
|
|
#ifdef LP_DISABLE_DRAW
|
||
|
|
return Mat();
|
||
|
|
#else
|
||
|
|
Mat canvas = normCanvas(img);
|
||
|
|
for (size_t i = 0; i < pointVec.size(); ++i)
|
||
|
|
{
|
||
|
|
Point pt(pointVec[i]);
|
||
|
|
if (xRadius == 0 && yRadius == 0)
|
||
|
|
{
|
||
|
|
canvas.at<uchar>(pt.y, pt.x) = grayVal;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Rect rect(pt.x - xRadius, pt.y - yRadius, xRadius * 2 + 1, yRadius * 2 + 1);
|
||
|
|
rectangle(canvas, rect, Scalar(grayVal, grayVal, grayVal));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
return canvas;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
cv::Mat drawPointsWithKeyPoints(const Mat& img, const vector<Point>& pointVec)
|
||
|
|
{
|
||
|
|
#ifdef LP_DISABLE_DRAW
|
||
|
|
return Mat();
|
||
|
|
#else
|
||
|
|
vector<KeyPoint> keypointVec;
|
||
|
|
for (size_t i = 0; i < pointVec.size(); ++i)
|
||
|
|
{
|
||
|
|
Point pt = pointVec[i];
|
||
|
|
keypointVec.push_back(KeyPoint(Point2f((float)pt.x, (float)pt.y), 1));
|
||
|
|
}
|
||
|
|
Mat canvas = normCanvas(img);
|
||
|
|
drawKeypoints(canvas, keypointVec, canvas);
|
||
|
|
|
||
|
|
return canvas;
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
void drawRotateRect(Mat& canvas, const RotatedRect& rr, const Scalar& color, int angleLen /*= 30*/)
|
||
|
|
{
|
||
|
|
// center
|
||
|
|
drawPoint(canvas, rr.center.x, rr.center.y, color);
|
||
|
|
|
||
|
|
// draw pose rect
|
||
|
|
Point2f pts[4];
|
||
|
|
rr.points(pts);
|
||
|
|
for (int i = 0; i < 3; ++i)
|
||
|
|
line(canvas, pts[i], pts[i + 1], color);
|
||
|
|
line(canvas, pts[3], pts[0], color);
|
||
|
|
|
||
|
|
// draw angle
|
||
|
|
int xshift = cos(rr.angle / 180 * CV_PI) * angleLen;
|
||
|
|
int yshift = sin(rr.angle / 180 * CV_PI) * angleLen;
|
||
|
|
Point2f p(rr.center.x + xshift, rr.center.y + yshift);
|
||
|
|
line(canvas, Point(rr.center.x, rr.center.y), p, color);
|
||
|
|
}
|
||
|
|
|
||
|
|
#if (CV_MAJOR_VERSION >= 3)
|
||
|
|
Mat highlightRoi(const Mat& roiImg, const Rect& r, const vector<Point2f>& roiVertexes)
|
||
|
|
{
|
||
|
|
assert(r.width == roiImg.cols && r.height == roiImg.rows);
|
||
|
|
|
||
|
|
Mat mask;// = DetectRoi::genMask(r, roiVertexes);
|
||
|
|
Mat canvas(roiImg.rows, roiImg.cols, CV_8UC4);
|
||
|
|
|
||
|
|
// bgr
|
||
|
|
static int from_to1_gay[] = { 0,0,0,1,0,2 };
|
||
|
|
static int from_to1_color[] = { 0,0,1,1,2,2 };
|
||
|
|
int* from_to1 = nullptr;
|
||
|
|
int channelCount = roiImg.channels();
|
||
|
|
if (channelCount == 1)
|
||
|
|
from_to1 = from_to1_gay;
|
||
|
|
else if (channelCount == 3 || channelCount == 4)
|
||
|
|
from_to1 = from_to1_color;
|
||
|
|
else {
|
||
|
|
assert(false && "highlightRoi: unexpected channel.");
|
||
|
|
return gDummyMat;
|
||
|
|
}
|
||
|
|
|
||
|
|
mixChannels(roiImg, canvas, from_to1, 3);
|
||
|
|
|
||
|
|
// a
|
||
|
|
static int from_to2[] = { 0,3 };
|
||
|
|
mixChannels(mask, canvas, from_to2, 1);
|
||
|
|
|
||
|
|
return canvas;
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
void drawPointDir(Mat& canvas, const Point& p, float angle, const Scalar& color, const Scalar& centerColor, int len, int thick)
|
||
|
|
{
|
||
|
|
float halfLen = len / 2.f;
|
||
|
|
float yStep = sin((90 + angle) / 180 * CV_PI) * halfLen;
|
||
|
|
float xStep = cos((90 + angle) / 180 * CV_PI) * halfLen;
|
||
|
|
line(canvas, Point(p.x - xStep, p.y - yStep), Point(p.x + xStep, p.y + yStep), color, thick, CV_AA);
|
||
|
|
#if (CV_MAJOR_VERSION >= 3)
|
||
|
|
rectangle(canvas, p, p, centerColor, FILLED);
|
||
|
|
#else
|
||
|
|
rectangle(canvas, p, p, centerColor, CV_FILLED);
|
||
|
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
|
void drawPatternPose(Mat& canvas, const Size& templateSize, const Vec4f& pose, const Scalar& color)
|
||
|
|
{
|
||
|
|
int cols = templateSize.width;
|
||
|
|
int rows = templateSize.height;
|
||
|
|
RotatedRect rr(Point2f(pose[0], pose[1]), Size2f(pose[3] * cols, pose[3] * cols), pose[2]);
|
||
|
|
|
||
|
|
drawRotateRect(canvas, rr, color);
|
||
|
|
}
|
||
|
|
|