lsd.cpp 42.4 KB
Newer Older
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2 3
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 5 6 7 8 9 10 11 12
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
13
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
40
//M*/
41 42

#include "precomp.hpp"
Daniel Angelov's avatar
Daniel Angelov committed
43 44
#include <vector>

45
/////////////////////////////////////////////////////////////////////////////////////////
46
// Default LSD parameters
47
// SIGMA_SCALE 0.6    - Sigma for Gaussian filter is computed as sigma = sigma_scale/scale.
48
// QUANT       2.0    - Bound to the quantization error on the gradient norm.
49 50 51 52 53
// ANG_TH      22.5   - Gradient angle tolerance in degrees.
// LOG_EPS     0.0    - Detection threshold: -log10(NFA) > log_eps
// DENSITY_TH  0.7    - Minimal density of region points in rectangle.
// N_BINS      1024   - Number of bins in pseudo-ordering of gradient modulus.

54 55 56 57 58
#define M_3_2_PI    (3 * CV_PI) / 2   // 3/2 pi
#define M_2__PI     (2 * CV_PI)         // 2 pi

#ifndef M_LN10
#define M_LN10      2.30258509299404568402
59 60
#endif

61
#define NOTDEF      double(-1024.0) // Label for pixels with undefined gradient.
62

63 64
#define NOTUSED     0   // Label for pixels not used in yet.
#define USED        1   // Label for pixels already used in detection.
65 66 67

#define RELATIVE_ERROR_FACTOR 100.0

68
const double DEG_TO_RADS = CV_PI / 180;
69 70 71 72 73 74 75 76 77 78 79

#define log_gamma(x) ((x)>15.0?log_gamma_windschitl(x):log_gamma_lanczos(x))

struct edge
{
    cv::Point p;
    bool taken;
};

/////////////////////////////////////////////////////////////////////////////////////////

80 81
inline double distSq(const double x1, const double y1,
                     const double x2, const double y2)
82 83 84 85
{
    return (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1);
}

86 87
inline double dist(const double x1, const double y1,
                   const double x2, const double y2)
88 89 90 91 92 93 94 95
{
    return sqrt(distSq(x1, y1, x2, y2));
}

// Signed angle difference
inline double angle_diff_signed(const double& a, const double& b)
{
    double diff = a - b;
Daniel Angelov's avatar
Daniel Angelov committed
96 97
    while(diff <= -CV_PI) diff += M_2__PI;
    while(diff >   CV_PI) diff -= M_2__PI;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    return diff;
}

// Absolute value angle difference
inline double angle_diff(const double& a, const double& b)
{
    return std::fabs(angle_diff_signed(a, b));
}

// Compare doubles by relative error.
inline bool double_equal(const double& a, const double& b)
{
    // trivial case
    if(a == b) return true;

    double abs_diff = fabs(a - b);
    double aa = fabs(a);
    double bb = fabs(b);
    double abs_max = (aa > bb)? aa : bb;

    if(abs_max < DBL_MIN) abs_max = DBL_MIN;

    return (abs_diff / abs_max) <= (RELATIVE_ERROR_FACTOR * DBL_EPSILON);
}

inline bool AsmallerB_XoverY(const edge& a, const edge& b)
{
    if (a.p.x == b.p.x) return a.p.y < b.p.y;
    else return a.p.x < b.p.x;
}

129
/**
130 131 132 133 134 135 136
 *   Computes the natural logarithm of the absolute value of
 *   the gamma function of x using Windschitl method.
 *   See http://www.rskey.org/gamma.htm
 */
inline double log_gamma_windschitl(const double& x)
{
    return 0.918938533204673 + (x-0.5)*log(x) - x
137
         + 0.5*x*log(x*sinh(1/x) + 1/(810.0*pow(x, 6.0)));
138 139
}

140
/**
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 *   Computes the natural logarithm of the absolute value of
 *   the gamma function of x using the Lanczos approximation.
 *   See http://www.rskey.org/gamma.htm
 */
