Commit 22c8010b authored by Daniel Angelov's avatar Daniel Angelov

Added needed header, changed macro name.

parent 3350533f
......@@ -191,10 +191,10 @@ enum { HOUGH_STANDARD = 0,
HOUGH_GRADIENT = 3
};
//! Variants of Line Segment Detector
//! Variants of Line Segment Detector
enum lsd_refine_lvl
{ LSD_REFINE_NONE = 0,
LSD_REFINE_STD = 1,
{ LSD_REFINE_NONE = 0,
LSD_REFINE_STD = 1,
LSD_REFINE_ADV = 2
};
......@@ -843,35 +843,35 @@ public:
/**
* Create an LSD object. Specifying scale, number of subdivisions for the image, should the lines be refined and other constants as follows:
*
* @param _refine How should the lines found be refined?
* @param _refine How should the lines found be refined?
* REFINE_NONE - No refinement applied.
* REFINE_STD - Standard refinement is applied. E.g. breaking arches into smaller line approximations.
* REFINE_ADV - Advanced refinement. Number of false alarms is calculated,
* REFINE_STD - Standard refinement is applied. E.g. breaking arches into smaller line approximations.
* REFINE_ADV - Advanced refinement. Number of false alarms is calculated,
* lines are refined through increase of precision, decrement in size, etc.
* @param _scale The scale of the image that will be used to find the lines. Range (0..1].
* @param _sigma_scale Sigma for Gaussian filter is computed as sigma = _sigma_scale/_scale.
* @param _quant Bound to the quantization error on the gradient norm.
* @param _quant Bound to the quantization error on the gradient norm.
* @param _ang_th Gradient angle tolerance in degrees.
* @param _log_eps Detection threshold: -log10(NFA) > _log_eps
* @param _density_th Minimal density of aligned region points in rectangle.
* @param _n_bins Number of bins in pseudo-ordering of gradient modulus.
*/
LSD(lsd_refine_lvl _refine = LSD_REFINE_STD, double _scale = 0.8,
double _sigma_scale = 0.6, double _quant = 2.0, double _ang_th = 22.5,
LSD(lsd_refine_lvl _refine = LSD_REFINE_STD, double _scale = 0.8,
double _sigma_scale = 0.6, double _quant = 2.0, double _ang_th = 22.5,
double _log_eps = 0, double _density_th = 0.7, int _n_bins = 1024);
/**
* Detect lines in the input image with the specified ROI.
*
* @param _image A grayscale(CV_8UC1) input image.
* @param _image A grayscale(CV_8UC1) input image.
* @param _lines Return: A vector of Vec4i elements specifying the beginning and ending point of a line.
* Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end.
* Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end.
* Returned lines are strictly oriented depending on the gradient.
* @param _roi Return: ROI of the image, where lines are to be found. If specified, the returning
* @param _roi Return: ROI of the image, where lines are to be found. If specified, the returning
* lines coordinates are image wise.
* @param width Return: Vector of widths of the regions, where the lines are found. E.g. Width of line.
* @param prec Return: Vector of precisions with which the lines are found.
* @param nfa Return: Vector containing number of false alarms in the line region, with precision of 10%.
* @param nfa Return: Vector containing number of false alarms in the line region, with precision of 10%.
* The bigger the value, logarithmically better the detection.
* * -1 corresponds to 10 mean false alarms
* * 0 corresponds to 1 mean false alarm
......@@ -884,16 +884,16 @@ public:
/**
* Draw lines on the given canvas.
*
* @param image The image, where lines will be drawn.
* @param image The image, where lines will be drawn.
* Should have the size of the image, where the lines were found
* @param lines The lines that need to be drawn
*/
*/
static void drawSegments(cv::Mat& image, const std::vector<cv::Vec4i>& lines);
/**
* Draw both vectors on the image canvas. Uses blue for lines 1 and red for lines 2.
*
* @param image The image, where lines will be drawn.
* @param image The image, where lines will be drawn.
* Should have the size of the image, where the lines were found
* @param lines1 The first lines that need to be drawn. Color - Blue.
* @param lines2 The second lines that need to be drawn. Color - Red.
......@@ -905,7 +905,7 @@ private:
cv::Mat image;
cv::Mat_<double> scaled_image;
double *scaled_image_data;
cv::Mat_<double> angles; // in rads
cv::Mat_<double> angles; // in rads
double *angles_data;
cv::Mat_<double> modgrad;
double *modgrad_data;
......@@ -956,18 +956,18 @@ private:
* Detect lines in the whole input image.
*
* @param lines Return: A vector of Vec4i elements specifying the beginning and ending point of a line.
* Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end.
* Where Vec4i is (x1, y1, x2, y2), point 1 is the start, point 2 - end.
* Returned lines are strictly oriented depending on the gradient.
* @param widths Return: Vector of widths of the regions, where the lines are found. E.g. Width of line.
* @param precisions Return: Vector of precisions with which the lines are found.
* @param nfas Return: Vector containing number of false alarms in the line region, with precision of 10%.
* @param nfas Return: Vector containing number of false alarms in the line region, with precision of 10%.
* The bigger the value, logarithmically better the detection.
* * -1 corresponds to 10 mean false alarms
* * 0 corresponds to 1 mean false alarm
* * 1 corresponds to 0.1 mean false alarms
*/
void flsd(std::vector<cv::Vec4i>& lines,
std::vector<double>* widths, std::vector<double>* precisions,
void flsd(std::vector<cv::Vec4i>& lines,
std::vector<double>* widths, std::vector<double>* precisions,
std::vector<double>* nfas);
/**
......@@ -975,13 +975,13 @@ private:
*
* @param threshold The minimum value of the angle that is considered defined, otherwise NOTDEF
* @param n_bins The number of bins with which gradients are ordered by, using bucket sort.
* @param list Return: Vector of coordinate points that are pseudo ordered by magnitude.
* @param list Return: Vector of coordinate points that are pseudo ordered by magnitude.
* Pixels would be ordered by norm value, up to a precision given by max_grad/n_bins.
*/
void ll_angle(const double& threshold, const unsigned int& n_bins, std::vector<coorlist>& list);
/**
* Grow a region starting from point s with a defined precision,
* Grow a region starting from point s with a defined precision,
* returning the containing points size and the angle of the gradients.
*
* @param s Starting point for the region.
......@@ -1014,41 +1014,41 @@ private:
const double& y, const double& reg_angle, const double& prec) const;
/**
* An estimation of the angle tolerance is performed by the standard deviation of the angle at points
* near the region's starting point. Then, a new region is grown starting from the same point, but using the
* estimated angle tolerance. If this fails to produce a rectangle with the right density of region points,
* An estimation of the angle tolerance is performed by the standard deviation of the angle at points
* near the region's starting point. Then, a new region is grown starting from the same point, but using the
* estimated angle tolerance. If this fails to produce a rectangle with the right density of region points,
* 'reduce_region_radius' is called to try to satisfy this condition.
*/
*/
bool refine(std::vector<RegionPoint>& reg, int& reg_size, double reg_angle,
const double prec, double p, rect& rec, const double& density_th);
/**
* Reduce the region size, by elimination the points far from the starting point, until that leads to
* Reduce the region size, by elimination the points far from the starting point, until that leads to
* rectangle with the right density of region points or to discard the region if too small.
*/
bool reduce_region_radius(std::vector<RegionPoint>& reg, int& reg_size, double reg_angle,
const double prec, double p, rect& rec, double density, const double& density_th);
/**
/**
* Try some rectangles variations to improve NFA value. Only if the rectangle is not meaningful (i.e., log_nfa <= log_eps).
* @return The new NFA value.
*/
double rect_improve(rect& rec) const;
/**
/**
* Calculates the number of correctly aligned points within the rectangle.
* @return The new NFA value.
*/
double rect_nfa(const rect& rec) const;
/**
/**
* Computes the NFA values based on the total number of points, points that agree.
* n, k, p are the binomial parameters.
* n, k, p are the binomial parameters.
* @return The new NFA value.
*/
double nfa(const int& n, const int& k, const double& p) const;
/**
/**
* Is the point at place 'address' aligned to angle theta, up to precision 'prec'?
* @return Whether the point is aligned.
*/
......
This diff is collapsed.
......@@ -13,20 +13,20 @@ public:
LSDBase() {};
protected:
Mat test_image;
vector<Vec4i> lines;
void GenerateWhiteNoise(Mat& image);
void GenerateConstColor(Mat& image);
void GenerateLines(Mat& image, const unsigned int numLines);
void GenerateRotatedRect(Mat& image);
virtual void SetUp();
Mat test_image;
vector<Vec4i> lines;
void GenerateWhiteNoise(Mat& image);
void GenerateConstColor(Mat& image);
void GenerateLines(Mat& image, const unsigned int numLines);
void GenerateRotatedRect(Mat& image);
virtual void SetUp();
};
class LSD_ADV: public LSDBase
{
public:
LSD_ADV() {};
LSD_ADV() {};
protected:
};
......@@ -34,7 +34,7 @@ protected:
class LSD_STD: public LSDBase
{
public:
LSD_STD() {};
LSD_STD() {};
protected:
};
......@@ -42,171 +42,171 @@ protected:
class LSD_NONE: public LSDBase
{
public:
LSD_NONE() {};
LSD_NONE() {};
protected:
};
void LSDBase::GenerateWhiteNoise(Mat& image)
{
image = Mat(img_size, CV_8UC1);
RNG rng(getTickCount());
rng.fill(image, RNG::UNIFORM, 0, 256);
image = Mat(img_size, CV_8UC1);
RNG rng(getTickCount());
rng.fill(image, RNG::UNIFORM, 0, 256);
}
void LSDBase::GenerateConstColor(Mat& image)
{
RNG rng(getTickCount());
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256)));
RNG rng(getTickCount());
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 256)));
}
void LSDBase::GenerateLines(Mat& image, const unsigned int numLines)
{
RNG rng(getTickCount());
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
for(unsigned int i = 0; i < numLines; ++i)
{
int y = rng.uniform(10, img_size.width - 10);
Point p1(y, 10);
Point p2(y, img_size.height - 10);
line(image, p1, p2, Scalar(255), 1);
}
RNG rng(getTickCount());
image = Mat(img_size, CV_8UC1, Scalar::all(rng.uniform(0, 128)));
for(unsigned int i = 0; i < numLines; ++i)
{
int y = rng.uniform(10, img_size.width - 10);
Point p1(y, 10);
Point p2(y, img_size.height - 10);
line(image, p1, p2, Scalar(255), 1);
}
}
void LSDBase::GenerateRotatedRect(Mat& image)
{
RNG rng(getTickCount());
image = Mat::zeros(img_size, CV_8UC1);
Point center(rng.uniform(img_size.width/4, img_size.width*3/4),
rng.uniform(img_size.height/4, img_size.height*3/4));
Size rect_size(rng.uniform(img_size.width/8, img_size.width/6),
rng.uniform(img_size.height/8, img_size.height/6));
float angle = rng.uniform(0, 360);
Point2f vertices[4];
RotatedRect rRect = RotatedRect(center, rect_size, angle);
rRect.points(vertices);
for (int i = 0; i < 4; i++)
{
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255));
}
RNG rng(getTickCount());
image = Mat::zeros(img_size, CV_8UC1);
Point center(rng.uniform(img_size.width/4, img_size.width*3/4),
rng.uniform(img_size.height/4, img_size.height*3/4));
Size rect_size(rng.uniform(img_size.width/8, img_size.width/6),
rng.uniform(img_size.height/8, img_size.height/6));
float angle = rng.uniform(0, 360);
Point2f vertices[4];
RotatedRect rRect = RotatedRect(center, rect_size, angle);
rRect.points(vertices);
for (int i = 0; i < 4; i++)
{
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255));
}
}
void LSDBase::SetUp()
{
lines.clear();
test_image = Mat();
lines.clear();
test_image = Mat();
}
TEST_F(LSD_ADV, whiteNoise)
{
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
ASSERT_GE((unsigned int)(40), lines.size());
ASSERT_GE((unsigned int)(40), lines.size());
}
TEST_F(LSD_ADV, constColor)
{
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(0), lines.size());
ASSERT_EQ((unsigned int)(0), lines.size());
}
TEST_F(LSD_ADV, lines)
{
const unsigned int numOfLines = 3;
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
const unsigned int numOfLines = 3;
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
}
TEST_F(LSD_ADV, rotatedRect)
{
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
ASSERT_LE((unsigned int)(4), lines.size());
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_ADV);
detector.detect(test_image, lines);
ASSERT_LE((unsigned int)(4), lines.size());
}
TEST_F(LSD_STD, whiteNoise)
{
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
ASSERT_GE((unsigned int)(50), lines.size());
ASSERT_GE((unsigned int)(50), lines.size());
}
TEST_F(LSD_STD, constColor)
{
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(0), lines.size());
ASSERT_EQ((unsigned int)(0), lines.size());
}
TEST_F(LSD_STD, lines)
{
const unsigned int numOfLines = 3; //1
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
const unsigned int numOfLines = 3; //1
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
}
TEST_F(LSD_STD, rotatedRect)
{
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(8), lines.size());
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_STD);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(8), lines.size());
}
TEST_F(LSD_NONE, whiteNoise)
{
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
GenerateWhiteNoise(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
ASSERT_GE((unsigned int)(50), lines.size());
ASSERT_GE((unsigned int)(50), lines.size());
}
TEST_F(LSD_NONE, constColor)
{
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
GenerateConstColor(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(0), lines.size());
ASSERT_EQ((unsigned int)(0), lines.size());
}
TEST_F(LSD_NONE, lines)
{
const unsigned int numOfLines = 3; //1
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
const unsigned int numOfLines = 3; //1
GenerateLines(test_image, numOfLines);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
ASSERT_EQ(numOfLines * 2, lines.size()); // * 2 because of Gibbs effect
}
TEST_F(LSD_NONE, rotatedRect)
{
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(8), lines.size());
GenerateRotatedRect(test_image);
LSD detector(LSD_REFINE_NONE);
detector.detect(test_image, lines);
ASSERT_EQ((unsigned int)(8), lines.size());
}
......@@ -2,6 +2,7 @@
#include <string>
#include "opencv2/core/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
......@@ -10,41 +11,41 @@ using namespace cv;
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "lsd_lines [input image]" << std::endl;
return false;
}
std::string in = argv[1];
Mat image = imread(in, CV_LOAD_IMAGE_GRAYSCALE);
// Create and LSD detector with std refinement.
LSD lsd_std(LSD_REFINE_STD);
double start = double(getTickCount());
vector<Vec4i> lines_std;
lsd_std.detect(image, lines_std);
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "OpenCV STD (blue) - " << duration_ms << " ms." << std::endl;
// Create an LSD detector with no refinement applied.
LSD lsd_none(LSD_REFINE_NONE);
start = double(getTickCount());
vector<Vec4i> lines_none;
lsd_none.detect(image, lines_none);
duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "OpenCV NONE (red)- " << duration_ms << " ms." << std::endl;
std::cout << "Overlapping pixels are shown in purple." << std::endl;
Mat difference = Mat::zeros(image.size(), CV_8UC1);
LSD::compareSegments(image.size(), lines_std, lines_none, &difference);
imshow("Line difference", difference);
Mat drawnLines(image);
LSD::drawSegments(drawnLines, lines_std);
imshow("Standard refinement", drawnLines);
waitKey();
return 0;
if (argc != 2)
{
std::cout << "lsd_lines [input image]" << std::endl;
return false;
}
std::string in = argv[1];
Mat image = imread(in, IMREAD_GRAYSCALE);
// Create and LSD detector with std refinement.
LSD lsd_std(LSD_REFINE_STD);
double start = double(getTickCount());
vector<Vec4i> lines_std;
lsd_std.detect(image, lines_std);
double duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "OpenCV STD (blue) - " << duration_ms << " ms." << std::endl;
// Create an LSD detector with no refinement applied.
LSD lsd_none(LSD_REFINE_NONE);
start = double(getTickCount());
vector<Vec4i> lines_none;
lsd_none.detect(image, lines_none);
duration_ms = (double(getTickCount()) - start) * 1000 / getTickFrequency();
std::cout << "OpenCV NONE (red)- " << duration_ms << " ms." << std::endl;
std::cout << "Overlapping pixels are shown in purple." << std::endl;
Mat difference = Mat::zeros(image.size(), CV_8UC1);
LSD::compareSegments(image.size(), lines_std, lines_none, &difference);
imshow("Line difference", difference);
Mat drawnLines(image);
LSD::drawSegments(drawnLines, lines_std);
imshow("Standard refinement", drawnLines);
waitKey();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment