2015-09-01 05:59:08 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
2010-05-12 01:44:00 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//
|
2013-02-14 19:36:20 +08:00
|
|
|
// License Agreement
|
2010-05-12 01:44:00 +08:00
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000, Intel Corporation, all rights reserved.
|
2013-02-14 19:36:20 +08:00
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
2014-04-18 12:55:38 +08:00
|
|
|
// Copyright (C) 2014, Itseez, Inc, all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's 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.
|
|
|
|
//
|
2013-02-14 19:36:20 +08:00
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2014-08-22 20:50:01 +08:00
|
|
|
#include "opencl_kernels_imgproc.hpp"
|
2017-11-21 19:18:47 +08:00
|
|
|
#include "opencv2/core/hal/intrin.hpp"
|
2017-12-28 22:23:11 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Classical Hough Transform
|
|
|
|
struct LinePolar
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
float rho;
|
|
|
|
float angle;
|
2013-02-13 03:16:06 +08:00
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
struct hough_cmp_gt
|
|
|
|
{
|
|
|
|
hough_cmp_gt(const int* _aux) : aux(_aux) {}
|
2017-11-21 19:18:47 +08:00
|
|
|
inline bool operator()(int l1, int l2) const
|
2013-03-28 20:12:13 +08:00
|
|
|
{
|
|
|
|
return aux[l1] > aux[l2] || (aux[l1] == aux[l2] && l1 < l2);
|
|
|
|
}
|
2013-02-13 03:16:06 +08:00
|
|
|
const int* aux;
|
|
|
|
};
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-02-09 03:54:43 +08:00
|
|
|
static void
|
|
|
|
createTrigTable( int numangle, double min_theta, double theta_step,
|
|
|
|
float irho, float *tabSin, float *tabCos )
|
|
|
|
{
|
|
|
|
float ang = static_cast<float>(min_theta);
|
|
|
|
for(int n = 0; n < numangle; ang += (float)theta_step, n++ )
|
|
|
|
{
|
|
|
|
tabSin[n] = (float)(sin((double)ang) * irho);
|
|
|
|
tabCos[n] = (float)(cos((double)ang) * irho);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
findLocalMaximums( int numrho, int numangle, int threshold,
|
|
|
|
const int *accum, std::vector<int>& sort_buf )
|
|
|
|
{
|
|
|
|
for(int r = 0; r < numrho; r++ )
|
|
|
|
for(int n = 0; n < numangle; n++ )
|
|
|
|
{
|
|
|
|
int base = (n+1) * (numrho+2) + r+1;
|
|
|
|
if( accum[base] > threshold &&
|
|
|
|
accum[base] > accum[base - 1] && accum[base] >= accum[base + 1] &&
|
|
|
|
accum[base] > accum[base - numrho - 2] && accum[base] >= accum[base + numrho + 2] )
|
|
|
|
sort_buf.push_back(base);
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 00:14:01 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/*
|
|
|
|
Here image is an input raster;
|
|
|
|
step is it's step; size characterizes it's ROI;
|
|
|
|
rho and theta are discretization steps (in pixels and radians correspondingly).
|
|
|
|
threshold is the minimum number of pixels in the feature for it
|
|
|
|
to be a candidate for line. lines is the output
|
|
|
|
array of (rho, theta) pairs. linesMax is the buffer size (number of pairs).
|
|
|
|
Functions return the actual number of found lines.
|
|
|
|
*/
|
|
|
|
static void
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesStandard( InputArray src, OutputArray lines, int type,
|
|
|
|
float rho, float theta,
|
|
|
|
int threshold, int linesMax,
|
2014-01-11 08:42:25 +08:00
|
|
|
double min_theta, double max_theta )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
CV_CheckType(type, type == CV_32FC2 || type == CV_32FC3, "Internal error");
|
|
|
|
|
|
|
|
Mat img = src.getMat();
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int i, j;
|
|
|
|
float irho = 1 / rho;
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
CV_Assert( img.type() == CV_8UC1 );
|
2018-05-24 04:42:12 +08:00
|
|
|
CV_Assert( linesMax > 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-08-13 19:08:27 +08:00
|
|
|
const uchar* image = img.ptr();
|
2013-02-13 03:16:06 +08:00
|
|
|
int step = (int)img.step;
|
|
|
|
int width = img.cols;
|
|
|
|
int height = img.rows;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
int max_rho = width + height;
|
|
|
|
int min_rho = -max_rho;
|
|
|
|
|
|
|
|
CV_CheckGE(max_theta, min_theta, "max_theta must be greater than min_theta");
|
|
|
|
|
2014-01-11 08:42:25 +08:00
|
|
|
int numangle = cvRound((max_theta - min_theta) / theta);
|
2018-05-24 04:42:12 +08:00
|
|
|
int numrho = cvRound(((max_rho - min_rho) + 1) / rho);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
|
2018-05-24 04:42:12 +08:00
|
|
|
if (type == CV_32FC2 && CV_IPP_CHECK_COND)
|
2014-04-11 15:31:21 +08:00
|
|
|
{
|
2014-10-03 19:17:28 +08:00
|
|
|
IppiSize srcSize = { width, height };
|
|
|
|
IppPointPolar delta = { rho, theta };
|
2018-05-24 04:42:12 +08:00
|
|
|
IppPointPolar dstRoi[2] = {{(Ipp32f) min_rho, (Ipp32f) min_theta},{(Ipp32f) max_rho, (Ipp32f) max_theta}};
|
2014-10-03 19:17:28 +08:00
|
|
|
int bufferSize;
|
|
|
|
int nz = countNonZero(img);
|
|
|
|
int ipp_linesMax = std::min(linesMax, nz*numangle/threshold);
|
|
|
|
int linesCount = 0;
|
2018-05-24 04:42:12 +08:00
|
|
|
std::vector<Vec2f> _lines(ipp_linesMax);
|
2014-10-03 19:17:28 +08:00
|
|
|
IppStatus ok = ippiHoughLineGetSize_8u_C1R(srcSize, delta, ipp_linesMax, &bufferSize);
|
2017-04-21 19:52:45 +08:00
|
|
|
Ipp8u* buffer = ippsMalloc_8u_L(bufferSize);
|
2018-05-24 04:42:12 +08:00
|
|
|
if (ok >= 0) {ok = CV_INSTRUMENT_FUN_IPP(ippiHoughLine_Region_8u32f_C1R, image, step, srcSize, (IppPointPolar*) &_lines[0], dstRoi, ipp_linesMax, &linesCount, delta, threshold, buffer);};
|
2014-10-03 19:17:28 +08:00
|
|
|
ippsFree(buffer);
|
|
|
|
if (ok >= 0)
|
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
lines.create(linesCount, 1, CV_32FC2);
|
|
|
|
Mat(linesCount, 1, CV_32FC2, &_lines[0]).copyTo(lines);
|
2014-10-03 19:17:28 +08:00
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIppErrorStatus();
|
2014-04-11 15:31:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-09 03:54:43 +08:00
|
|
|
|
|
|
|
Mat _accum = Mat::zeros( (numangle+2), (numrho+2), CV_32SC1 );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<int> _sort_buf;
|
2013-02-13 03:16:06 +08:00
|
|
|
AutoBuffer<float> _tabSin(numangle);
|
|
|
|
AutoBuffer<float> _tabCos(numangle);
|
2018-02-09 03:54:43 +08:00
|
|
|
int *accum = _accum.ptr<int>();
|
2018-06-11 06:42:00 +08:00
|
|
|
float *tabSin = _tabSin.data(), *tabCos = _tabCos.data();
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2018-02-09 03:54:43 +08:00
|
|
|
// create sin and cos table
|
|
|
|
createTrigTable( numangle, min_theta, theta,
|
2018-06-11 06:42:00 +08:00
|
|
|
irho, tabSin, tabCos);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// stage 1. fill accumulator
|
|
|
|
for( i = 0; i < height; i++ )
|
|
|
|
for( j = 0; j < width; j++ )
|
|
|
|
{
|
|
|
|
if( image[i * step + j] != 0 )
|
2012-06-15 21:04:17 +08:00
|
|
|
for(int n = 0; n < numangle; n++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-15 21:04:17 +08:00
|
|
|
int r = cvRound( j * tabCos[n] + i * tabSin[n] );
|
2010-05-12 01:44:00 +08:00
|
|
|
r += (numrho - 1) / 2;
|
|
|
|
accum[(n+1) * (numrho+2) + r+1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stage 2. find local maximums
|
2018-02-09 03:54:43 +08:00
|
|
|
findLocalMaximums( numrho, numangle, threshold, accum, _sort_buf );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// stage 3. sort the detected lines by accumulator value
|
2013-03-28 20:12:13 +08:00
|
|
|
std::sort(_sort_buf.begin(), _sort_buf.end(), hough_cmp_gt(accum));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// stage 4. store the first min(total,linesMax) lines to the output buffer
|
2013-02-25 00:14:01 +08:00
|
|
|
linesMax = std::min(linesMax, (int)_sort_buf.size());
|
2013-02-13 03:16:06 +08:00
|
|
|
double scale = 1./(numrho+2);
|
2018-05-24 04:42:12 +08:00
|
|
|
|
|
|
|
lines.create(linesMax, 1, type);
|
|
|
|
Mat _lines = lines.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
for( i = 0; i < linesMax; i++ )
|
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
LinePolar line;
|
2013-02-13 05:23:57 +08:00
|
|
|
int idx = _sort_buf[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
int n = cvFloor(idx*scale) - 1;
|
|
|
|
int r = idx - (n+1)*(numrho+2) - 1;
|
|
|
|
line.rho = (r - (numrho - 1)*0.5f) * rho;
|
2015-03-14 08:04:13 +08:00
|
|
|
line.angle = static_cast<float>(min_theta) + n * theta;
|
2018-05-24 04:42:12 +08:00
|
|
|
if (type == CV_32FC2)
|
|
|
|
{
|
|
|
|
_lines.at<Vec2f>(i) = Vec2f(line.rho, line.angle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_DbgAssert(type == CV_32FC3);
|
|
|
|
_lines.at<Vec3f>(i) = Vec3f(line.rho, line.angle, (float)accum[idx]);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Multi-Scale variant of Classical Hough Transform
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
struct hough_index
|
|
|
|
{
|
|
|
|
hough_index() : value(0), rho(0.f), theta(0.f) {}
|
|
|
|
hough_index(int _val, float _rho, float _theta)
|
|
|
|
: value(_val), rho(_rho), theta(_theta) {}
|
2013-02-25 00:14:01 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
int value;
|
|
|
|
float rho, theta;
|
|
|
|
};
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
static void
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesSDiv( InputArray image, OutputArray lines, int type,
|
2013-02-13 03:16:06 +08:00
|
|
|
float rho, float theta, int threshold,
|
2018-05-24 04:42:12 +08:00
|
|
|
int srn, int stn, int linesMax,
|
2014-01-11 08:42:25 +08:00
|
|
|
double min_theta, double max_theta )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
CV_CheckType(type, type == CV_32FC2 || type == CV_32FC3, "Internal error");
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
#define _POINT(row, column)\
|
|
|
|
(image_src[(row)*step+(column)])
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
Mat img = image.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
int index, i;
|
|
|
|
int ri, ti, ti1, ti0;
|
|
|
|
int row, col;
|
|
|
|
float r, t; /* Current rho and theta */
|
|
|
|
float rv; /* Some temporary rho value */
|
|
|
|
|
|
|
|
int fn = 0;
|
|
|
|
float xc, yc;
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
const float d2r = (float)(CV_PI / 180);
|
2010-05-12 01:44:00 +08:00
|
|
|
int sfn = srn * stn;
|
|
|
|
int fi;
|
|
|
|
int count;
|
|
|
|
int cmax = 0;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<hough_index> lst;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
CV_Assert( img.type() == CV_8UC1 );
|
2014-10-07 18:57:02 +08:00
|
|
|
CV_Assert( linesMax > 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
threshold = MIN( threshold, 255 );
|
|
|
|
|
2014-08-13 19:08:27 +08:00
|
|
|
const uchar* image_src = img.ptr();
|
2013-02-13 03:16:06 +08:00
|
|
|
int step = (int)img.step;
|
|
|
|
int w = img.cols;
|
|
|
|
int h = img.rows;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
float irho = 1 / rho;
|
|
|
|
float itheta = 1 / theta;
|
|
|
|
float srho = rho / srn;
|
|
|
|
float stheta = theta / stn;
|
|
|
|
float isrho = 1 / srho;
|
|
|
|
float istheta = 1 / stheta;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
int rn = cvFloor( std::sqrt( (double)w * w + (double)h * h ) * irho );
|
2013-02-13 03:16:06 +08:00
|
|
|
int tn = cvFloor( 2 * CV_PI * itheta );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
lst.push_back(hough_index(threshold, -1.f, 0.f));
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Precalculate sin table
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<float> _sinTable( 5 * tn * stn );
|
2013-02-13 03:16:06 +08:00
|
|
|
float* sinTable = &_sinTable[0];
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for( index = 0; index < 5 * tn * stn; index++ )
|
|
|
|
sinTable[index] = (float)cos( stheta * index * 0.2f );
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<uchar> _caccum(rn * tn, (uchar)0);
|
2013-02-13 03:16:06 +08:00
|
|
|
uchar* caccum = &_caccum[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Counting all feature pixels
|
2010-05-12 01:44:00 +08:00
|
|
|
for( row = 0; row < h; row++ )
|
|
|
|
for( col = 0; col < w; col++ )
|
|
|
|
fn += _POINT( row, col ) != 0;
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<int> _x(fn), _y(fn);
|
2013-02-13 03:16:06 +08:00
|
|
|
int* x = &_x[0], *y = &_y[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Full Hough Transform (it's accumulator update part)
|
2010-05-12 01:44:00 +08:00
|
|
|
fi = 0;
|
|
|
|
for( row = 0; row < h; row++ )
|
|
|
|
{
|
|
|
|
for( col = 0; col < w; col++ )
|
|
|
|
{
|
|
|
|
if( _POINT( row, col ))
|
|
|
|
{
|
|
|
|
int halftn;
|
|
|
|
float r0;
|
|
|
|
float scale_factor;
|
|
|
|
int iprev = -1;
|
|
|
|
float phi, phi1;
|
2013-02-13 03:16:06 +08:00
|
|
|
float theta_it; // Value of theta for iterating
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Remember the feature point
|
2010-05-12 01:44:00 +08:00
|
|
|
x[fi] = col;
|
|
|
|
y[fi] = row;
|
|
|
|
fi++;
|
|
|
|
|
|
|
|
yc = (float) row + 0.5f;
|
|
|
|
xc = (float) col + 0.5f;
|
|
|
|
|
|
|
|
/* Update the accumulator */
|
|
|
|
t = (float) fabs( cvFastArctan( yc, xc ) * d2r );
|
2013-02-25 00:14:01 +08:00
|
|
|
r = (float) std::sqrt( (double)xc * xc + (double)yc * yc );
|
2010-05-12 01:44:00 +08:00
|
|
|
r0 = r * irho;
|
2013-02-13 03:16:06 +08:00
|
|
|
ti0 = cvFloor( (t + CV_PI*0.5) * itheta );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
caccum[ti0]++;
|
|
|
|
|
|
|
|
theta_it = rho / r;
|
|
|
|
theta_it = theta_it < theta ? theta_it : theta;
|
|
|
|
scale_factor = theta_it * itheta;
|
2013-02-13 03:16:06 +08:00
|
|
|
halftn = cvFloor( CV_PI / theta_it );
|
|
|
|
for( ti1 = 1, phi = theta_it - (float)(CV_PI*0.5), phi1 = (theta_it + t) * itheta;
|
2010-05-12 01:44:00 +08:00
|
|
|
ti1 < halftn; ti1++, phi += theta_it, phi1 += scale_factor )
|
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
rv = r0 * std::cos( phi );
|
2014-11-06 12:46:04 +08:00
|
|
|
i = (int)rv * tn;
|
2010-05-12 01:44:00 +08:00
|
|
|
i += cvFloor( phi1 );
|
|
|
|
assert( i >= 0 );
|
|
|
|
assert( i < rn * tn );
|
|
|
|
caccum[i] = (uchar) (caccum[i] + ((i ^ iprev) != 0));
|
|
|
|
iprev = i;
|
|
|
|
if( cmax < caccum[i] )
|
|
|
|
cmax = caccum[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Starting additional analysis
|
2010-05-12 01:44:00 +08:00
|
|
|
count = 0;
|
|
|
|
for( ri = 0; ri < rn; ri++ )
|
|
|
|
{
|
|
|
|
for( ti = 0; ti < tn; ti++ )
|
|
|
|
{
|
2011-08-17 17:19:46 +08:00
|
|
|
if( caccum[ri * tn + ti] > threshold )
|
2010-05-12 01:44:00 +08:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( count * 100 > rn * tn )
|
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesStandard( image, lines, type, rho, theta, threshold, linesMax, min_theta, max_theta );
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<uchar> _buffer(srn * stn + 2);
|
2013-02-13 03:16:06 +08:00
|
|
|
uchar* buffer = &_buffer[0];
|
|
|
|
uchar* mcaccum = buffer + 1;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for( ri = 0; ri < rn; ri++ )
|
|
|
|
{
|
|
|
|
for( ti = 0; ti < tn; ti++ )
|
|
|
|
{
|
|
|
|
if( caccum[ri * tn + ti] > threshold )
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
memset( mcaccum, 0, sfn * sizeof( uchar ));
|
|
|
|
|
|
|
|
for( index = 0; index < fn; index++ )
|
|
|
|
{
|
|
|
|
int ti2;
|
|
|
|
float r0;
|
|
|
|
|
|
|
|
yc = (float) y[index] + 0.5f;
|
|
|
|
xc = (float) x[index] + 0.5f;
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Update the accumulator
|
2010-05-12 01:44:00 +08:00
|
|
|
t = (float) fabs( cvFastArctan( yc, xc ) * d2r );
|
2013-02-25 00:14:01 +08:00
|
|
|
r = (float) std::sqrt( (double)xc * xc + (double)yc * yc ) * isrho;
|
2013-02-13 03:16:06 +08:00
|
|
|
ti0 = cvFloor( (t + CV_PI * 0.5) * istheta );
|
2010-05-12 01:44:00 +08:00
|
|
|
ti2 = (ti * stn - ti0) * 5;
|
|
|
|
r0 = (float) ri *srn;
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
for( ti1 = 0; ti1 < stn; ti1++, ti2 += 5 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
rv = r * sinTable[(int) (std::abs( ti2 ))] - r0;
|
2010-05-12 01:44:00 +08:00
|
|
|
i = cvFloor( rv ) * stn + ti1;
|
|
|
|
|
|
|
|
i = CV_IMAX( i, -1 );
|
|
|
|
i = CV_IMIN( i, sfn );
|
|
|
|
mcaccum[i]++;
|
|
|
|
assert( i >= -1 );
|
|
|
|
assert( i <= sfn );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
// Find peaks in maccum...
|
2010-05-12 01:44:00 +08:00
|
|
|
for( index = 0; index < sfn; index++ )
|
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
int pos = (int)(lst.size() - 1);
|
|
|
|
if( pos < 0 || lst[pos].value < mcaccum[index] )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
hough_index vi(mcaccum[index],
|
|
|
|
index / stn * srho + ri * rho,
|
|
|
|
index % stn * stheta + ti * theta - (float)(CV_PI*0.5));
|
|
|
|
lst.push_back(vi);
|
|
|
|
for( ; pos >= 0; pos-- )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
if( lst[pos].value > vi.value )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2013-02-13 03:16:06 +08:00
|
|
|
lst[pos+1] = lst[pos];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2013-02-13 03:16:06 +08:00
|
|
|
lst[pos+1] = vi;
|
|
|
|
if( (int)lst.size() > linesMax )
|
|
|
|
lst.pop_back();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
lines.create((int)lst.size(), 1, type);
|
|
|
|
Mat _lines = lines.getMat();
|
2013-02-13 05:23:57 +08:00
|
|
|
for( size_t idx = 0; idx < lst.size(); idx++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 05:23:57 +08:00
|
|
|
if( lst[idx].rho < 0 )
|
2013-02-13 03:16:06 +08:00
|
|
|
continue;
|
2018-05-24 04:42:12 +08:00
|
|
|
if (type == CV_32FC2)
|
|
|
|
{
|
|
|
|
_lines.at<Vec2f>((int)idx) = Vec2f(lst[idx].rho, lst[idx].theta);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CV_DbgAssert(type == CV_32FC3);
|
|
|
|
_lines.at<Vec3f>((int)idx) = Vec3f(lst[idx].rho, lst[idx].theta, (float)lst[idx].value);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
* Probabilistic Hough Transform *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
|
|
|
static void
|
2013-02-13 03:16:06 +08:00
|
|
|
HoughLinesProbabilistic( Mat& image,
|
|
|
|
float rho, float theta, int threshold,
|
|
|
|
int lineLength, int lineGap,
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Vec4i>& lines, int linesMax )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
Point pt;
|
2010-05-12 01:44:00 +08:00
|
|
|
float irho = 1 / rho;
|
2013-02-13 03:16:06 +08:00
|
|
|
RNG rng((uint64)-1);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
CV_Assert( image.type() == CV_8UC1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
int width = image.cols;
|
|
|
|
int height = image.rows;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
int numangle = cvRound(CV_PI / theta);
|
|
|
|
int numrho = cvRound(((width + height) * 2 + 1) / rho);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
#if defined HAVE_IPP && IPP_VERSION_X100 >= 810 && !IPP_DISABLE_HOUGH
|
2014-10-03 19:17:28 +08:00
|
|
|
CV_IPP_CHECK()
|
2014-04-23 15:59:19 +08:00
|
|
|
{
|
2014-10-03 19:17:28 +08:00
|
|
|
IppiSize srcSize = { width, height };
|
|
|
|
IppPointPolar delta = { rho, theta };
|
|
|
|
IppiHoughProbSpec* pSpec;
|
|
|
|
int bufferSize, specSize;
|
|
|
|
int ipp_linesMax = std::min(linesMax, numangle*numrho);
|
|
|
|
int linesCount = 0;
|
|
|
|
lines.resize(ipp_linesMax);
|
|
|
|
IppStatus ok = ippiHoughProbLineGetSize_8u_C1R(srcSize, delta, &specSize, &bufferSize);
|
2017-04-21 19:52:45 +08:00
|
|
|
Ipp8u* buffer = ippsMalloc_8u_L(bufferSize);
|
|
|
|
pSpec = (IppiHoughProbSpec*) ippsMalloc_8u_L(specSize);
|
2014-10-03 19:17:28 +08:00
|
|
|
if (ok >= 0) ok = ippiHoughProbLineInit_8u32f_C1R(srcSize, delta, ippAlgHintNone, pSpec);
|
2017-04-21 19:52:45 +08:00
|
|
|
if (ok >= 0) {ok = CV_INSTRUMENT_FUN_IPP(ippiHoughProbLine_8u32f_C1R, image.data, (int)image.step, srcSize, threshold, lineLength, lineGap, (IppiPoint*) &lines[0], ipp_linesMax, &linesCount, buffer, pSpec);};
|
2014-10-03 19:17:28 +08:00
|
|
|
|
2017-04-21 19:52:45 +08:00
|
|
|
ippsFree(pSpec);
|
2014-10-03 19:17:28 +08:00
|
|
|
ippsFree(buffer);
|
|
|
|
if (ok >= 0)
|
|
|
|
{
|
|
|
|
lines.resize(linesCount);
|
|
|
|
CV_IMPL_ADD(CV_IMPL_IPP);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lines.clear();
|
|
|
|
setIppErrorStatus();
|
2014-04-23 15:59:19 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
Mat accum = Mat::zeros( numangle, numrho, CV_32SC1 );
|
|
|
|
Mat mask( height, width, CV_8UC1 );
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<float> trigtab(numangle*2);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
for( int n = 0; n < numangle; n++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-14 19:36:20 +08:00
|
|
|
trigtab[n*2] = (float)(cos((double)n*theta) * irho);
|
|
|
|
trigtab[n*2+1] = (float)(sin((double)n*theta) * irho);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2013-02-13 03:16:06 +08:00
|
|
|
const float* ttab = &trigtab[0];
|
2014-08-13 19:08:27 +08:00
|
|
|
uchar* mdata0 = mask.ptr();
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Point> nzloc;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// stage 1. collect non-zero image points
|
2013-02-13 03:16:06 +08:00
|
|
|
for( pt.y = 0; pt.y < height; pt.y++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
const uchar* data = image.ptr(pt.y);
|
|
|
|
uchar* mdata = mask.ptr(pt.y);
|
2010-05-12 01:44:00 +08:00
|
|
|
for( pt.x = 0; pt.x < width; pt.x++ )
|
|
|
|
{
|
|
|
|
if( data[pt.x] )
|
|
|
|
{
|
|
|
|
mdata[pt.x] = (uchar)1;
|
2013-02-13 03:16:06 +08:00
|
|
|
nzloc.push_back(pt);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mdata[pt.x] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
int count = (int)nzloc.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// stage 2. process all the points in random order
|
|
|
|
for( ; count > 0; count-- )
|
|
|
|
{
|
|
|
|
// choose random point out of the remaining ones
|
2013-02-13 03:16:06 +08:00
|
|
|
int idx = rng.uniform(0, count);
|
2010-05-12 01:44:00 +08:00
|
|
|
int max_val = threshold-1, max_n = 0;
|
2013-02-13 03:16:06 +08:00
|
|
|
Point point = nzloc[idx];
|
|
|
|
Point line_end[2];
|
2010-05-12 01:44:00 +08:00
|
|
|
float a, b;
|
2014-08-13 19:08:27 +08:00
|
|
|
int* adata = accum.ptr<int>();
|
2013-02-13 03:16:06 +08:00
|
|
|
int i = point.y, j = point.x, k, x0, y0, dx0, dy0, xflag;
|
2010-05-12 01:44:00 +08:00
|
|
|
int good_line;
|
|
|
|
const int shift = 16;
|
|
|
|
|
|
|
|
// "remove" it by overriding it with the last element
|
2013-02-13 03:16:06 +08:00
|
|
|
nzloc[idx] = nzloc[count-1];
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
// check if it has been excluded already (i.e. belongs to some other line)
|
|
|
|
if( !mdata0[i*width + j] )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// update accumulator, find the most probable line
|
2013-02-13 03:16:06 +08:00
|
|
|
for( int n = 0; n < numangle; n++, adata += numrho )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
int r = cvRound( j * ttab[n*2] + i * ttab[n*2+1] );
|
2010-05-12 01:44:00 +08:00
|
|
|
r += (numrho - 1) / 2;
|
|
|
|
int val = ++adata[r];
|
|
|
|
if( max_val < val )
|
|
|
|
{
|
|
|
|
max_val = val;
|
|
|
|
max_n = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it is too "weak" candidate, continue with another point
|
|
|
|
if( max_val < threshold )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// from the current point walk in each direction
|
|
|
|
// along the found line and extract the line segment
|
|
|
|
a = -ttab[max_n*2+1];
|
|
|
|
b = ttab[max_n*2];
|
|
|
|
x0 = j;
|
|
|
|
y0 = i;
|
|
|
|
if( fabs(a) > fabs(b) )
|
|
|
|
{
|
|
|
|
xflag = 1;
|
|
|
|
dx0 = a > 0 ? 1 : -1;
|
|
|
|
dy0 = cvRound( b*(1 << shift)/fabs(a) );
|
|
|
|
y0 = (y0 << shift) + (1 << (shift-1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xflag = 0;
|
|
|
|
dy0 = b > 0 ? 1 : -1;
|
|
|
|
dx0 = cvRound( a*(1 << shift)/fabs(b) );
|
|
|
|
x0 = (x0 << shift) + (1 << (shift-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
for( k = 0; k < 2; k++ )
|
|
|
|
{
|
|
|
|
int gap = 0, x = x0, y = y0, dx = dx0, dy = dy0;
|
|
|
|
|
|
|
|
if( k > 0 )
|
|
|
|
dx = -dx, dy = -dy;
|
|
|
|
|
|
|
|
// walk along the line using fixed-point arithmetics,
|
|
|
|
// stop at the image border or in case of too big gap
|
|
|
|
for( ;; x += dx, y += dy )
|
|
|
|
{
|
|
|
|
uchar* mdata;
|
|
|
|
int i1, j1;
|
|
|
|
|
|
|
|
if( xflag )
|
|
|
|
{
|
|
|
|
j1 = x;
|
|
|
|
i1 = y >> shift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j1 = x >> shift;
|
|
|
|
i1 = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( j1 < 0 || j1 >= width || i1 < 0 || i1 >= height )
|
|
|
|
break;
|
|
|
|
|
|
|
|
mdata = mdata0 + i1*width + j1;
|
|
|
|
|
|
|
|
// for each non-zero point:
|
|
|
|
// update line end,
|
|
|
|
// clear the mask element
|
|
|
|
// reset the gap
|
|
|
|
if( *mdata )
|
|
|
|
{
|
|
|
|
gap = 0;
|
|
|
|
line_end[k].y = i1;
|
|
|
|
line_end[k].x = j1;
|
|
|
|
}
|
|
|
|
else if( ++gap > lineGap )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
good_line = std::abs(line_end[1].x - line_end[0].x) >= lineLength ||
|
|
|
|
std::abs(line_end[1].y - line_end[0].y) >= lineLength;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
for( k = 0; k < 2; k++ )
|
|
|
|
{
|
|
|
|
int x = x0, y = y0, dx = dx0, dy = dy0;
|
|
|
|
|
|
|
|
if( k > 0 )
|
|
|
|
dx = -dx, dy = -dy;
|
|
|
|
|
|
|
|
// walk along the line using fixed-point arithmetics,
|
|
|
|
// stop at the image border or in case of too big gap
|
|
|
|
for( ;; x += dx, y += dy )
|
|
|
|
{
|
|
|
|
uchar* mdata;
|
|
|
|
int i1, j1;
|
|
|
|
|
|
|
|
if( xflag )
|
|
|
|
{
|
|
|
|
j1 = x;
|
|
|
|
i1 = y >> shift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
j1 = x >> shift;
|
|
|
|
i1 = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdata = mdata0 + i1*width + j1;
|
|
|
|
|
|
|
|
// for each non-zero point:
|
|
|
|
// update line end,
|
|
|
|
// clear the mask element
|
|
|
|
// reset the gap
|
|
|
|
if( *mdata )
|
|
|
|
{
|
|
|
|
if( good_line )
|
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
adata = accum.ptr<int>();
|
2013-02-13 03:16:06 +08:00
|
|
|
for( int n = 0; n < numangle; n++, adata += numrho )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
int r = cvRound( j1 * ttab[n*2] + i1 * ttab[n*2+1] );
|
2010-05-12 01:44:00 +08:00
|
|
|
r += (numrho - 1) / 2;
|
|
|
|
adata[r]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*mdata = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i1 == line_end[k].y && j1 == line_end[k].x )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( good_line )
|
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
Vec4i lr(line_end[0].x, line_end[0].y, line_end[1].x, line_end[1].y);
|
|
|
|
lines.push_back(lr);
|
|
|
|
if( (int)lines.size() >= linesMax )
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 16:50:12 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2014-10-07 18:57:02 +08:00
|
|
|
#define OCL_MAX_LINES 4096
|
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
static bool ocl_makePointsList(InputArray _src, OutputArray _pointsList, InputOutputArray _counters)
|
2014-08-22 20:50:01 +08:00
|
|
|
{
|
|
|
|
UMat src = _src.getUMat();
|
2014-09-05 16:20:29 +08:00
|
|
|
_pointsList.create(1, (int) src.total(), CV_32SC1);
|
|
|
|
UMat pointsList = _pointsList.getUMat();
|
|
|
|
UMat counters = _counters.getUMat();
|
2014-08-25 19:57:58 +08:00
|
|
|
ocl::Device dev = ocl::Device::getDefault();
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
const int pixPerWI = 16;
|
|
|
|
int workgroup_size = min((int) dev.maxWorkGroupSize(), (src.cols + pixPerWI - 1)/pixPerWI);
|
|
|
|
ocl::Kernel pointListKernel("make_point_list", ocl::imgproc::hough_lines_oclsrc,
|
2014-09-04 20:31:30 +08:00
|
|
|
format("-D MAKE_POINTS_LIST -D GROUP_SIZE=%d -D LOCAL_SIZE=%d", workgroup_size, src.cols));
|
2014-08-22 20:50:01 +08:00
|
|
|
if (pointListKernel.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
pointListKernel.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnlyNoSize(pointsList),
|
2014-09-04 20:31:30 +08:00
|
|
|
ocl::KernelArg::PtrWriteOnly(counters));
|
2014-09-09 16:50:12 +08:00
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t localThreads[2] = { (size_t)workgroup_size, 1 };
|
|
|
|
size_t globalThreads[2] = { (size_t)workgroup_size, (size_t)src.rows };
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
return pointListKernel.run(2, globalThreads, localThreads, false);
|
|
|
|
}
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
static bool ocl_fillAccum(InputArray _pointsList, OutputArray _accum, int total_points, double rho, double theta, int numrho, int numangle)
|
|
|
|
{
|
|
|
|
UMat pointsList = _pointsList.getUMat();
|
|
|
|
_accum.create(numangle + 2, numrho + 2, CV_32SC1);
|
|
|
|
UMat accum = _accum.getUMat();
|
|
|
|
ocl::Device dev = ocl::Device::getDefault();
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
float irho = (float) (1 / rho);
|
|
|
|
int workgroup_size = min((int) dev.maxWorkGroupSize(), total_points);
|
2014-09-09 16:50:12 +08:00
|
|
|
|
2014-08-25 19:57:58 +08:00
|
|
|
ocl::Kernel fillAccumKernel;
|
2014-09-05 16:20:29 +08:00
|
|
|
size_t localThreads[2];
|
|
|
|
size_t globalThreads[2];
|
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
size_t local_memory_needed = (numrho + 2)*sizeof(int);
|
2014-08-25 19:57:58 +08:00
|
|
|
if (local_memory_needed > dev.localMemSize())
|
|
|
|
{
|
2014-09-04 20:31:30 +08:00
|
|
|
accum.setTo(Scalar::all(0));
|
2014-08-25 19:57:58 +08:00
|
|
|
fillAccumKernel.create("fill_accum_global", ocl::imgproc::hough_lines_oclsrc,
|
|
|
|
format("-D FILL_ACCUM_GLOBAL"));
|
2014-09-05 16:20:29 +08:00
|
|
|
if (fillAccumKernel.empty())
|
|
|
|
return false;
|
2014-09-04 20:31:30 +08:00
|
|
|
globalThreads[0] = workgroup_size; globalThreads[1] = numangle;
|
2014-10-07 18:57:02 +08:00
|
|
|
fillAccumKernel.args(ocl::KernelArg::ReadOnlyNoSize(pointsList), ocl::KernelArg::WriteOnlyNoSize(accum),
|
2014-09-05 16:20:29 +08:00
|
|
|
total_points, irho, (float) theta, numrho, numangle);
|
|
|
|
return fillAccumKernel.run(2, globalThreads, NULL, false);
|
2014-08-25 19:57:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fillAccumKernel.create("fill_accum_local", ocl::imgproc::hough_lines_oclsrc,
|
2014-09-04 20:31:30 +08:00
|
|
|
format("-D FILL_ACCUM_LOCAL -D LOCAL_SIZE=%d -D BUFFER_SIZE=%d", workgroup_size, numrho + 2));
|
2014-09-05 16:20:29 +08:00
|
|
|
if (fillAccumKernel.empty())
|
|
|
|
return false;
|
2014-09-04 20:31:30 +08:00
|
|
|
localThreads[0] = workgroup_size; localThreads[1] = 1;
|
|
|
|
globalThreads[0] = workgroup_size; globalThreads[1] = numangle+2;
|
2014-10-07 18:57:02 +08:00
|
|
|
fillAccumKernel.args(ocl::KernelArg::ReadOnlyNoSize(pointsList), ocl::KernelArg::WriteOnlyNoSize(accum),
|
2014-09-05 16:20:29 +08:00
|
|
|
total_points, irho, (float) theta, numrho, numangle);
|
|
|
|
return fillAccumKernel.run(2, globalThreads, localThreads, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
static bool ocl_HoughLines(InputArray _src, OutputArray _lines, double rho, double theta, int threshold,
|
2014-09-05 16:20:29 +08:00
|
|
|
double min_theta, double max_theta)
|
|
|
|
{
|
|
|
|
CV_Assert(_src.type() == CV_8UC1);
|
|
|
|
|
|
|
|
if (max_theta < 0 || max_theta > CV_PI ) {
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error( Error::StsBadArg, "max_theta must fall between 0 and pi" );
|
2014-08-25 19:57:58 +08:00
|
|
|
}
|
2014-09-05 16:20:29 +08:00
|
|
|
if (min_theta < 0 || min_theta > max_theta ) {
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error( Error::StsBadArg, "min_theta must fall between 0 and max_theta" );
|
2014-09-05 16:20:29 +08:00
|
|
|
}
|
2014-10-07 18:57:02 +08:00
|
|
|
if (!(rho > 0 && theta > 0)) {
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error( Error::StsBadArg, "rho and theta must be greater 0" );
|
2014-10-07 18:57:02 +08:00
|
|
|
}
|
2014-09-05 16:20:29 +08:00
|
|
|
|
|
|
|
UMat src = _src.getUMat();
|
|
|
|
int numangle = cvRound((max_theta - min_theta) / theta);
|
|
|
|
int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
|
2014-09-09 16:50:12 +08:00
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
UMat pointsList;
|
|
|
|
UMat counters(1, 2, CV_32SC1, Scalar::all(0));
|
|
|
|
|
|
|
|
if (!ocl_makePointsList(src, pointsList, counters))
|
2014-08-22 20:50:01 +08:00
|
|
|
return false;
|
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
int total_points = counters.getMat(ACCESS_READ).at<int>(0, 0);
|
|
|
|
if (total_points <= 0)
|
|
|
|
{
|
2018-06-01 00:36:39 +08:00
|
|
|
_lines.release();
|
2014-09-05 16:20:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
UMat accum;
|
|
|
|
if (!ocl_fillAccum(pointsList, accum, total_points, rho, theta, numrho, numangle))
|
2014-08-22 20:50:01 +08:00
|
|
|
return false;
|
|
|
|
|
2014-09-05 15:37:16 +08:00
|
|
|
const int pixPerWI = 8;
|
2014-08-25 19:57:58 +08:00
|
|
|
ocl::Kernel getLinesKernel("get_lines", ocl::imgproc::hough_lines_oclsrc,
|
|
|
|
format("-D GET_LINES"));
|
|
|
|
if (getLinesKernel.empty())
|
|
|
|
return false;
|
|
|
|
|
2014-10-07 18:57:02 +08:00
|
|
|
int linesMax = threshold > 0 ? min(total_points*numangle/threshold, OCL_MAX_LINES) : OCL_MAX_LINES;
|
2014-09-04 20:31:30 +08:00
|
|
|
UMat lines(linesMax, 1, CV_32FC2);
|
|
|
|
|
2014-08-25 19:57:58 +08:00
|
|
|
getLinesKernel.args(ocl::KernelArg::ReadOnly(accum), ocl::KernelArg::WriteOnlyNoSize(lines),
|
2014-09-04 20:31:30 +08:00
|
|
|
ocl::KernelArg::PtrWriteOnly(counters), linesMax, threshold, (float) rho, (float) theta);
|
2014-08-25 19:57:58 +08:00
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalThreads[2] = { ((size_t)numrho + pixPerWI - 1)/pixPerWI, (size_t)numangle };
|
2014-08-25 19:57:58 +08:00
|
|
|
if (!getLinesKernel.run(2, globalThreads, NULL, false))
|
|
|
|
return false;
|
2014-09-09 16:50:12 +08:00
|
|
|
|
2014-09-04 20:31:30 +08:00
|
|
|
int total_lines = min(counters.getMat(ACCESS_READ).at<int>(0, 1), linesMax);
|
2014-08-25 19:57:58 +08:00
|
|
|
if (total_lines > 0)
|
|
|
|
_lines.assign(lines.rowRange(Range(0, total_lines)));
|
|
|
|
else
|
2018-06-01 00:36:39 +08:00
|
|
|
_lines.release();
|
2014-08-25 19:57:58 +08:00
|
|
|
return true;
|
2013-02-13 03:16:06 +08:00
|
|
|
}
|
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
static bool ocl_HoughLinesP(InputArray _src, OutputArray _lines, double rho, double theta, int threshold,
|
2014-09-05 16:20:29 +08:00
|
|
|
double minLineLength, double maxGap)
|
|
|
|
{
|
|
|
|
CV_Assert(_src.type() == CV_8UC1);
|
2014-09-09 16:50:12 +08:00
|
|
|
|
2014-10-07 18:57:02 +08:00
|
|
|
if (!(rho > 0 && theta > 0)) {
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error( Error::StsBadArg, "rho and theta must be greater 0" );
|
2014-10-07 18:57:02 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 16:20:29 +08:00
|
|
|
UMat src = _src.getUMat();
|
2014-09-09 16:50:12 +08:00
|
|
|
int numangle = cvRound(CV_PI / theta);
|
|
|
|
int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
|
|
|
|
|
|
|
|
UMat pointsList;
|
|
|
|
UMat counters(1, 2, CV_32SC1, Scalar::all(0));
|
|
|
|
|
|
|
|
if (!ocl_makePointsList(src, pointsList, counters))
|
|
|
|
return false;
|
2014-09-05 16:20:29 +08:00
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
int total_points = counters.getMat(ACCESS_READ).at<int>(0, 0);
|
|
|
|
if (total_points <= 0)
|
|
|
|
{
|
2018-06-01 00:36:39 +08:00
|
|
|
_lines.release();
|
2014-09-09 16:50:12 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UMat accum;
|
|
|
|
if (!ocl_fillAccum(pointsList, accum, total_points, rho, theta, numrho, numangle))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ocl::Kernel getLinesKernel("get_lines", ocl::imgproc::hough_lines_oclsrc,
|
|
|
|
format("-D GET_LINES_PROBABOLISTIC"));
|
|
|
|
if (getLinesKernel.empty())
|
|
|
|
return false;
|
|
|
|
|
2014-10-07 18:57:02 +08:00
|
|
|
int linesMax = threshold > 0 ? min(total_points*numangle/threshold, OCL_MAX_LINES) : OCL_MAX_LINES;
|
2014-09-09 16:50:12 +08:00
|
|
|
UMat lines(linesMax, 1, CV_32SC4);
|
|
|
|
|
|
|
|
getLinesKernel.args(ocl::KernelArg::ReadOnly(accum), ocl::KernelArg::ReadOnly(src),
|
|
|
|
ocl::KernelArg::WriteOnlyNoSize(lines), ocl::KernelArg::PtrWriteOnly(counters),
|
|
|
|
linesMax, threshold, (int) minLineLength, (int) maxGap, (float) rho, (float) theta);
|
|
|
|
|
2015-10-16 22:10:00 +08:00
|
|
|
size_t globalThreads[2] = { (size_t)numrho, (size_t)numangle };
|
2014-09-09 16:50:12 +08:00
|
|
|
if (!getLinesKernel.run(2, globalThreads, NULL, false))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int total_lines = min(counters.getMat(ACCESS_READ).at<int>(0, 1), linesMax);
|
|
|
|
if (total_lines > 0)
|
|
|
|
_lines.assign(lines.rowRange(Range(0, total_lines)));
|
|
|
|
else
|
2018-06-01 00:36:39 +08:00
|
|
|
_lines.release();
|
2014-09-09 16:50:12 +08:00
|
|
|
|
|
|
|
return true;
|
2014-09-05 16:20:29 +08:00
|
|
|
}
|
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
#endif /* HAVE_OPENCL */
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
void HoughLines( InputArray _image, OutputArray lines,
|
2017-11-21 19:18:47 +08:00
|
|
|
double rho, double theta, int threshold,
|
|
|
|
double srn, double stn, double min_theta, double max_theta )
|
2013-02-13 03:16:06 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
int type = CV_32FC2;
|
|
|
|
if (lines.fixedType())
|
|
|
|
{
|
|
|
|
type = lines.type();
|
|
|
|
CV_CheckType(type, type == CV_32FC2 || type == CV_32FC3, "Wrong type of output lines");
|
|
|
|
}
|
2014-08-22 20:50:01 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
CV_OCL_RUN(srn == 0 && stn == 0 && _image.isUMat() && lines.isUMat() && type == CV_32FC2,
|
|
|
|
ocl_HoughLines(_image, lines, rho, theta, threshold, min_theta, max_theta));
|
2013-02-13 03:16:06 +08:00
|
|
|
|
|
|
|
if( srn == 0 && stn == 0 )
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesStandard(_image, lines, type, (float)rho, (float)theta, threshold, INT_MAX, min_theta, max_theta );
|
2013-02-13 03:16:06 +08:00
|
|
|
else
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesSDiv(_image, lines, type, (float)rho, (float)theta, threshold, cvRound(srn), cvRound(stn), INT_MAX, min_theta, max_theta);
|
2013-02-13 03:16:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
void HoughLinesP(InputArray _image, OutputArray _lines,
|
|
|
|
double rho, double theta, int threshold,
|
|
|
|
double minLineLength, double maxGap )
|
2013-02-13 03:16:06 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2014-09-09 16:50:12 +08:00
|
|
|
CV_OCL_RUN(_image.isUMat() && _lines.isUMat(),
|
|
|
|
ocl_HoughLinesP(_image, _lines, rho, theta, threshold, minLineLength, maxGap));
|
2014-09-05 16:20:29 +08:00
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
Mat image = _image.getMat();
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector<Vec4i> lines;
|
2013-02-13 05:23:57 +08:00
|
|
|
HoughLinesProbabilistic(image, (float)rho, (float)theta, threshold, cvRound(minLineLength), cvRound(maxGap), lines, INT_MAX);
|
2013-02-13 03:16:06 +08:00
|
|
|
Mat(lines).copyTo(_lines);
|
|
|
|
}
|
|
|
|
|
2018-02-09 03:54:43 +08:00
|
|
|
void HoughLinesPointSet( InputArray _point, OutputArray _lines, int lines_max, int threshold,
|
|
|
|
double min_rho, double max_rho, double rho_step,
|
|
|
|
double min_theta, double max_theta, double theta_step )
|
|
|
|
{
|
|
|
|
std::vector<Vec3d> lines;
|
|
|
|
std::vector<Point2f> point;
|
|
|
|
_point.copyTo(point);
|
|
|
|
|
|
|
|
CV_Assert( _point.type() == CV_32FC2 || _point.type() == CV_32SC2 );
|
|
|
|
if( lines_max <= 0 ) {
|
|
|
|
CV_Error( Error::StsBadArg, "lines_max must be greater than 0" );
|
|
|
|
}
|
|
|
|
if( threshold < 0) {
|
|
|
|
CV_Error( Error::StsBadArg, "threshold must be greater than 0" );
|
|
|
|
}
|
|
|
|
if( ((max_rho - min_rho) <= 0) || ((max_theta - min_theta) <= 0) ) {
|
|
|
|
CV_Error( Error::StsBadArg, "max must be greater than min" );
|
|
|
|
}
|
|
|
|
if( ((rho_step <= 0)) || ((theta_step <= 0)) ) {
|
|
|
|
CV_Error( Error::StsBadArg, "step must be greater than 0" );
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
float irho = 1 / (float)rho_step;
|
|
|
|
float irho_min = ((float)min_rho * irho);
|
|
|
|
int numangle = cvRound((max_theta - min_theta) / theta_step);
|
|
|
|
int numrho = cvRound((max_rho - min_rho + 1) / rho_step);
|
|
|
|
|
|
|
|
Mat _accum = Mat::zeros( (numangle+2), (numrho+2), CV_32SC1 );
|
|
|
|
std::vector<int> _sort_buf;
|
|
|
|
AutoBuffer<float> _tabSin(numangle);
|
|
|
|
AutoBuffer<float> _tabCos(numangle);
|
|
|
|
int *accum = _accum.ptr<int>();
|
2018-06-11 06:42:00 +08:00
|
|
|
float *tabSin = _tabSin.data(), *tabCos = _tabCos.data();
|
2018-02-09 03:54:43 +08:00
|
|
|
|
|
|
|
// create sin and cos table
|
|
|
|
createTrigTable( numangle, min_theta, theta_step,
|
|
|
|
irho, tabSin, tabCos );
|
|
|
|
|
|
|
|
// stage 1. fill accumlator
|
|
|
|
for( i = 0; i < (int)point.size(); i++ )
|
|
|
|
for(int n = 0; n < numangle; n++ )
|
|
|
|
{
|
|
|
|
int r = cvRound( point.at(i).x * tabCos[n] + point.at(i).y * tabSin[n] - irho_min);
|
|
|
|
accum[(n+1) * (numrho+2) + r+1]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stage 2. find local maximums
|
|
|
|
findLocalMaximums( numrho, numangle, threshold, accum, _sort_buf );
|
|
|
|
|
|
|
|
// stage 3. sort the detected lines by accumulator value
|
|
|
|
std::sort(_sort_buf.begin(), _sort_buf.end(), hough_cmp_gt(accum));
|
|
|
|
|
|
|
|
// stage 4. store the first min(total,linesMax) lines to the output buffer
|
|
|
|
lines_max = std::min(lines_max, (int)_sort_buf.size());
|
|
|
|
double scale = 1./(numrho+2);
|
|
|
|
for( i = 0; i < lines_max; i++ )
|
|
|
|
{
|
|
|
|
LinePolar line;
|
|
|
|
int idx = _sort_buf[i];
|
|
|
|
int n = cvFloor(idx*scale) - 1;
|
|
|
|
int r = idx - (n+1)*(numrho+2) - 1;
|
|
|
|
line.rho = static_cast<float>(min_rho) + r * (float)rho_step;
|
|
|
|
line.angle = static_cast<float>(min_theta) + n * (float)theta_step;
|
|
|
|
lines.push_back(Vec3d((double)accum[idx], (double)line.rho, (double)line.angle));
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat(lines).copyTo(_lines);
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
/****************************************************************************************\
|
|
|
|
* Circle Detection *
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
struct EstimatedCircle
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
EstimatedCircle(Vec3f _c, int _accum) :
|
|
|
|
c(_c), accum(_accum) {}
|
2017-11-21 19:18:47 +08:00
|
|
|
Vec3f c;
|
2017-12-28 22:23:11 +08:00
|
|
|
int accum;
|
2017-11-21 19:18:47 +08:00
|
|
|
};
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
static bool cmpAccum(const EstimatedCircle& left, const EstimatedCircle& right)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
// Compare everything so the order is completely deterministic
|
|
|
|
// Larger accum first
|
|
|
|
if (left.accum > right.accum)
|
|
|
|
return true;
|
|
|
|
else if (left.accum < right.accum)
|
|
|
|
return false;
|
|
|
|
// Larger radius first
|
|
|
|
else if (left.c[2] > right.c[2])
|
|
|
|
return true;
|
|
|
|
else if (left.c[2] < right.c[2])
|
|
|
|
return false;
|
|
|
|
// Smaller X
|
|
|
|
else if (left.c[0] < right.c[0])
|
|
|
|
return true;
|
|
|
|
else if (left.c[0] > right.c[0])
|
|
|
|
return false;
|
|
|
|
// Smaller Y
|
|
|
|
else if (left.c[1] < right.c[1])
|
|
|
|
return true;
|
|
|
|
else if (left.c[1] > right.c[1])
|
|
|
|
return false;
|
|
|
|
// Identical - neither object is less than the other
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
static inline Vec3f GetCircle(const EstimatedCircle& est)
|
2017-12-28 22:23:11 +08:00
|
|
|
{
|
|
|
|
return est.c;
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
static inline Vec4f GetCircle4f(const EstimatedCircle& est)
|
|
|
|
{
|
|
|
|
return Vec4f(est.c[0], est.c[1], est.c[2], (float)est.accum);
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
class NZPointList : public std::vector<Point>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
NZPointList(const NZPointList& other); // non-copyable
|
|
|
|
|
|
|
|
public:
|
|
|
|
NZPointList(int reserveSize = 256)
|
|
|
|
{
|
|
|
|
reserve(reserveSize);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class NZPointSet
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
NZPointSet(const NZPointSet& other); // non-copyable
|
|
|
|
|
|
|
|
public:
|
|
|
|
Mat_<uchar> positions;
|
|
|
|
|
|
|
|
NZPointSet(int rows, int cols) :
|
|
|
|
positions(rows, cols, (uchar)0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const Point& pt)
|
|
|
|
{
|
|
|
|
positions(pt) = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void insert(const NZPointSet& from)
|
|
|
|
{
|
|
|
|
cv::bitwise_or(from.positions, positions, positions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void toList(NZPointList& list) const
|
|
|
|
{
|
|
|
|
for (int y = 0; y < positions.rows; y++)
|
|
|
|
{
|
|
|
|
const uchar *ptr = positions.ptr<uchar>(y, 0);
|
|
|
|
for (int x = 0; x < positions.cols; x++)
|
|
|
|
{
|
|
|
|
if (ptr[x])
|
|
|
|
{
|
|
|
|
list.push_back(Point(x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
class HoughCirclesAccumInvoker : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HoughCirclesAccumInvoker(const Mat &_edges, const Mat &_dx, const Mat &_dy, int _minRadius, int _maxRadius, float _idp,
|
2017-12-28 22:23:11 +08:00
|
|
|
std::vector<Mat>& _accumVec, NZPointSet& _nz, Mutex& _mtx) :
|
2017-11-21 19:18:47 +08:00
|
|
|
edges(_edges), dx(_dx), dy(_dy), minRadius(_minRadius), maxRadius(_maxRadius), idp(_idp),
|
|
|
|
accumVec(_accumVec), nz(_nz), mutex(_mtx)
|
|
|
|
{
|
|
|
|
acols = cvCeil(edges.cols * idp), arows = cvCeil(edges.rows * idp);
|
|
|
|
astep = acols + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
~HoughCirclesAccumInvoker() { }
|
|
|
|
|
2018-03-15 21:16:51 +08:00
|
|
|
void operator()(const Range &boundaries) const CV_OVERRIDE
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
|
|
|
Mat accumLocal = Mat(arows + 2, acols + 2, CV_32SC1, Scalar::all(0));
|
|
|
|
int *adataLocal = accumLocal.ptr<int>();
|
2017-12-28 22:23:11 +08:00
|
|
|
NZPointSet nzLocal(nz.positions.rows, nz.positions.cols);
|
2017-11-21 19:18:47 +08:00
|
|
|
int startRow = boundaries.start;
|
|
|
|
int endRow = boundaries.end;
|
|
|
|
int numCols = edges.cols;
|
|
|
|
|
|
|
|
if(edges.isContinuous() && dx.isContinuous() && dy.isContinuous())
|
|
|
|
{
|
|
|
|
numCols *= (boundaries.end - boundaries.start);
|
|
|
|
endRow = boundaries.start + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accumulate circle evidence for each edge pixel
|
|
|
|
for(int y = startRow; y < endRow; ++y )
|
|
|
|
{
|
|
|
|
const uchar* edgeData = edges.ptr<const uchar>(y);
|
|
|
|
const short* dxData = dx.ptr<const short>(y);
|
|
|
|
const short* dyData = dy.ptr<const short>(y);
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
for(; x < numCols; ++x )
|
|
|
|
{
|
|
|
|
#if CV_SIMD128
|
|
|
|
{
|
|
|
|
v_uint8x16 v_zero = v_setzero_u8();
|
|
|
|
|
|
|
|
for(; x <= numCols - 32; x += 32) {
|
|
|
|
v_uint8x16 v_edge1 = v_load(edgeData + x);
|
|
|
|
v_uint8x16 v_edge2 = v_load(edgeData + x + 16);
|
|
|
|
|
|
|
|
v_uint8x16 v_cmp1 = (v_edge1 == v_zero);
|
|
|
|
v_uint8x16 v_cmp2 = (v_edge2 == v_zero);
|
|
|
|
|
|
|
|
unsigned int mask1 = v_signmask(v_cmp1);
|
|
|
|
unsigned int mask2 = v_signmask(v_cmp2);
|
|
|
|
|
|
|
|
mask1 ^= 0x0000ffff;
|
|
|
|
mask2 ^= 0x0000ffff;
|
|
|
|
|
|
|
|
if(mask1)
|
|
|
|
{
|
|
|
|
x += trailingZeros32(mask1);
|
|
|
|
goto _next_step;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mask2)
|
|
|
|
{
|
|
|
|
x += trailingZeros32(mask2 << 16);
|
|
|
|
goto _next_step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for(; x < numCols && !edgeData[x]; ++x)
|
|
|
|
;
|
|
|
|
|
|
|
|
if(x == numCols)
|
|
|
|
continue;
|
|
|
|
#if CV_SIMD128
|
|
|
|
_next_step:
|
|
|
|
#endif
|
|
|
|
float vx, vy;
|
|
|
|
int sx, sy, x0, y0, x1, y1;
|
|
|
|
|
|
|
|
vx = dxData[x];
|
|
|
|
vy = dyData[x];
|
|
|
|
|
|
|
|
if(vx == 0 && vy == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
float mag = std::sqrt(vx*vx+vy*vy);
|
|
|
|
|
|
|
|
if(mag < 1.0f)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Point pt = Point(x % edges.cols, y + x / edges.cols);
|
2017-12-28 22:23:11 +08:00
|
|
|
nzLocal.insert(pt);
|
2017-11-21 19:18:47 +08:00
|
|
|
|
|
|
|
sx = cvRound((vx * idp) * 1024 / mag);
|
|
|
|
sy = cvRound((vy * idp) * 1024 / mag);
|
|
|
|
|
|
|
|
x0 = cvRound((pt.x * idp) * 1024);
|
|
|
|
y0 = cvRound((pt.y * idp) * 1024);
|
|
|
|
|
|
|
|
// Step from min_radius to max_radius in both directions of the gradient
|
|
|
|
for(int k1 = 0; k1 < 2; k1++ )
|
|
|
|
{
|
|
|
|
x1 = x0 + minRadius * sx;
|
|
|
|
y1 = y0 + minRadius * sy;
|
|
|
|
|
|
|
|
for(int r = minRadius; r <= maxRadius; x1 += sx, y1 += sy, r++ )
|
|
|
|
{
|
|
|
|
int x2 = x1 >> 10, y2 = y1 >> 10;
|
|
|
|
if( (unsigned)x2 >= (unsigned)acols ||
|
|
|
|
(unsigned)y2 >= (unsigned)arows )
|
|
|
|
break;
|
|
|
|
|
|
|
|
adataLocal[y2*astep + x2]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sx = -sx; sy = -sy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
{ // TODO Try using TLSContainers
|
|
|
|
AutoLock lock(mutex);
|
|
|
|
accumVec.push_back(accumLocal);
|
|
|
|
nz.insert(nzLocal);
|
|
|
|
}
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Mat &edges, &dx, &dy;
|
|
|
|
int minRadius, maxRadius;
|
|
|
|
float idp;
|
|
|
|
std::vector<Mat>& accumVec;
|
2017-12-28 22:23:11 +08:00
|
|
|
NZPointSet& nz;
|
2017-11-21 19:18:47 +08:00
|
|
|
|
|
|
|
int acols, arows, astep;
|
|
|
|
|
|
|
|
Mutex& mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HoughCirclesFindCentersInvoker : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HoughCirclesFindCentersInvoker(const Mat &_accum, std::vector<int> &_centers, int _accThreshold, Mutex& _mutex) :
|
|
|
|
accum(_accum), centers(_centers), accThreshold(_accThreshold), _lock(_mutex)
|
|
|
|
{
|
|
|
|
acols = accum.cols;
|
|
|
|
arows = accum.rows;
|
|
|
|
adata = accum.ptr<int>();
|
|
|
|
}
|
|
|
|
|
|
|
|
~HoughCirclesFindCentersInvoker() {}
|
|
|
|
|
2018-03-15 21:16:51 +08:00
|
|
|
void operator()(const Range &boundaries) const CV_OVERRIDE
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
|
|
|
int startRow = boundaries.start;
|
|
|
|
int endRow = boundaries.end;
|
|
|
|
std::vector<int> centersLocal;
|
|
|
|
bool singleThread = (boundaries == Range(1, accum.rows - 1));
|
|
|
|
|
|
|
|
startRow = max(1, startRow);
|
|
|
|
endRow = min(arows - 1, endRow);
|
|
|
|
|
|
|
|
//Find possible circle centers
|
|
|
|
for(int y = startRow; y < endRow; ++y )
|
|
|
|
{
|
|
|
|
int x = 1;
|
|
|
|
int base = y * acols + x;
|
|
|
|
|
|
|
|
for(; x < acols - 1; ++x, ++base )
|
|
|
|
{
|
|
|
|
if( adata[base] > accThreshold &&
|
|
|
|
adata[base] > adata[base-1] && adata[base] >= adata[base+1] &&
|
|
|
|
adata[base] > adata[base-acols] && adata[base] >= adata[base+acols] )
|
|
|
|
centersLocal.push_back(base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!centersLocal.empty())
|
|
|
|
{
|
|
|
|
if(singleThread)
|
|
|
|
centers = centersLocal;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AutoLock alock(_lock);
|
|
|
|
centers.insert(centers.end(), centersLocal.begin(), centersLocal.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Mat &accum;
|
|
|
|
std::vector<int> ¢ers;
|
|
|
|
int accThreshold;
|
|
|
|
|
|
|
|
int acols, arows;
|
|
|
|
const int *adata;
|
|
|
|
Mutex& _lock;
|
|
|
|
};
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
template<typename T>
|
|
|
|
static bool CheckDistance(const std::vector<T> &circles, size_t endIdx, const T& circle, float minDist2)
|
2017-12-28 22:23:11 +08:00
|
|
|
{
|
|
|
|
bool goodPoint = true;
|
|
|
|
for (uint j = 0; j < endIdx; ++j)
|
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
T pt = circles[j];
|
2017-12-28 22:23:11 +08:00
|
|
|
float distX = circle[0] - pt[0], distY = circle[1] - pt[1];
|
|
|
|
if (distX * distX + distY * distY < minDist2)
|
|
|
|
{
|
|
|
|
goodPoint = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return goodPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GetCircleCenters(const std::vector<int> ¢ers, std::vector<Vec3f> &circles, int acols, float minDist, float dr)
|
|
|
|
{
|
|
|
|
size_t centerCnt = centers.size();
|
|
|
|
float minDist2 = minDist * minDist;
|
|
|
|
for (size_t i = 0; i < centerCnt; ++i)
|
|
|
|
{
|
|
|
|
int center = centers[i];
|
|
|
|
int y = center / acols;
|
|
|
|
int x = center - y * acols;
|
|
|
|
Vec3f circle = Vec3f((x + 0.5f) * dr, (y + 0.5f) * dr, 0);
|
|
|
|
|
|
|
|
bool goodPoint = CheckDistance(circles, circles.size(), circle, minDist2);
|
|
|
|
if (goodPoint)
|
|
|
|
circles.push_back(circle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
static void GetCircleCenters(const std::vector<int> ¢ers, std::vector<Vec4f> &circles, int acols, float minDist, float dr)
|
|
|
|
{
|
|
|
|
size_t centerCnt = centers.size();
|
|
|
|
float minDist2 = minDist * minDist;
|
|
|
|
for (size_t i = 0; i < centerCnt; ++i)
|
|
|
|
{
|
|
|
|
int center = centers[i];
|
|
|
|
int y = center / acols;
|
|
|
|
int x = center - y * acols;
|
|
|
|
Vec4f circle = Vec4f((x + 0.5f) * dr, (y + 0.5f) * dr, 0, (float)center);
|
|
|
|
|
|
|
|
bool goodPoint = CheckDistance(circles, circles.size(), circle, minDist2);
|
|
|
|
if (goodPoint)
|
|
|
|
circles.push_back(circle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void RemoveOverlaps(std::vector<T>& circles, float minDist)
|
2017-12-28 22:23:11 +08:00
|
|
|
{
|
|
|
|
float minDist2 = minDist * minDist;
|
|
|
|
size_t endIdx = 1;
|
|
|
|
for (size_t i = 1; i < circles.size(); ++i)
|
|
|
|
{
|
2018-05-24 04:42:12 +08:00
|
|
|
T circle = circles[i];
|
2017-12-28 22:23:11 +08:00
|
|
|
if (CheckDistance(circles, endIdx, circle, minDist2))
|
|
|
|
{
|
|
|
|
circles[endIdx] = circle;
|
|
|
|
++endIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
circles.resize(endIdx);
|
|
|
|
}
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
static void CreateCircles(const std::vector<EstimatedCircle>& circlesEst, std::vector<Vec3f>& circles)
|
|
|
|
{
|
|
|
|
std::transform(circlesEst.begin(), circlesEst.end(), std::back_inserter(circles), GetCircle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateCircles(const std::vector<EstimatedCircle>& circlesEst, std::vector<Vec4f>& circles)
|
|
|
|
{
|
|
|
|
std::transform(circlesEst.begin(), circlesEst.end(), std::back_inserter(circles), GetCircle4f);
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
template<class NZPoints>
|
2017-11-21 19:18:47 +08:00
|
|
|
class HoughCircleEstimateRadiusInvoker : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
2017-12-28 22:23:11 +08:00
|
|
|
HoughCircleEstimateRadiusInvoker(const NZPoints &_nz, int _nzSz, const std::vector<int> &_centers, std::vector<EstimatedCircle> &_circlesEst,
|
|
|
|
int _acols, int _accThreshold, int _minRadius, int _maxRadius,
|
|
|
|
float _dp, Mutex& _mutex) :
|
|
|
|
nz(_nz), nzSz(_nzSz), centers(_centers), circlesEst(_circlesEst), acols(_acols), accThreshold(_accThreshold),
|
|
|
|
minRadius(_minRadius), maxRadius(_maxRadius), dr(_dp), _lock(_mutex)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
|
|
|
minRadius2 = (float)minRadius * minRadius;
|
|
|
|
maxRadius2 = (float)maxRadius * maxRadius;
|
|
|
|
centerSz = (int)centers.size();
|
2017-12-28 22:23:11 +08:00
|
|
|
CV_Assert(nzSz > 0);
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
|
2018-04-07 20:59:10 +08:00
|
|
|
~HoughCircleEstimateRadiusInvoker() {}
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
protected:
|
|
|
|
inline int filterCircles(const Point2f& curCenter, float* ddata) const;
|
|
|
|
|
2018-03-15 21:16:51 +08:00
|
|
|
void operator()(const Range &boundaries) const CV_OVERRIDE
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
std::vector<EstimatedCircle> circlesLocal;
|
2017-11-21 19:18:47 +08:00
|
|
|
const int nBinsPerDr = 10;
|
|
|
|
int nBins = cvRound((maxRadius - minRadius)/dr*nBinsPerDr);
|
2017-12-28 22:23:11 +08:00
|
|
|
AutoBuffer<int> bins(nBins);
|
|
|
|
AutoBuffer<float> distBuf(nzSz), distSqrtBuf(nzSz);
|
2018-06-11 06:42:00 +08:00
|
|
|
float *ddata = distBuf.data();
|
|
|
|
float *dSqrtData = distSqrtBuf.data();
|
2017-11-21 19:18:47 +08:00
|
|
|
|
|
|
|
bool singleThread = (boundaries == Range(0, centerSz));
|
|
|
|
int i = boundaries.start;
|
|
|
|
|
|
|
|
// For each found possible center
|
|
|
|
// Estimate radius and check support
|
|
|
|
for(; i < boundaries.end; ++i)
|
|
|
|
{
|
|
|
|
int ofs = centers[i];
|
|
|
|
int y = ofs / acols;
|
|
|
|
int x = ofs - y * acols;
|
|
|
|
|
|
|
|
//Calculate circle's center in pixels
|
|
|
|
Point2f curCenter = Point2f((x + 0.5f) * dr, (y + 0.5f) * dr);
|
2017-12-28 22:23:11 +08:00
|
|
|
int nzCount = filterCircles(curCenter, ddata);
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
int maxCount = 0;
|
|
|
|
float rBest = 0;
|
|
|
|
if(nzCount)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
Mat_<float> distMat(1, nzCount, ddata);
|
|
|
|
Mat_<float> distSqrtMat(1, nzCount, dSqrtData);
|
|
|
|
sqrt(distMat, distSqrtMat);
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2018-06-11 06:42:00 +08:00
|
|
|
memset(bins.data(), 0, sizeof(bins[0])*bins.size());
|
2017-12-28 22:23:11 +08:00
|
|
|
for(int k = 0; k < nzCount; k++)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
int bin = std::max(0, std::min(nBins-1, cvRound((dSqrtData[k] - minRadius)/dr*nBinsPerDr)));
|
|
|
|
bins[bin]++;
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
for(int j = nBins - 1; j > 0; j--)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
if(bins[j])
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
int upbin = j;
|
|
|
|
int curCount = 0;
|
|
|
|
for(; j > upbin - nBinsPerDr && j >= 0; j--)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
curCount += bins[j];
|
|
|
|
}
|
|
|
|
float rCur = (upbin + j)/2.f /nBinsPerDr * dr + minRadius;
|
|
|
|
if((curCount * rBest >= maxCount * rCur) ||
|
|
|
|
(rBest < FLT_EPSILON && curCount >= maxCount))
|
|
|
|
{
|
|
|
|
rBest = rCur;
|
|
|
|
maxCount = curCount;
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
// Check if the circle has enough support
|
|
|
|
if(maxCount > accThreshold)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
circlesLocal.push_back(EstimatedCircle(Vec3f(curCenter.x, curCenter.y, rBest), maxCount));
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
if(!circlesLocal.empty())
|
|
|
|
{
|
|
|
|
std::sort(circlesLocal.begin(), circlesLocal.end(), cmpAccum);
|
|
|
|
if(singleThread)
|
|
|
|
{
|
|
|
|
std::swap(circlesEst, circlesLocal);
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
AutoLock alock(_lock);
|
|
|
|
if (circlesEst.empty())
|
|
|
|
std::swap(circlesEst, circlesLocal);
|
|
|
|
else
|
|
|
|
circlesEst.insert(circlesEst.end(), circlesLocal.begin(), circlesLocal.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const NZPoints &nz;
|
|
|
|
int nzSz;
|
|
|
|
const std::vector<int> ¢ers;
|
|
|
|
std::vector<EstimatedCircle> &circlesEst;
|
|
|
|
int acols, accThreshold, minRadius, maxRadius;
|
|
|
|
float dr;
|
|
|
|
int centerSz;
|
|
|
|
float minRadius2, maxRadius2;
|
|
|
|
Mutex& _lock;
|
|
|
|
};
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
template<>
|
|
|
|
inline int HoughCircleEstimateRadiusInvoker<NZPointList>::filterCircles(const Point2f& curCenter, float* ddata) const
|
|
|
|
{
|
|
|
|
int nzCount = 0;
|
|
|
|
const Point* nz_ = &nz[0];
|
|
|
|
int j = 0;
|
|
|
|
#if CV_SIMD128
|
|
|
|
{
|
|
|
|
const v_float32x4 v_minRadius2 = v_setall_f32(minRadius2);
|
|
|
|
const v_float32x4 v_maxRadius2 = v_setall_f32(maxRadius2);
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
v_float32x4 v_curCenterX = v_setall_f32(curCenter.x);
|
|
|
|
v_float32x4 v_curCenterY = v_setall_f32(curCenter.y);
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
float CV_DECL_ALIGNED(16) rbuf[4];
|
|
|
|
for(; j <= nzSz - 4; j += 4)
|
|
|
|
{
|
|
|
|
v_float32x4 v_nzX, v_nzY;
|
|
|
|
v_load_deinterleave((const float*)&nz_[j], v_nzX, v_nzY); // FIXIT use proper datatype
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
v_float32x4 v_x = v_cvt_f32(v_reinterpret_as_s32(v_nzX));
|
|
|
|
v_float32x4 v_y = v_cvt_f32(v_reinterpret_as_s32(v_nzY));
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
v_float32x4 v_dx = v_x - v_curCenterX;
|
|
|
|
v_float32x4 v_dy = v_y - v_curCenterY;
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
v_float32x4 v_r2 = (v_dx * v_dx) + (v_dy * v_dy);
|
|
|
|
v_float32x4 vmask = (v_minRadius2 <= v_r2) & (v_r2 <= v_maxRadius2);
|
|
|
|
unsigned int mask = v_signmask(vmask);
|
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
v_store_aligned(rbuf, v_r2);
|
|
|
|
if (mask & 1) ddata[nzCount++] = rbuf[0];
|
|
|
|
if (mask & 2) ddata[nzCount++] = rbuf[1];
|
|
|
|
if (mask & 4) ddata[nzCount++] = rbuf[2];
|
|
|
|
if (mask & 8) ddata[nzCount++] = rbuf[3];
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 22:23:11 +08:00
|
|
|
#endif
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
// Estimate best radius
|
|
|
|
for(; j < nzSz; ++j)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
const Point pt = nz_[j];
|
|
|
|
float _dx = curCenter.x - pt.x, _dy = curCenter.y - pt.y;
|
|
|
|
float _r2 = _dx * _dx + _dy * _dy;
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
if(minRadius2 <= _r2 && _r2 <= maxRadius2)
|
|
|
|
{
|
|
|
|
ddata[nzCount++] = _r2;
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 22:23:11 +08:00
|
|
|
return nzCount;
|
|
|
|
}
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
template<>
|
|
|
|
inline int HoughCircleEstimateRadiusInvoker<NZPointSet>::filterCircles(const Point2f& curCenter, float* ddata) const
|
|
|
|
{
|
|
|
|
int nzCount = 0;
|
|
|
|
const Mat_<uchar>& positions = nz.positions;
|
|
|
|
|
|
|
|
const int rOuter = maxRadius + 1;
|
|
|
|
const Range xOuter = Range(std::max(int(curCenter.x - rOuter), 0), std::min(int(curCenter.x + rOuter), positions.cols));
|
|
|
|
const Range yOuter = Range(std::max(int(curCenter.y - rOuter), 0), std::min(int(curCenter.y + rOuter), positions.rows));
|
2017-11-21 19:18:47 +08:00
|
|
|
|
|
|
|
#if CV_SIMD128
|
2017-12-28 22:23:11 +08:00
|
|
|
const int numSIMDPoints = 4;
|
|
|
|
|
|
|
|
const v_float32x4 v_minRadius2 = v_setall_f32(minRadius2);
|
|
|
|
const v_float32x4 v_maxRadius2 = v_setall_f32(maxRadius2);
|
|
|
|
const v_float32x4 v_curCenterX_0123 = v_setall_f32(curCenter.x) - v_float32x4(0.0f, 1.0f, 2.0f, 3.0f);
|
2017-11-21 19:18:47 +08:00
|
|
|
#endif
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
for (int y = yOuter.start; y < yOuter.end; y++)
|
|
|
|
{
|
|
|
|
const uchar* ptr = positions.ptr(y, 0);
|
|
|
|
float dy = curCenter.y - y;
|
|
|
|
float dy2 = dy * dy;
|
|
|
|
|
|
|
|
int x = xOuter.start;
|
|
|
|
#if CV_SIMD128
|
|
|
|
{
|
|
|
|
const v_float32x4 v_dy2 = v_setall_f32(dy2);
|
|
|
|
const v_uint32x4 v_zero_u32 = v_setall_u32(0);
|
|
|
|
float CV_DECL_ALIGNED(16) rbuf[4];
|
|
|
|
for (; x <= xOuter.end - 4; x += numSIMDPoints)
|
|
|
|
{
|
|
|
|
v_uint32x4 v_mask = v_load_expand_q(ptr + x);
|
|
|
|
v_mask = v_mask != v_zero_u32;
|
|
|
|
|
|
|
|
v_float32x4 v_x = v_cvt_f32(v_setall_s32(x));
|
|
|
|
v_float32x4 v_dx = v_x - v_curCenterX_0123;
|
|
|
|
|
|
|
|
v_float32x4 v_r2 = (v_dx * v_dx) + v_dy2;
|
|
|
|
v_float32x4 vmask = (v_minRadius2 <= v_r2) & (v_r2 <= v_maxRadius2) & v_reinterpret_as_f32(v_mask);
|
|
|
|
unsigned int mask = v_signmask(vmask);
|
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
v_store_aligned(rbuf, v_r2);
|
|
|
|
if (mask & 1) ddata[nzCount++] = rbuf[0];
|
|
|
|
if (mask & 2) ddata[nzCount++] = rbuf[1];
|
|
|
|
if (mask & 4) ddata[nzCount++] = rbuf[2];
|
|
|
|
if (mask & 8) ddata[nzCount++] = rbuf[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
for (; x < xOuter.end; x++)
|
|
|
|
{
|
|
|
|
if (ptr[x])
|
|
|
|
{
|
|
|
|
float _dx = curCenter.x - x;
|
|
|
|
float _r2 = _dx * _dx + dy2;
|
|
|
|
if(minRadius2 <= _r2 && _r2 <= maxRadius2)
|
|
|
|
{
|
|
|
|
ddata[nzCount++] = _r2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nzCount;
|
|
|
|
}
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
template <typename CircleType>
|
|
|
|
static void HoughCirclesGradient(InputArray _image, OutputArray _circles,
|
|
|
|
float dp, float minDist,
|
2017-11-21 19:18:47 +08:00
|
|
|
int minRadius, int maxRadius, int cannyThreshold,
|
2017-12-28 22:23:11 +08:00
|
|
|
int accThreshold, int maxCircles, int kernelSize, bool centersOnly)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
|
|
|
CV_Assert(kernelSize == -1 || kernelSize == 3 || kernelSize == 5 || kernelSize == 7);
|
2018-05-24 04:42:12 +08:00
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
dp = max(dp, 1.f);
|
|
|
|
float idp = 1.f/dp;
|
|
|
|
|
|
|
|
Mat edges, dx, dy;
|
|
|
|
|
|
|
|
Sobel(_image, dx, CV_16S, 1, 0, kernelSize, 1, 0, BORDER_REPLICATE);
|
|
|
|
Sobel(_image, dy, CV_16S, 0, 1, kernelSize, 1, 0, BORDER_REPLICATE);
|
|
|
|
Canny(dx, dy, edges, std::max(1, cannyThreshold / 2), cannyThreshold, false);
|
|
|
|
|
|
|
|
Mutex mtx;
|
|
|
|
int numThreads = std::max(1, getNumThreads());
|
|
|
|
std::vector<Mat> accumVec;
|
2017-12-28 22:23:11 +08:00
|
|
|
NZPointSet nz(_image.rows(), _image.cols());
|
2017-11-21 19:18:47 +08:00
|
|
|
parallel_for_(Range(0, edges.rows),
|
|
|
|
HoughCirclesAccumInvoker(edges, dx, dy, minRadius, maxRadius, idp, accumVec, nz, mtx),
|
|
|
|
numThreads);
|
2017-12-28 22:23:11 +08:00
|
|
|
int nzSz = cv::countNonZero(nz.positions);
|
|
|
|
if(nzSz <= 0)
|
2017-11-21 19:18:47 +08:00
|
|
|
return;
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
Mat accum = accumVec[0];
|
2017-11-21 19:18:47 +08:00
|
|
|
for(size_t i = 1; i < accumVec.size(); i++)
|
|
|
|
{
|
|
|
|
accum += accumVec[i];
|
|
|
|
}
|
2017-12-28 22:23:11 +08:00
|
|
|
accumVec.clear();
|
2017-11-21 19:18:47 +08:00
|
|
|
|
|
|
|
std::vector<int> centers;
|
|
|
|
|
|
|
|
// 4 rows when multithreaded because there is a bit overhead
|
|
|
|
// and on the other side there are some row ranges where centers are concentrated
|
|
|
|
parallel_for_(Range(1, accum.rows - 1),
|
|
|
|
HoughCirclesFindCentersInvoker(accum, centers, accThreshold, mtx),
|
|
|
|
(numThreads > 1) ? ((accum.rows - 2) / 4) : 1);
|
|
|
|
|
|
|
|
int centerCnt = (int)centers.size();
|
|
|
|
if(centerCnt == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::sort(centers.begin(), centers.end(), hough_cmp_gt(accum.ptr<int>()));
|
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
std::vector<CircleType> circles;
|
2017-11-21 19:18:47 +08:00
|
|
|
circles.reserve(256);
|
2017-12-28 22:23:11 +08:00
|
|
|
if (centersOnly)
|
|
|
|
{
|
|
|
|
// Just get the circle centers
|
|
|
|
GetCircleCenters(centers, circles, accum.cols, minDist, dp);
|
|
|
|
}
|
|
|
|
else
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
std::vector<EstimatedCircle> circlesEst;
|
|
|
|
if (nzSz < maxRadius * maxRadius)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
// Faster to use a list
|
|
|
|
NZPointList nzList(nzSz);
|
|
|
|
nz.toList(nzList);
|
|
|
|
// One loop iteration per thread if multithreaded.
|
|
|
|
parallel_for_(Range(0, centerCnt),
|
|
|
|
HoughCircleEstimateRadiusInvoker<NZPointList>(nzList, nzSz, centers, circlesEst, accum.cols,
|
|
|
|
accThreshold, minRadius, maxRadius, dp, mtx),
|
|
|
|
numThreads);
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
2017-12-28 22:23:11 +08:00
|
|
|
else
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
// Faster to use a matrix
|
|
|
|
// One loop iteration per thread if multithreaded.
|
|
|
|
parallel_for_(Range(0, centerCnt),
|
|
|
|
HoughCircleEstimateRadiusInvoker<NZPointSet>(nz, nzSz, centers, circlesEst, accum.cols,
|
|
|
|
accThreshold, minRadius, maxRadius, dp, mtx),
|
|
|
|
numThreads);
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
// Sort by accumulator value
|
|
|
|
std::sort(circlesEst.begin(), circlesEst.end(), cmpAccum);
|
2018-05-24 04:42:12 +08:00
|
|
|
|
|
|
|
// Create Circles
|
|
|
|
CreateCircles(circlesEst, circles);
|
2017-12-28 22:23:11 +08:00
|
|
|
RemoveOverlaps(circles, minDist);
|
|
|
|
}
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
if (circles.size() > 0)
|
2017-11-21 19:18:47 +08:00
|
|
|
{
|
2017-12-28 22:23:11 +08:00
|
|
|
int numCircles = std::min(maxCircles, int(circles.size()));
|
2018-05-24 04:42:12 +08:00
|
|
|
Mat(1, numCircles, cv::traits::Type<CircleType>::value, &circles[0]).copyTo(_circles);
|
2017-12-28 22:23:11 +08:00
|
|
|
return;
|
2017-11-21 19:18:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HoughCircles( InputArray _image, OutputArray _circles,
|
|
|
|
int method, double dp, double minDist,
|
|
|
|
double param1, double param2,
|
|
|
|
int minRadius, int maxRadius,
|
|
|
|
int maxCircles, double param3 )
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2017-11-21 19:18:47 +08:00
|
|
|
|
2018-05-24 04:42:12 +08:00
|
|
|
int type = CV_32FC3;
|
|
|
|
if( _circles.fixedType() )
|
|
|
|
{
|
|
|
|
type = _circles.type();
|
|
|
|
CV_CheckType(type, type == CV_32FC3 || type == CV_32FC4, "Wrong type of output circles");
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Assert(!_image.empty() && _image.type() == CV_8UC1 && (_image.isMat() || _image.isUMat()));
|
|
|
|
CV_Assert(_circles.isMat() || _circles.isVector());
|
|
|
|
|
|
|
|
if( dp <= 0 || minDist <= 0 || param1 <= 0 || param2 <= 0)
|
|
|
|
CV_Error( Error::StsOutOfRange, "dp, min_dist, canny_threshold and acc_threshold must be all positive numbers" );
|
|
|
|
|
|
|
|
int cannyThresh = cvRound(param1), accThresh = cvRound(param2), kernelSize = cvRound(param3);
|
|
|
|
|
|
|
|
minRadius = std::max(0, minRadius);
|
|
|
|
|
|
|
|
if(maxCircles < 0)
|
|
|
|
maxCircles = INT_MAX;
|
|
|
|
|
2017-12-28 22:23:11 +08:00
|
|
|
bool centersOnly = (maxRadius < 0);
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
if( maxRadius <= 0 )
|
|
|
|
maxRadius = std::max( _image.rows(), _image.cols() );
|
|
|
|
else if( maxRadius <= minRadius )
|
|
|
|
maxRadius = minRadius + 2;
|
|
|
|
|
|
|
|
switch( method )
|
|
|
|
{
|
|
|
|
case CV_HOUGH_GRADIENT:
|
2018-05-24 04:42:12 +08:00
|
|
|
if (type == CV_32FC3)
|
|
|
|
HoughCirclesGradient<Vec3f>(_image, _circles, (float)dp, (float)minDist,
|
|
|
|
minRadius, maxRadius, cannyThresh,
|
|
|
|
accThresh, maxCircles, kernelSize, centersOnly);
|
|
|
|
else if (type == CV_32FC4)
|
|
|
|
HoughCirclesGradient<Vec4f>(_image, _circles, (float)dp, (float)minDist,
|
|
|
|
minRadius, maxRadius, cannyThresh,
|
|
|
|
accThresh, maxCircles, kernelSize, centersOnly);
|
|
|
|
else
|
|
|
|
CV_Error(Error::StsError, "Internal error");
|
2017-11-21 19:18:47 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CV_Error( Error::StsBadArg, "Unrecognized method id. Actually only CV_HOUGH_GRADIENT is supported." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HoughCircles( InputArray _image, OutputArray _circles,
|
|
|
|
int method, double dp, double minDist,
|
|
|
|
double param1, double param2,
|
|
|
|
int minRadius, int maxRadius )
|
|
|
|
{
|
|
|
|
HoughCircles(_image, _circles, method, dp, minDist, param1, param2, minRadius, maxRadius, -1, 3);
|
|
|
|
}
|
|
|
|
} // \namespace cv
|
2013-02-13 03:16:06 +08:00
|
|
|
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
/* Wrapper function for standard hough transform */
|
|
|
|
CV_IMPL CvSeq*
|
|
|
|
cvHoughLines2( CvArr* src_image, void* lineStorage, int method,
|
|
|
|
double rho, double theta, int threshold,
|
2014-01-11 08:42:25 +08:00
|
|
|
double param1, double param2,
|
|
|
|
double min_theta, double max_theta )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-02-13 03:16:06 +08:00
|
|
|
cv::Mat image = cv::cvarrToMat(src_image);
|
|
|
|
std::vector<cv::Vec2f> l2;
|
|
|
|
std::vector<cv::Vec4i> l4;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
CvMat* mat = 0;
|
|
|
|
CvSeq* lines = 0;
|
|
|
|
CvSeq lines_header;
|
|
|
|
CvSeqBlock lines_block;
|
|
|
|
int lineType, elemSize;
|
|
|
|
int linesMax = INT_MAX;
|
|
|
|
int iparam1, iparam2;
|
|
|
|
|
|
|
|
if( !lineStorage )
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error(cv::Error::StsNullPtr, "NULL destination" );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( rho <= 0 || theta <= 0 || threshold <= 0 )
|
2017-11-21 19:18:47 +08:00
|
|
|
CV_Error( cv::Error::StsOutOfRange, "rho, theta and threshold must be positive" );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( method != CV_HOUGH_PROBABILISTIC )
|
|
|
|
{
|
|
|
|
lineType = CV_32FC2;
|
|
|
|
elemSize = sizeof(float)*2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineType = CV_32SC4;
|
|
|
|
elemSize = sizeof(int)*4;
|
|
|
|
}
|
|
|
|
|
2017-07-01 23:43:48 +08:00
|
|
|
bool isStorage = isStorageOrMat(lineStorage);
|
|
|
|
|
|
|
|
if( isStorage )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
lines = cvCreateSeq( lineType, sizeof(CvSeq), elemSize, (CvMemStorage*)lineStorage );
|
|
|
|
}
|
2017-07-01 23:43:48 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
mat = (CvMat*)lineStorage;
|
|
|
|
|
|
|
|
if( !CV_IS_MAT_CONT( mat->type ) || (mat->rows != 1 && mat->cols != 1) )
|
|
|
|
CV_Error( CV_StsBadArg,
|
|
|
|
"The destination matrix should be continuous and have a single row or a single column" );
|
|
|
|
|
|
|
|
if( CV_MAT_TYPE( mat->type ) != lineType )
|
|
|
|
CV_Error( CV_StsBadArg,
|
|
|
|
"The destination matrix data type is inappropriate, see the manual" );
|
|
|
|
|
|
|
|
lines = cvMakeSeqHeaderForArray( lineType, sizeof(CvSeq), elemSize, mat->data.ptr,
|
|
|
|
mat->rows + mat->cols - 1, &lines_header, &lines_block );
|
|
|
|
linesMax = lines->total;
|
|
|
|
cvClearSeq( lines );
|
|
|
|
}
|
2012-06-15 21:04:17 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
iparam1 = cvRound(param1);
|
|
|
|
iparam2 = cvRound(param2);
|
|
|
|
|
|
|
|
switch( method )
|
|
|
|
{
|
|
|
|
case CV_HOUGH_STANDARD:
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesStandard( image, l2, CV_32FC2, (float)rho,
|
|
|
|
(float)theta, threshold, linesMax, min_theta, max_theta );
|
2013-02-13 03:16:06 +08:00
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
case CV_HOUGH_MULTI_SCALE:
|
2018-05-24 04:42:12 +08:00
|
|
|
HoughLinesSDiv( image, l2, CV_32FC2, (float)rho, (float)theta,
|
|
|
|
threshold, iparam1, iparam2, linesMax, min_theta, max_theta );
|
2013-02-13 03:16:06 +08:00
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
case CV_HOUGH_PROBABILISTIC:
|
2013-02-13 03:16:06 +08:00
|
|
|
HoughLinesProbabilistic( image, (float)rho, (float)theta,
|
|
|
|
threshold, iparam1, iparam2, l4, linesMax );
|
|
|
|
break;
|
2010-05-12 01:44:00 +08:00
|
|
|
default:
|
|
|
|
CV_Error( CV_StsBadArg, "Unrecognized method id" );
|
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
int nlines = (int)(l2.size() + l4.size());
|
|
|
|
|
2017-07-01 23:43:48 +08:00
|
|
|
if( !isStorage )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
if( mat->cols > mat->rows )
|
2013-02-13 03:16:06 +08:00
|
|
|
mat->cols = nlines;
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
2013-02-13 03:16:06 +08:00
|
|
|
mat->rows = nlines;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-02-13 03:16:06 +08:00
|
|
|
if( nlines )
|
|
|
|
{
|
|
|
|
cv::Mat lx = method == CV_HOUGH_STANDARD || method == CV_HOUGH_MULTI_SCALE ?
|
|
|
|
cv::Mat(nlines, 1, CV_32FC2, &l2[0]) : cv::Mat(nlines, 1, CV_32SC4, &l4[0]);
|
|
|
|
|
2017-07-01 23:43:48 +08:00
|
|
|
if (isStorage)
|
2013-02-13 03:16:06 +08:00
|
|
|
{
|
2017-07-01 23:43:48 +08:00
|
|
|
cvSeqPushMulti(lines, lx.ptr(), nlines);
|
2013-02-13 03:16:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-01 23:43:48 +08:00
|
|
|
cv::Mat dst(nlines, 1, lx.type(), mat->data.ptr);
|
|
|
|
lx.copyTo(dst);
|
2013-02-13 03:16:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-01 23:43:48 +08:00
|
|
|
if( isStorage )
|
|
|
|
return lines;
|
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CV_IMPL CvSeq*
|
|
|
|
cvHoughCircles( CvArr* src_image, void* circle_storage,
|
|
|
|
int method, double dp, double min_dist,
|
|
|
|
double param1, double param2,
|
|
|
|
int min_radius, int max_radius )
|
|
|
|
{
|
2017-11-21 19:18:47 +08:00
|
|
|
CvSeq* circles = NULL;
|
2010-05-12 01:44:00 +08:00
|
|
|
int circles_max = INT_MAX;
|
2017-11-21 19:18:47 +08:00
|
|
|
cv::Mat src = cv::cvarrToMat(src_image), circles_mat;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( !circle_storage )
|
|
|
|
CV_Error( CV_StsNullPtr, "NULL destination" );
|
|
|
|
|
2017-07-01 23:43:48 +08:00
|
|
|
bool isStorage = isStorageOrMat(circle_storage);
|
|
|
|
|
|
|
|
if(isStorage)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
circles = cvCreateSeq( CV_32FC3, sizeof(CvSeq),
|
|
|
|
sizeof(float)*3, (CvMemStorage*)circle_storage );
|
|
|
|
}
|
2017-07-01 23:43:48 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2017-11-21 19:18:47 +08:00
|
|
|
CvSeq circles_header;
|
|
|
|
CvSeqBlock circles_block;
|
|
|
|
CvMat *mat = (CvMat*)circle_storage;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( !CV_IS_MAT_CONT( mat->type ) || (mat->rows != 1 && mat->cols != 1) ||
|
|
|
|
CV_MAT_TYPE(mat->type) != CV_32FC3 )
|
|
|
|
CV_Error( CV_StsBadArg,
|
2017-11-21 19:18:47 +08:00
|
|
|
"The destination matrix should be continuous and have a single row or a single column" );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
circles = cvMakeSeqHeaderForArray( CV_32FC3, sizeof(CvSeq), sizeof(float)*3,
|
|
|
|
mat->data.ptr, mat->rows + mat->cols - 1, &circles_header, &circles_block );
|
|
|
|
circles_max = circles->total;
|
|
|
|
cvClearSeq( circles );
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:18:47 +08:00
|
|
|
cv::HoughCircles(src, circles_mat, method, dp, min_dist, param1, param2, min_radius, max_radius, circles_max, 3);
|
|
|
|
cvSeqPushMulti(circles, circles_mat.data, (int)circles_mat.total());
|
|
|
|
return circles;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|