inline double log_gamma_lanczos(const double& x)
{
    static double q[7] = { 75122.6331530, 80916.6278952, 36308.2951477,
                         8687.24529705, 1168.92649479, 83.8676043424,
                         2.50662827511 };
    double a = (x + 0.5) * log(x + 5.5) - (x + 5.5);
    double b = 0;
    for(int n = 0; n < 7; ++n)
    {
        a -= log(x + double(n));
        b += q[n] * pow(x, double(n));
    }
    return a + log(b);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
namespace cv{

class LineSegmentDetectorImpl : public LineSegmentDetector
{
public:

/**
 * Create a LineSegmentDetectorImpl 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?
 *                      LSD_REFINE_NONE - No refinement applied.
 *                      LSD_REFINE_STD  - Standard refinement is applied. E.g. breaking arches into smaller line approximations.
 *                      LSD_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 _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.
 */
    LineSegmentDetectorImpl(int _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);

/**
188
 * Detect lines in the input image.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
 *
 * @param _image    A grayscale(CV_8UC1) input image.
 *                  If only a roi needs to be selected, use
 *                  lsd_ptr->detect(image(roi), ..., lines);
 *                  lines += Scalar(roi.x, roi.y, roi.x, roi.y);
 * @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.
 *                          Returned lines are strictly oriented depending on the gradient.
 * @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%.
 *                          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
 *                          This vector will be calculated _only_ when the objects type is REFINE_ADV
 */
206
    void detect(InputArray _image, OutputArray _lines,
207 208 209 210 211 212 213 214 215 216
                OutputArray width = noArray(), OutputArray prec = noArray(),
                OutputArray nfa = noArray());

/**
 * Draw lines on the given canvas.
 *
 * @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
 */
217
    void drawSegments(InputOutputArray _image, InputArray lines);
218 219 220 221

/**
 * Draw both vectors on the image canvas. Uses blue for lines 1 and red for lines 2.
 *
222
 * @param size      The size of the image, where lines1 and lines2 were found.
223 224
 * @param lines1    The first lines that need to be drawn. Color - Blue.
 * @param lines2    The second lines that need to be drawn. Color - Red.
225 226
 * @param image     An optional image, where lines will be drawn.
 *                  Should have the size of the image, where the lines were found
227 228
 * @return          The number of mismatching pixels between lines1 and lines2.
 */
229
    int compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image = noArray());
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

private:
    Mat image;
    Mat_<double> scaled_image;
    double *scaled_image_data;
    Mat_<double> angles;     // in rads
    double *angles_data;
    Mat_<double> modgrad;
    double *modgrad_data;
    Mat_<uchar> used;

    int img_width;
    int img_height;
    double LOG_NT;

    bool w_needed;
    bool p_needed;
    bool n_needed;

    const double SCALE;
    const int doRefine;
    const double SIGMA_SCALE;
    const double QUANT;
    const double ANG_TH;
    const double LOG_EPS;
    const double DENSITY_TH;
    const int N_BINS;

    struct RegionPoint {
        int x;
        int y;
        uchar* used;
        double angle;
        double modgrad;
    };


    struct coorlist
    {
        Point2i p;
        struct coorlist* next;
    };

    struct rect
    {
        double x1, y1, x2, y2;    // first and second point of the line segment
        double width;             // rectangle width
        double x, y;              // center of the rectangle
        double theta;             // angle
        double dx,dy;             // (dx,dy) is vector oriented as the line segment
        double prec;              // tolerance angle
        double p;                 // probability of a point with angle within 'prec'
    };

284 285
    LineSegmentDetectorImpl& operator= (const LineSegmentDetectorImpl&); // to quiet MSVC

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/**
 * 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.
 *                              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%.
 *                              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<Vec4i>& lines,
              std::vector<double>& widths, std::vector<double>& precisions,
              std::vector<double>& nfas);

/**
 * Finds the angles and the gradients of the image. Generates a list of pseudo ordered points.
 *
 * @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.
 *                  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,
 * returning the containing points size and the angle of the gradients.
 *
 * @param s         Starting point for the region.
 * @param reg       Return: Vector of points, that are part of the region
 * @param reg_size  Return: The size of the region.
 * @param reg_angle Return: The mean angle of the region.
 * @param prec      The precision by which each region angle should be aligned to the mean.
 */
    void region_grow(const Point2i& s, std::vector<RegionPoint>& reg,
                     int& reg_size, double& reg_angle, const double& prec);

/**
 * Finds the bounding rotated rectangle of a region.
 *
 * @param reg       The region of points, from which the rectangle to be constructed from.
 * @param reg_size  The number of points in the region.
 * @param reg_angle The mean angle of the region.
 * @param prec      The precision by which points were found.
 * @param p         Probability of a point with angle within 'prec'.
 * @param rec       Return: The generated rectangle.
 */
    void region2rect(const std::vector<RegionPoint>& reg, const int reg_size, const double reg_angle,
338
                     const double prec, const double p, rect& rec) const;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

/**
 * Compute region's angle as the principal inertia axis of the region.
 * @return          Regions angle.
 */
    double get_theta(const std::vector<RegionPoint>& reg, const int& reg_size, const double& x,
                     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,
 * '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
 * 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.
 * @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.
 */
    bool isAligned(const int& address, const double& theta, const double& prec) const;
};

/////////////////////////////////////////////////////////////////////////////////////////

391
CV_EXPORTS Ptr<LineSegmentDetector> createLineSegmentDetector(
392 393 394
        int _refine, double _scale, double _sigma_scale, double _quant, double _ang_th,
        double _log_eps, double _density_th, int _n_bins)
{
395
    return makePtr<LineSegmentDetectorImpl>(
396
            _refine, _scale, _sigma_scale, _quant, _ang_th,
397
            _log_eps, _density_th, _n_bins);
398 399 400 401 402
}

/////////////////////////////////////////////////////////////////////////////////////////

LineSegmentDetectorImpl::LineSegmentDetectorImpl(int _refine, double _scale, double _sigma_scale, double _quant,
403
        double _ang_th, double _log_eps, double _density_th, int _n_bins)
404 405 406 407 408 409 410 411
        :SCALE(_scale), doRefine(_refine), SIGMA_SCALE(_sigma_scale), QUANT(_quant),
        ANG_TH(_ang_th), LOG_EPS(_log_eps), DENSITY_TH(_density_th), N_BINS(_n_bins)
{
    CV_Assert(_scale > 0 && _sigma_scale > 0 && _quant >= 0 &&
              _ang_th > 0 && _ang_th < 180 && _density_th >= 0 && _density_th < 1 &&
              _n_bins > 0);
}

412
void LineSegmentDetectorImpl::detect(InputArray _image, OutputArray _lines,
413
                OutputArray _width, OutputArray _prec, OutputArray _nfa)
414 415 416 417
{
    Mat_<double> img = _image.getMat();
    CV_Assert(!img.empty() && img.channels() == 1);

418 419
    // Convert image to double
    img.convertTo(image, CV_64FC1);
420 421

    std::vector<Vec4i> lines;
422 423 424 425 426 427 428
    std::vector<double> w, p, n;
    w_needed = _width.needed();
    p_needed = _prec.needed();
    n_needed = _nfa.needed();

    CV_Assert((!_nfa.needed()) ||                              // NFA InputArray will be filled _only_ when
              (_nfa.needed() && doRefine >= LSD_REFINE_ADV));  // REFINE_ADV type LineSegmentDetectorImpl object is created.
429 430 431 432

    flsd(lines, w, p, n);

    Mat(lines).copyTo(_lines);
433 434 435
    if(w_needed) Mat(w).copyTo(_width);
    if(p_needed) Mat(p).copyTo(_prec);
    if(n_needed) Mat(n).copyTo(_nfa);
436 437
}

438 439 440
void LineSegmentDetectorImpl::flsd(std::vector<Vec4i>& lines,
    std::vector<double>& widths, std::vector<double>& precisions,
    std::vector<double>& nfas)
441 442
{
    // Angle tolerance
Daniel Angelov's avatar
Daniel Angelov committed
443
    const double prec = CV_PI * ANG_TH / 180;
444 445
    const double p = ANG_TH / 180;
    const double rho = QUANT / sin(prec);    // gradient magnitude threshold
446

447
    std::vector<coorlist> list;
Daniel Angelov's avatar
Daniel Angelov committed
448
    if(SCALE != 1)
449 450 451 452
    {
        Mat gaussian_img;
        const double sigma = (SCALE < 1)?(SIGMA_SCALE / SCALE):(SIGMA_SCALE);
        const double sprec = 3;
453
        const unsigned int h =  (unsigned int)(ceil(sigma * sqrt(2 * sprec * log(10.0))));
454
        Size ksize(1 + 2 * h, 1 + 2 * h); // kernel size
455 456 457 458 459 460 461 462 463 464 465
        GaussianBlur(image, gaussian_img, ksize, sigma);
        // Scale image to needed size
        resize(gaussian_img, scaled_image, Size(), SCALE, SCALE);
        ll_angle(rho, N_BINS, list);
    }
    else
    {
        scaled_image = image;
        ll_angle(rho, N_BINS, list);
    }

466
    LOG_NT = 5 * (log10(double(img_width)) + log10(double(img_height))) / 2 + log10(11.0);
467 468
    const int min_reg_size = int(-LOG_NT/log10(p)); // minimal number of points in region that can give a meaningful event

469 470 471 472
    // // Initialize region only when needed
    // Mat region = Mat::zeros(scaled_image.size(), CV_8UC1);
    used = Mat_<uchar>::zeros(scaled_image.size()); // zeros = NOTUSED
    std::vector<RegionPoint> reg(img_width * img_height);
473 474

    // Search for line segments
475
    unsigned int ls_count = 0;
Ilya Lavrenov's avatar
Ilya Lavrenov committed
476
    for(size_t i = 0, list_size = list.size(); i < list_size; ++i)
477 478 479 480 481 482 483
    {
        unsigned int adx = list[i].p.x + list[i].p.y * img_width;
        if((used.data[adx] == NOTUSED) && (angles_data[adx] != NOTDEF))
        {
            int reg_size;
            double reg_angle;
            region_grow(list[i].p, reg, reg_size, reg_angle, prec);
484

485 486
            // Ignore small regions
            if(reg_size < min_reg_size) { continue; }
487

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
            // Construct rectangular approximation for the region
            rect rec;
            region2rect(reg, reg_size, reg_angle, prec, p, rec);

            double log_nfa = -1;
            if(doRefine > LSD_REFINE_NONE)
            {
                // At least REFINE_STANDARD lvl.
                if(!refine(reg, reg_size, reg_angle, prec, p, rec, DENSITY_TH)) { continue; }

                if(doRefine >= LSD_REFINE_ADV)
                {
                    // Compute NFA
                    log_nfa = rect_improve(rec);
                    if(log_nfa <= LOG_EPS) { continue; }
                }
            }
            // Found new line
            ++ls_count;

            // Add the offset
            rec.x1 += 0.5; rec.y1 += 0.5;
            rec.x2 += 0.5; rec.y2 += 0.5;

            // scale the result values if a sub-sampling was performed
            if(SCALE != 1)
            {
                rec.x1 /= SCALE; rec.y1 /= SCALE;
                rec.x2 /= SCALE; rec.y2 /= SCALE;
                rec.width /= SCALE;
            }
519

520
            //Store the relevant data
521
            lines.push_back(Vec4i(int(rec.x1), int(rec.y1), int(rec.x2), int(rec.y2)));
522 523 524 525
            if(w_needed) widths.push_back(rec.width);
            if(p_needed) precisions.push_back(rec.p);
            if(n_needed && doRefine >= LSD_REFINE_ADV) nfas.push_back(log_nfa);

526 527 528 529 530 531 532 533 534 535

            // //Add the linesID to the region on the image
            // for(unsigned int el = 0; el < reg_size; el++)
            // {
            //     region.data[reg[i].x + reg[i].y * width] = ls_count;
            // }
        }
    }
}

536 537 538
void LineSegmentDetectorImpl::ll_angle(const double& threshold,
                                   const unsigned int& n_bins,
                                   std::vector<coorlist>& list)
539 540
{
    //Initialize data
541 542
    angles = Mat_<double>(scaled_image.size());
    modgrad = Mat_<double>(scaled_image.size());
543

544 545 546 547
    angles_data = angles.ptr<double>(0);
    modgrad_data = modgrad.ptr<double>(0);
    scaled_image_data = scaled_image.ptr<double>(0);

548
    img_width = scaled_image.cols;
549 550
    img_height = scaled_image.rows;

551
    // Undefined the down and right boundaries
552 553
    angles.row(img_height - 1).setTo(NOTDEF);
    angles.col(img_width - 1).setTo(NOTDEF);
554

555
    // Computing gradient for remaining pixels
556 557
    CV_Assert(scaled_image.isContinuous() &&
              modgrad.isContinuous() &&
558 559 560 561 562 563 564 565 566
              angles.isContinuous());   // Accessing image data linearly

    double max_grad = -1;
    for(int y = 0; y < img_height - 1; ++y)
    {
        for(int addr = y * img_width, addr_end = addr + img_width - 1; addr < addr_end; ++addr)
        {
            double DA = scaled_image_data[addr + img_width + 1] - scaled_image_data[addr];
            double BC = scaled_image_data[addr + 1] - scaled_image_data[addr + img_width];
567 568 569 570
            double gx = DA + BC;    // gradient x component
            double gy = DA - BC;    // gradient y component
            double norm = std::sqrt((gx * gx + gy * gy) / 4); // gradient norm

571 572
            modgrad_data[addr] = norm;    // store gradient

573
            if (norm <= threshold)  // norm too small, gradient no defined
574 575 576 577 578
            {
                angles_data[addr] = NOTDEF;
            }
            else
            {
579
                angles_data[addr] = fastAtan2(float(gx), float(-gy)) * DEG_TO_RADS;  // gradient angle computation
580 581 582 583 584
                if (norm > max_grad) { max_grad = norm; }
            }

        }
    }
585

586 587 588 589 590 591 592 593 594 595 596 597
    // Compute histogram of gradient values
    list = std::vector<coorlist>(img_width * img_height);
    std::vector<coorlist*> range_s(n_bins);
    std::vector<coorlist*> range_e(n_bins);
    unsigned int count = 0;
    double bin_coef = (max_grad > 0) ? double(n_bins - 1) / max_grad : 0; // If all image is smooth, max_grad <= 0

    for(int y = 0; y < img_height - 1; ++y)
    {
        const double* norm = modgrad_data + y * img_width;
        for(int x = 0; x < img_width - 1; ++x, ++norm)
        {
598
            // Store the point in the right bin according to its norm
599 600 601 602 603 604 605 606 607 608 609 610
            int i = int((*norm) * bin_coef);
            if(!range_e[i])
            {
                range_e[i] = range_s[i] = &list[count];
                ++count;
            }
            else
            {
                range_e[i]->next = &list[count];
                range_e[i] = &list[count];
                ++count;
            }
611
            range_e[i]->p = Point(x, y);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
            range_e[i]->next = 0;
        }
    }

    // Sort
    int idx = n_bins - 1;
    for(;idx > 0 && !range_s[idx]; --idx);
    coorlist* start = range_s[idx];
    coorlist* end = range_e[idx];
    if(start)
    {
        while(idx > 0)
        {
            --idx;
            if(range_s[idx])
            {
                end->next = range_s[idx];
                end = range_e[idx];
            }
        }
    }
}

635 636
void LineSegmentDetectorImpl::region_grow(const Point2i& s, std::vector<RegionPoint>& reg,
                                      int& reg_size, double& reg_angle, const double& prec)
637 638 639 640 641 642 643 644 645 646 647
{
    // Point to this region
    reg_size = 1;
    reg[0].x = s.x;
    reg[0].y = s.y;
    int addr = s.x + s.y * img_width;
    reg[0].used = used.data + addr;
    reg_angle = angles_data[addr];
    reg[0].angle = reg_angle;
    reg[0].modgrad = modgrad_data[addr];

648 649
    float sumdx = float(std::cos(reg_angle));
    float sumdy = float(std::sin(reg_angle));
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    *reg[0].used = USED;

    //Try neighboring regions
    for(int i = 0; i < reg_size; ++i)
    {
        const RegionPoint& rpoint = reg[i];
        int xx_min = std::max(rpoint.x - 1, 0), xx_max = std::min(rpoint.x + 1, img_width - 1);
        int yy_min = std::max(rpoint.y - 1, 0), yy_max = std::min(rpoint.y + 1, img_height - 1);
        for(int yy = yy_min; yy <= yy_max; ++yy)
        {
            int c_addr = xx_min + yy * img_width;
            for(int xx = xx_min; xx <= xx_max; ++xx, ++c_addr)
            {
                if((used.data[c_addr] != USED) &&
                   (isAligned(c_addr, reg_angle, prec)))
                {
                    // Add point
                    used.data[c_addr] = USED;
                    RegionPoint& region_point = reg[reg_size];
                    region_point.x = xx;
                    region_point.y = yy;
                    region_point.used = &(used.data[c_addr]);
                    region_point.modgrad = modgrad_data[c_addr];
                    const double& angle = angles_data[c_addr];
                    region_point.angle = angle;
                    ++reg_size;

                    // Update region's angle
                    sumdx += cos(float(angle));
                    sumdy += sin(float(angle));
                    // reg_angle is used in the isAligned, so it needs to be updates?
681
                    reg_angle = fastAtan2(sumdy, sumdx) * DEG_TO_RADS;
682 683 684 685 686 687
                }
            }
        }
    }
}

688 689
void LineSegmentDetectorImpl::region2rect(const std::vector<RegionPoint>& reg, const int reg_size,
                                      const double reg_angle, const double prec, const double p, rect& rec) const
690 691 692 693 694 695 696 697 698 699 700 701 702
{
    double x = 0, y = 0, sum = 0;
    for(int i = 0; i < reg_size; ++i)
    {
        const RegionPoint& pnt = reg[i];
        const double& weight = pnt.modgrad;
        x += double(pnt.x) * weight;
        y += double(pnt.y) * weight;
        sum += weight;
    }

    // Weighted sum must differ from 0
    CV_Assert(sum > 0);
703

704 705 706 707 708 709 710 711 712 713 714 715 716 717
    x /= sum;
    y /= sum;

    double theta = get_theta(reg, reg_size, x, y, reg_angle, prec);

    // Find length and width
    double dx = cos(theta);
    double dy = sin(theta);
    double l_min = 0, l_max = 0, w_min = 0, w_max = 0;

    for(int i = 0; i < reg_size; ++i)
    {
        double regdx = double(reg[i].x) - x;
        double regdy = double(reg[i].y) - y;
718

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
        double l = regdx * dx + regdy * dy;
        double w = -regdx * dy + regdy * dx;

        if(l > l_max) l_max = l;
        else if(l < l_min) l_min = l;
        if(w > w_max) w_max = w;
        else if(w < w_min) w_min = w;
    }

    // Store values
    rec.x1 = x + l_min * dx;
    rec.y1 = y + l_min * dy;
    rec.x2 = x + l_max * dx;
    rec.y2 = y + l_max * dy;
    rec.width = w_max - w_min;
    rec.x = x;
    rec.y = y;
    rec.theta = theta;
    rec.dx = dx;
    rec.dy = dy;
    rec.prec = prec;
    rec.p = p;

    // Min width of 1 pixel
    if(rec.width < 1.0) rec.width = 1.0;
}

746 747
double LineSegmentDetectorImpl::get_theta(const std::vector<RegionPoint>& reg, const int& reg_size, const double& x,
                                      const double& y, const double& reg_angle, const double& prec) const
748 749 750 751 752
{
    double Ixx = 0.0;
    double Iyy = 0.0;
    double Ixy = 0.0;

753
    // Compute inertia matrix
754 755
    for(int i = 0; i < reg_size; ++i)
    {
756
        const double& regx = reg[i].x;
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
        const double& regy = reg[i].y;
        const double& weight = reg[i].modgrad;
        double dx = regx - x;
        double dy = regy - y;
        Ixx += dy * dy * weight;
        Iyy += dx * dx * weight;
        Ixy -= dx * dy * weight;
    }

    // Check if inertia matrix is null
    CV_Assert(!(double_equal(Ixx, 0) && double_equal(Iyy, 0) && double_equal(Ixy, 0)));

    // Compute smallest eigenvalue
    double lambda = 0.5 * (Ixx + Iyy - sqrt((Ixx - Iyy) * (Ixx - Iyy) + 4.0 * Ixy * Ixy));

    // Compute angle
    double theta = (fabs(Ixx)>fabs(Iyy))?
774 775
                    double(fastAtan2(float(lambda - Ixx), float(Ixy))):
                    double(fastAtan2(float(Ixy), float(lambda - Iyy))); // in degs
776 777
    theta *= DEG_TO_RADS;

778
    // Correct angle by 180 deg if necessary
Daniel Angelov's avatar
Daniel Angelov committed
779
    if(angle_diff(theta, reg_angle) > prec) { theta += CV_PI; }
780 781 782 783

    return theta;
}

784 785
bool LineSegmentDetectorImpl::refine(std::vector<RegionPoint>& reg, int& reg_size, double reg_angle,
                                 const double prec, double p, rect& rec, const double& density_th)
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
{
    double density = double(reg_size) / (dist(rec.x1, rec.y1, rec.x2, rec.y2) * rec.width);

    if (density >= density_th) { return true; }

    // Try to reduce angle tolerance
    double xc = double(reg[0].x);
    double yc = double(reg[0].y);
    const double& ang_c = reg[0].angle;
    double sum = 0, s_sum = 0;
    int n = 0;

    for (int i = 0; i < reg_size; ++i)
    {
        *(reg[i].used) = NOTUSED;
        if (dist(xc, yc, reg[i].x, reg[i].y) < rec.width)
        {
            const double& angle = reg[i].angle;
            double ang_d = angle_diff_signed(angle, ang_c);
            sum += ang_d;
            s_sum += ang_d * ang_d;
            ++n;
        }
    }
    double mean_angle = sum / double(n);
    // 2 * standard deviation
812
    double tau = 2.0 * sqrt((s_sum - 2.0 * mean_angle * sum) / double(n) + mean_angle * mean_angle);
813 814 815 816 817 818 819 820 821

    // Try new region
    region_grow(Point(reg[0].x, reg[0].y), reg, reg_size, reg_angle, tau);

    if (reg_size < 2) { return false; }

    region2rect(reg, reg_size, reg_angle, prec, p, rec);
    density = double(reg_size) / (dist(rec.x1, rec.y1, rec.x2, rec.y2) * rec.width);

822 823
    if (density < density_th)
    {
824 825 826 827 828 829 830 831
        return reduce_region_radius(reg, reg_size, reg_angle, prec, p, rec, density, density_th);
    }
    else
    {
        return true;
    }
}

832
bool LineSegmentDetectorImpl::reduce_region_radius(std::vector<RegionPoint>& reg, int& reg_size, double reg_angle,
833 834 835 836 837 838 839 840 841 842 843 844
                const double prec, double p, rect& rec, double density, const double& density_th)
{
    // Compute region's radius
    double xc = double(reg[0].x);
    double yc = double(reg[0].y);
    double radSq1 = distSq(xc, yc, rec.x1, rec.y1);
    double radSq2 = distSq(xc, yc, rec.x2, rec.y2);
    double radSq = radSq1 > radSq2 ? radSq1 : radSq2;

    while(density < density_th)
    {
        radSq *= 0.75*0.75; // Reduce region's radius to 75% of its value
845
        // Remove points from the region and update 'used' map
846 847 848 849
        for(int i = 0; i < reg_size; ++i)
        {
            if(distSq(xc, yc, double(reg[i].x), double(reg[i].y)) > radSq)
            {
850
                // Remove point from the region
851 852 853
                *(reg[i].used) = NOTUSED;
                std::swap(reg[i], reg[reg_size - 1]);
                --reg_size;
854
                --i; // To avoid skipping one point
855 856 857 858 859
            }
        }

        if(reg_size < 2) { return false; }

860
        // Re-compute rectangle
861 862 863
        region2rect(reg, reg_size ,reg_angle, prec, p, rec);

        // Re-compute region points density
864 865
        density = double(reg_size) /
                  (dist(rec.x1, rec.y1, rec.x2, rec.y2) * rec.width);
866 867 868 869 870
    }

    return true;
}

871
double LineSegmentDetectorImpl::rect_improve(rect& rec) const
872 873 874 875 876 877 878 879 880 881 882 883 884 885
{
    double delta = 0.5;
    double delta_2 = delta / 2.0;

    double log_nfa = rect_nfa(rec);

    if(log_nfa > LOG_EPS) return log_nfa; // Good rectangle

    // Try to improve
    // Finer precision
    rect r = rect(rec); // Copy
    for(int n = 0; n < 5; ++n)
    {
        r.p /= 2;
Daniel Angelov's avatar
Daniel Angelov committed
886
        r.prec = r.p * CV_PI;
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
        double log_nfa_new = rect_nfa(r);
        if(log_nfa_new > log_nfa)
        {
            log_nfa = log_nfa_new;
            rec = rect(r);
        }
    }
    if(log_nfa > LOG_EPS) return log_nfa;

    // Try to reduce width
    r = rect(rec);
    for(unsigned int n = 0; n < 5; ++n)
    {
        if((r.width - delta) >= 0.5)
        {
            r.width -= delta;
            double log_nfa_new = rect_nfa(r);
            if(log_nfa_new > log_nfa)
            {
                rec = rect(r);
                log_nfa = log_nfa_new;
            }
        }
    }
    if(log_nfa > LOG_EPS) return log_nfa;
912

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
    // Try to reduce one side of rectangle
    r = rect(rec);
    for(unsigned int n = 0; n < 5; ++n)
    {
        if((r.width - delta) >= 0.5)
        {
            r.x1 += -r.dy * delta_2;
            r.y1 +=  r.dx * delta_2;
            r.x2 += -r.dy * delta_2;
            r.y2 +=  r.dx * delta_2;
            r.width -= delta;
            double log_nfa_new = rect_nfa(r);
            if(log_nfa_new > log_nfa)
            {
                rec = rect(r);
                log_nfa = log_nfa_new;
            }
        }
    }
    if(log_nfa > LOG_EPS) return log_nfa;

    // Try to reduce other side of rectangle
    r = rect(rec);
    for(unsigned int n = 0; n < 5; ++n)
    {
        if((r.width - delta) >= 0.5)
        {
            r.x1 -= -r.dy * delta_2;
            r.y1 -=  r.dx * delta_2;
            r.x2 -= -r.dy * delta_2;
            r.y2 -=  r.dx * delta_2;
            r.width -= delta;
            double log_nfa_new = rect_nfa(r);
            if(log_nfa_new > log_nfa)
            {
                rec = rect(r);
                log_nfa = log_nfa_new;
            }
        }
    }
    if(log_nfa > LOG_EPS) return log_nfa;

    // Try finer precision
    r = rect(rec);
    for(unsigned int n = 0; n < 5; ++n)
    {
        if((r.width - delta) >= 0.5)
        {
            r.p /= 2;
Daniel Angelov's avatar
Daniel Angelov committed
962
            r.prec = r.p * CV_PI;
963 964 965 966 967 968 969 970 971 972 973 974
            double log_nfa_new = rect_nfa(r);
            if(log_nfa_new > log_nfa)
            {
                rec = rect(r);
                log_nfa = log_nfa_new;
            }
        }
    }

    return log_nfa;
}

975
double LineSegmentDetectorImpl::rect_nfa(const rect& rec) const
976 977 978 979 980 981 982 983 984 985
{
    int total_pts = 0, alg_pts = 0;
    double half_width = rec.width / 2.0;
    double dyhw = rec.dy * half_width;
    double dxhw = rec.dx * half_width;

    std::vector<edge> ordered_x(4);
    edge* min_y = &ordered_x[0];
    edge* max_y = &ordered_x[0]; // Will be used for loop range

986 987 988 989
    ordered_x[0].p.x = int(rec.x1 - dyhw); ordered_x[0].p.y = int(rec.y1 + dxhw); ordered_x[0].taken = false;
    ordered_x[1].p.x = int(rec.x2 - dyhw); ordered_x[1].p.y = int(rec.y2 + dxhw); ordered_x[1].taken = false;
    ordered_x[2].p.x = int(rec.x2 + dyhw); ordered_x[2].p.y = int(rec.y2 - dxhw); ordered_x[2].taken = false;
    ordered_x[3].p.x = int(rec.x1 + dyhw); ordered_x[3].p.y = int(rec.y1 - dxhw); ordered_x[3].taken = false;
990

991
    std::sort(ordered_x.begin(), ordered_x.end(), AsmallerB_XoverY);
992

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    // Find min y. And mark as taken. find max y.
    for(unsigned int i = 1; i < 4; ++i)
    {
        if(min_y->p.y > ordered_x[i].p.y) {min_y = &ordered_x[i]; }
        if(max_y->p.y < ordered_x[i].p.y) {max_y = &ordered_x[i]; }
    }
    min_y->taken = true;

    // Find leftmost untaken point;
    edge* leftmost = 0;
    for(unsigned int i = 0; i < 4; ++i)
    {
        if(!ordered_x[i].taken)
        {
1007
            if(!leftmost) // if uninitialized
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
            {
                leftmost = &ordered_x[i];
            }
            else if (leftmost->p.x > ordered_x[i].p.x)
            {
                leftmost = &ordered_x[i];
            }
        }
    }
    leftmost->taken = true;

    // Find rightmost untaken point;
    edge* rightmost = 0;
    for(unsigned int i = 0; i < 4; ++i)
    {
        if(!ordered_x[i].taken)
        {
1025
            if(!rightmost) // if uninitialized
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
            {
                rightmost = &ordered_x[i];
            }
            else if (rightmost->p.x < ordered_x[i].p.x)
            {
                rightmost = &ordered_x[i];
            }
        }
    }
    rightmost->taken = true;

    // Find last untaken point;
    edge* tailp = 0;
    for(unsigned int i = 0; i < 4; ++i)
    {
        if(!ordered_x[i].taken)
        {
1043
            if(!tailp) // if uninitialized
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
            {
                tailp = &ordered_x[i];
            }
            else if (tailp->p.x > ordered_x[i].p.x)
            {
                tailp = &ordered_x[i];
            }
        }
    }
    tailp->taken = true;

1055
    double flstep = (min_y->p.y != leftmost->p.y) ?
1056
                    (min_y->p.x - leftmost->p.x) / (min_y->p.y - leftmost->p.y) : 0; //first left step
1057
    double slstep = (leftmost->p.y != tailp->p.x) ?
1058
                    (leftmost->p.x - tailp->p.x) / (leftmost->p.y - tailp->p.x) : 0; //second left step
1059 1060

    double frstep = (min_y->p.y != rightmost->p.y) ?
1061
                    (min_y->p.x - rightmost->p.x) / (min_y->p.y - rightmost->p.y) : 0; //first right step
1062
    double srstep = (rightmost->p.y != tailp->p.x) ?
1063
                    (rightmost->p.x - tailp->p.x) / (rightmost->p.y - tailp->p.x) : 0; //second right step
1064

1065 1066
    double lstep = flstep, rstep = frstep;

1067
    double left_x = min_y->p.x, right_x = min_y->p.x;
1068

1069 1070 1071 1072 1073
    // Loop around all points in the region and count those that are aligned.
    int min_iter = std::max(min_y->p.y, 0);
    int max_iter = std::min(max_y->p.y, img_height - 1);
    for(int y = min_iter; y <= max_iter; ++y)
    {
1074 1075
        int adx = y * img_width + int(left_x);
        for(int x = int(left_x); x <= int(right_x); ++x, ++adx)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
        {
            ++total_pts;
            if(isAligned(adx, rec.theta, rec.prec))
            {
                ++alg_pts;
            }
        }

        if(y >= leftmost->p.y) { lstep = slstep; }
        if(y >= rightmost->p.y) { rstep = srstep; }

        left_x += lstep;
        right_x += rstep;
    }

    return nfa(total_pts, alg_pts, rec.p);
}

1094
double LineSegmentDetectorImpl::nfa(const int& n, const int& k, const double& p) const
1095 1096
{
    // Trivial cases
1097
    if(n == 0 || k == 0) { return -LOG_NT; }
1098 1099 1100 1101 1102 1103 1104 1105 1106
    if(n == k) { return -LOG_NT - double(n) * log10(p); }

    double p_term = p / (1 - p);

    double log1term = (double(n) + 1) - log_gamma(double(k) + 1)
                - log_gamma(double(n-k) + 1)
                + double(k) * log(p) + double(n-k) * log(1.0 - p);
    double term = exp(log1term);

1107
    if(double_equal(term, 0))
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    {
        if(k > n * p) return -log1term / M_LN10 - LOG_NT;
        else return -LOG_NT;
    }

    // Compute more terms if needed
    double bin_tail = term;
    double tolerance = 0.1; // an error of 10% in the result is accepted
    for(int i = k + 1; i <= n; ++i)
    {
        double bin_term = double(n - i + 1) / double(i);
        double mult_term = bin_term * p_term;
        term *= mult_term;
        bin_tail += term;
        if(bin_term < 1)
        {
            double err = term * ((1 - pow(mult_term, double(n-i+1))) / (1 - mult_term) - 1);
            if(err < tolerance * fabs(-log10(bin_tail) - LOG_NT) * bin_tail) break;
        }

    }
    return -log10(bin_tail) - LOG_NT;
}

1132
inline bool LineSegmentDetectorImpl::isAligned(const int& address, const double& theta, const double& prec) const
1133 1134 1135 1136 1137
{
    if(address < 0) { return false; }
    const double& a = angles_data[address];
    if(a == NOTDEF) { return false; }

1138
    // It is assumed that 'theta' and 'a' are in the range [-pi,pi]
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    double n_theta = theta - a;
    if(n_theta < 0) { n_theta = -n_theta; }
    if(n_theta > M_3_2_PI)
    {
        n_theta -= M_2__PI;
        if(n_theta < 0) n_theta = -n_theta;
    }

    return n_theta <= prec;
}


1151
void LineSegmentDetectorImpl::drawSegments(InputOutputArray _image, InputArray lines)
1152
{
1153
    CV_Assert(!_image.empty() && (_image.channels() == 1 || _image.channels() == 3));
1154 1155

    Mat gray;
1156
    if (_image.channels() == 1)
1157
    {
1158
        gray = _image.getMatRef();
1159
    }
1160
    else if (_image.channels() == 3)
1161
    {
1162
        cvtColor(_image, gray, CV_BGR2GRAY);
1163
    }
1164

1165 1166 1167 1168 1169 1170
    // Create a 3 channel image in order to draw colored lines
    std::vector<Mat> planes;
    planes.push_back(gray);
    planes.push_back(gray);
    planes.push_back(gray);

1171 1172 1173 1174
    merge(planes, _image);

    Mat _lines;
    _lines = lines.getMat();
1175 1176

    // Draw segments
1177
    for(int i = 0; i < _lines.size().width; ++i)
1178
    {
1179 1180 1181 1182
        const Vec4i& v = _lines.at<Vec4i>(i);
        Point b(v[0], v[1]);
        Point e(v[2], v[3]);
        line(_image.getMatRef(), b, e, Scalar(0, 0, 255), 1);
1183 1184 1185
    }
}

1186

1187
int LineSegmentDetectorImpl::compareSegments(const Size& size, InputArray lines1, InputArray lines2, InputOutputArray _image)
1188
{
1189
    Size sz = size;
1190
    if (_image.needed() && _image.size() != size) sz = _image.size();
1191
    CV_Assert(sz.area());
1192

1193 1194
    Mat_<uchar> I1 = Mat_<uchar>::zeros(sz);
    Mat_<uchar> I2 = Mat_<uchar>::zeros(sz);
1195

1196 1197 1198 1199
    Mat _lines1;
    Mat _lines2;
    _lines1 = lines1.getMat();
    _lines2 = lines2.getMat();
1200
    // Draw segments
1201 1202
    std::vector<Mat> _lines;
    for(int i = 0; i < _lines1.size().width; ++i)
1203
    {
1204 1205
        Point b(_lines1.at<Vec4i>(i)[0], _lines1.at<Vec4i>(i)[1]);
        Point e(_lines1.at<Vec4i>(i)[2], _lines1.at<Vec4i>(i)[3]);
1206 1207
        line(I1, b, e, Scalar::all(255), 1);
    }
1208
    for(int i = 0; i < _lines2.size().width; ++i)
1209
    {
1210 1211
        Point b(_lines2.at<Vec4i>(i)[0], _lines2.at<Vec4i>(i)[1]);
        Point e(_lines2.at<Vec4i>(i)[2], _lines2.at<Vec4i>(i)[3]);
1212 1213 1214 1215 1216 1217 1218 1219
        line(I2, b, e, Scalar::all(255), 1);
    }

    // Count the pixels that don't agree
    Mat Ixor;
    bitwise_xor(I1, I2, Ixor);
    int N = countNonZero(Ixor);

1220
    if (_image.needed())
1221
    {
1222 1223 1224
        CV_Assert(_image.channels() == 3);
        Mat img = _image.getMatRef();
        CV_Assert(img.isContinuous() && I1.isContinuous() && I2.isContinuous());
1225

1226 1227 1228 1229 1230 1231
        for (unsigned int i = 0; i < I1.total(); ++i)
        {
            uchar i1 = I1.data[i];
            uchar i2 = I2.data[i];
            if (i1 || i2)
            {
1232 1233 1234 1235 1236 1237
                unsigned int base_idx = i * 3;
                if (i1) img.data[base_idx] = 255;
                else img.data[base_idx] = 0;
                img.data[base_idx + 1] = 0;
                if (i2) img.data[base_idx + 2] = 255;
                else img.data[base_idx + 2] = 0;
1238 1239 1240 1241 1242 1243
            }
        }
    }

    return N;
}
1244 1245

} // namespace cv