mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-22 07:31:26 +08:00
added a calculation of the Fast Hough Transform of an image
This commit is contained in:
165
modules/ximgproc/include/fast_hough_transform.hpp
Normal file
165
modules/ximgproc/include/fast_hough_transform.hpp
Normal file
@@ -0,0 +1,165 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2015, Smart Engines Ltd, all rights reserved.
|
||||
// Copyright (C) 2015, Institute for Information Transmission Problems of the Russian Academy of Sciences (Kharkevich Institute), all rights reserved.
|
||||
// Copyright (C) 2015, Dmitry Nikolaev, Simon Karpenko, Michail Aliev, Elena Kuznetsova, all rights reserved.
|
||||
// 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.
|
||||
//
|
||||
// * 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.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#ifndef __OPENCV_FAST_HOUGH_TRANSFORM_HPP__
|
||||
#define __OPENCV_FAST_HOUGH_TRANSFORM_HPP__
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include "opencv2/core.hpp"
|
||||
|
||||
|
||||
namespace cv { namespace ximgproc {
|
||||
|
||||
/**
|
||||
* @brief Specifies the part of Hough space to calculate
|
||||
* @details The enum specifies the part of Hough space to calculate. Each
|
||||
* member specifies primarily direction of lines (horizontal or vertical)
|
||||
* and the direction of angle changes.
|
||||
* Direction of angle changes is from multiples of 90 to odd multiples of 45.
|
||||
* The image considered to be written top-down and left-to-right.
|
||||
* Angles are started from vertical line and go clockwise.
|
||||
* Separate quarters and halves are written in orientation they should be in
|
||||
* full Hough space.
|
||||
*/
|
||||
enum AngleRangeOption
|
||||
{
|
||||
ARO_0_45 = 0, //< Vertical primarily direction and clockwise angle changes
|
||||
ARO_45_90 = 1, //< Horizontal primarily direction and counterclockwise angle changes
|
||||
ARO_90_135 = 2, //< Horizontal primarily direction and clockwise angle changes
|
||||
ARO_315_0 = 3, //< Vertical primarily direction and counterclockwise angle changes
|
||||
ARO_315_45 = 4, //< Vertical primarily direction
|
||||
ARO_45_135 = 5, //< Horizontal primarily direction
|
||||
ARO_315_135 = 6, //< Full set of directions
|
||||
ARO_CTR_HOR = 7, //< 90 +/- atan(0.5), interval approximately from 64.5 to 116.5 degrees.
|
||||
//< It is used for calculating Fast Hough Transform for images skewed by atan(0.5).
|
||||
ARO_CTR_VER = 8 //< +/- atan(0.5), interval approximately from 333.5(-26.5) to 26.5 degrees
|
||||
//< It is used for calculating Fast Hough Transform for images skewed by atan(0.5).
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Specifies binary operations.
|
||||
* @details The enum specifies binary operations, that is such ones which involve
|
||||
* two operands. Formally, a binary operation @f$ f @f$ on a set @f$ S @f$
|
||||
* is a binary relation that maps elements of the Cartesian product
|
||||
* @f$ S \times S @f$ to @f$ S @f$:
|
||||
* @f[ f: S \times S \to S @f]
|
||||
* @ingroup MinUtils_MathOper
|
||||
*/
|
||||
enum HoughOp
|
||||
{
|
||||
FHT_MIN = 0, //< Binary minimum operation. The constant specifies the binary minimum operation
|
||||
//< @f$ f @f$ that is defined as follows: @f[ f(x, y) = \min(x, y) @f]
|
||||
FHT_MAX = 1, //< Binary maximum operation. The constant specifies the binary maximum operation
|
||||
//< @f$ f @f$ that is defined as follows: @f[ f(x, y) = \max(x, y) @f]
|
||||
FHT_ADD = 2, //< Binary addition operation. The constant specifies the binary addition operation
|
||||
//< @f$ f @f$ that is defined as follows: @f[ f(x, y) = x + y @f]
|
||||
FHT_AVE = 3 //< Binary average operation. The constant specifies the binary average operation
|
||||
//< @f$ f @f$ that is defined as follows: @f[ f(x, y) = \frac{x + y}{2} @f]
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Specifies to do or not to do skewing of Hough transform image
|
||||
* @details The enum specifies to do or not to do skewing of Hough transform image
|
||||
* so it would be no cycling in Hough transform image through borders of image.
|
||||
*/
|
||||
enum HoughDeskewOption
|
||||
{
|
||||
HDO_RAW = 0, //< Use raw cyclic image
|
||||
HDO_DESKEW = 1 //< Prepare deskewed image
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Specifies the degree of rules validation.
|
||||
* @details The enum specifies the degree of rules validation. This can be used,
|
||||
* for example, to choose a proper way of input arguments validation.
|
||||
*/
|
||||
typedef enum {
|
||||
RO_STRICT = 0x00, ///< Validate each rule in a proper way.
|
||||
RO_IGNORE_BORDERS = 0x01, ///< Skip validations of image borders.
|
||||
} RulesOption;
|
||||
|
||||
/**
|
||||
* @brief Calculates 2D Fast Hough transform of an image.
|
||||
* @param dst The destination image, result of transformation.
|
||||
* @param src The source (input) image.
|
||||
* @param dstMatDepth The depth of destination image
|
||||
* @param operation The operation to be applied, see cv::HoughOp
|
||||
* @param angleRange The part of Hough space to calculate, see cv::AngleRangeOption
|
||||
* @param makeSkew Specifies to do or not to do image skewing, see cv::HoughDeskewOption
|
||||
*
|
||||
* The function calculates the fast Hough transform for full, half or quarter
|
||||
* range of angles.
|
||||
*/
|
||||
CV_EXPORTS_W void FastHoughTransform( InputArray src,
|
||||
OutputArray dst,
|
||||
int dstMatDepth,
|
||||
int angleRange = ARO_315_135,
|
||||
int op = FHT_ADD,
|
||||
int makeSkew = HDO_DESKEW );
|
||||
|
||||
/**
|
||||
* @brief Calculates coordinates of line segment corresponded by point in Hough space.
|
||||
* @param line Coordinates of line segment corresponded by point in Hough space.
|
||||
* @param houghPoint Point in Hough space.
|
||||
* @param srcImgInfo The source (input) image of Hough transform.
|
||||
* @param angleRange The part of Hough space where point is situated, see cv::AngleRangeOption
|
||||
* @param makeSkew Specifies to do or not to do image skewing, see cv::HoughDeskewOption
|
||||
* @param rules Specifies strictness of line segment calculating, see cv::RulesOption
|
||||
* @remarks If rules parameter set to RO_STRICT
|
||||
then returned line cut along the border of source image.
|
||||
* @remarks If rules parameter set to RO_WEAK then in case of point, which belongs
|
||||
the incorrect part of Hough image, returned line will not intersect source image.
|
||||
*
|
||||
* The function calculates coordinates of line segment corresponded by point in Hough space.
|
||||
*/
|
||||
CV_EXPORTS_W void HoughPoint2Line( OutputArray line,
|
||||
const Point &houghPoint,
|
||||
const Mat &srcImgInfo,
|
||||
int angleRange = ARO_315_135,
|
||||
int makeSkew = HDO_DESKEW,
|
||||
int rules = RO_IGNORE_BORDERS );
|
||||
|
||||
} }// namespace cv::ximgproc
|
||||
|
||||
#endif //__cplusplus
|
||||
#endif //__OPENCV_FAST_HOUGH_TRANSFORM_HPP__
|
91
modules/ximgproc/perf/perf_fast_hough_transform.cpp
Normal file
91
modules/ximgproc/perf/perf_fast_hough_transform.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2015, Smart Engines Ltd, all rights reserved.
|
||||
// Copyright (C) 2015, Institute for Information Transmission Problems of the Russian Academy of Sciences (Kharkevich Institute), all rights reserved.
|
||||
// Copyright (C) 2015, Dmitry Nikolaev, Simon Karpenko, Michail Aliev, Elena Kuznetsova, all rights reserved.
|
||||
// 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.
|
||||
//
|
||||
// * 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.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "perf_precomp.hpp"
|
||||
#include "fast_hough_transform.hpp"
|
||||
|
||||
namespace cvtest {
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
using namespace cv::ximgproc;
|
||||
using namespace perf;
|
||||
using namespace testing;
|
||||
|
||||
using std::tr1::make_tuple;
|
||||
using std::tr1::get;
|
||||
|
||||
typedef std::tr1::tuple<Size, MatType, MatDepth> srcSize_srcType_dstDepth_t;
|
||||
typedef perf::TestBaseWithParam<srcSize_srcType_dstDepth_t>
|
||||
srcSize_srcType_dstDepth;
|
||||
|
||||
#define ALL_MAT_DEPHTS CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F
|
||||
|
||||
PERF_TEST_P(srcSize_srcType_dstDepth, FastHoughTransform,
|
||||
testing::Combine(
|
||||
testing::Values(TYPICAL_MAT_SIZES),
|
||||
testing::Values(TYPICAL_MAT_TYPES),
|
||||
testing::Values(ALL_MAT_DEPHTS)
|
||||
)
|
||||
)
|
||||
{
|
||||
Size srcSize = get<0>(GetParam());
|
||||
int srcType = get<1>(GetParam());
|
||||
int dstDepth = get<2>(GetParam());
|
||||
|
||||
Mat src(srcSize, srcType);
|
||||
Mat fht;
|
||||
|
||||
declare.in(src, WARMUP_RNG);
|
||||
|
||||
TEST_CYCLE_N(3)
|
||||
{
|
||||
FastHoughTransform(src, fht, dstDepth);
|
||||
}
|
||||
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
#undef ALL_MAT_DEPHTS
|
||||
|
||||
} // namespace cvtest
|
333
modules/ximgproc/samples/fast_hough_transform.cpp
Normal file
333
modules/ximgproc/samples/fast_hough_transform.cpp
Normal file
@@ -0,0 +1,333 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2015, Smart Engines Ltd, all rights reserved.
|
||||
// Copyright (C) 2015, Institute for Information Transmission Problems of the Russian Academy of Sciences (Kharkevich Institute), all rights reserved.
|
||||
// Copyright (C) 2015, Dmitry Nikolaev, Simon Karpenko, Michail Aliev, Elena Kuznetsova, all rights reserved.
|
||||
// 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.
|
||||
//
|
||||
// * 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.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/core/utility.hpp>
|
||||
|
||||
#include <opencv2/ximgproc.hpp>
|
||||
|
||||
#include "fast_hough_transform.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
|
||||
using namespace cv;
|
||||
using namespace cv::ximgproc;
|
||||
using namespace std;
|
||||
|
||||
static void help()
|
||||
{
|
||||
cout << "\nThis program demonstrates line finding with the Fast Hough transform.\n"
|
||||
"Usage:\n"
|
||||
"./fasthoughtransform\n"
|
||||
"<image_name>, default is '../../../samples/data/building.jpg'\n"
|
||||
"<fht_image_depth>, default is " << CV_32S << "\n"
|
||||
"<fht_angle_range>, default is " << 6 << " (@see cv::AngleRangeOption)\n"
|
||||
"<fht_operator>, default is " << 2 << " (@see cv::HoughOp)\n"
|
||||
"<fht_makeskew>, default is " << 1 << "(@see cv::HoughDeskewOption)" << endl;
|
||||
}
|
||||
|
||||
static bool parseArgs(int argc, const char **argv,
|
||||
Mat &img,
|
||||
int &houghDepth,
|
||||
int &houghAngleRange,
|
||||
int &houghOperator,
|
||||
int &houghSkew)
|
||||
{
|
||||
if (argc > 6)
|
||||
{
|
||||
cout << "Too many arguments" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *filename = argc >= 2 ? argv[1]
|
||||
: "../../../samples/data/building.jpg";
|
||||
img = imread(filename, 0);
|
||||
if (img.empty())
|
||||
{
|
||||
cout << "Failed to load image from '" << filename << "'" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
houghDepth = argc >= 3 ? atoi(argv[2]) : CV_32S;
|
||||
houghAngleRange = argc >= 4 ? atoi(argv[3]) : 6;//ARO_315_135
|
||||
houghOperator = argc >= 5 ? atoi(argv[4]) : 2;//FHT_ADD
|
||||
houghSkew = argc >= 6 ? atoi(argv[5]) : 1;//HDO_DESKEW
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool getEdges(const Mat &src, Mat &dst)
|
||||
{
|
||||
Mat ucharSingleSrc;
|
||||
src.convertTo(ucharSingleSrc, CV_8UC1);
|
||||
|
||||
Canny(ucharSingleSrc, dst, 50, 200, 3);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool fht(const Mat &src, Mat &dst,
|
||||
int dstDepth, int angleRange, int op, int skew)
|
||||
{
|
||||
clock_t clocks = clock();
|
||||
//for (int i = 0; i < 100; ++i)
|
||||
//{
|
||||
FastHoughTransform(src, dst, dstDepth, angleRange, op, skew);
|
||||
//}
|
||||
clocks = clock() - clocks;
|
||||
double secs = (double)clocks / CLOCKS_PER_SEC;
|
||||
cout << std::setprecision(2) << "FastHoughTransform finished in " << secs
|
||||
<< " seconds" << endl;
|
||||
|
||||
//clocks = clock();
|
||||
//for (int i = 0; i < 100; ++i)
|
||||
//{
|
||||
// vector<Vec2f> s_lines;
|
||||
// HoughLines(src, s_lines, 1, CV_PI/360, 100, 0, 0);
|
||||
//}
|
||||
//clocks = clock() - clocks;
|
||||
//secs = (double)clocks / CLOCKS_PER_SEC;
|
||||
//std::cout << std::setprecision(2) << "HoughLines finished in " << secs << " seconds" << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool rel(pair<T, Point> const &a, pair<T, Point> const &b)
|
||||
{
|
||||
return a.first > b.first;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool incIfGreater(const T& a, const T& b, int *value)
|
||||
{
|
||||
if (!value || a < b)
|
||||
return false;
|
||||
if (a > b)
|
||||
++(*value);
|
||||
return true;
|
||||
}
|
||||
|
||||
static const int MAX_LEN = 10000;
|
||||
|
||||
template<typename T>
|
||||
bool getLocalExtr(vector<Vec4i> &lines,
|
||||
const Mat &src,
|
||||
const Mat &fht,
|
||||
float minWeight,
|
||||
int maxCount)
|
||||
{
|
||||
vector<pair<T, Point> > weightedPoints;
|
||||
for (int y = 0; y < fht.rows; ++y)
|
||||
{
|
||||
if (weightedPoints.size() > MAX_LEN)
|
||||
break;
|
||||
|
||||
T const *pLine = (T *)fht.ptr(max(y - 1, 0));
|
||||
T const *cLine = (T *)fht.ptr(y);
|
||||
T const *nLine = (T *)fht.ptr(min(y + 1, fht.rows - 1));
|
||||
|
||||
for (int x = 0; x < fht.cols; ++x)
|
||||
{
|
||||
if (weightedPoints.size() > MAX_LEN)
|
||||
break;
|
||||
|
||||
T const value = cLine[x];
|
||||
if (value >= minWeight)
|
||||
{
|
||||
int isLocalMax = 0;
|
||||
for (int xx = max(x - 1, 0);
|
||||
xx <= min(x + 1, fht.cols - 1);
|
||||
++xx)
|
||||
{
|
||||
if (!incIfGreater(value, pLine[xx], &isLocalMax) ||
|
||||
!incIfGreater(value, cLine[xx], &isLocalMax) ||
|
||||
!incIfGreater(value, nLine[xx], &isLocalMax))
|
||||
{
|
||||
isLocalMax = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isLocalMax > 0)
|
||||
weightedPoints.push_back(make_pair(value, Point(x, y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (weightedPoints.empty())
|
||||
return true;
|
||||
|
||||
sort(weightedPoints.begin(), weightedPoints.end(), &rel<T>);
|
||||
weightedPoints.resize(min(static_cast<int>(weightedPoints.size()),
|
||||
maxCount));
|
||||
|
||||
for (size_t i = 0; i < weightedPoints.size(); ++i)
|
||||
{
|
||||
Vec4i houghLine(0, 0, 0, 0);
|
||||
HoughPoint2Line(houghLine, weightedPoints[i].second, src);
|
||||
lines.push_back(houghLine);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool getLocalExtr(vector<Vec4i> &lines,
|
||||
const Mat &src,
|
||||
const Mat &fht,
|
||||
float minWeight,
|
||||
int maxCount)
|
||||
{
|
||||
int const depth = CV_MAT_DEPTH(fht.type());
|
||||
switch (depth)
|
||||
{
|
||||
case 0:
|
||||
return getLocalExtr<uchar>(lines, src, fht, minWeight, maxCount);
|
||||
case 1:
|
||||
return getLocalExtr<schar>(lines, src, fht, minWeight, maxCount);
|
||||
case 2:
|
||||
return getLocalExtr<ushort>(lines, src, fht, minWeight, maxCount);
|
||||
case 3:
|
||||
return getLocalExtr<short>(lines, src, fht, minWeight, maxCount);
|
||||
case 4:
|
||||
return getLocalExtr<int>(lines, src, fht, minWeight, maxCount);
|
||||
case 5:
|
||||
return getLocalExtr<float>(lines, src, fht, minWeight, maxCount);
|
||||
case 6:
|
||||
return getLocalExtr<double>(lines, src, fht, minWeight, maxCount);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void rescale(Mat const &src, Mat &dst,
|
||||
int const maxHeight=500,
|
||||
int const maxWidth = 1000)
|
||||
{
|
||||
double scale = min(min(static_cast<double>(maxWidth) / src.cols,
|
||||
static_cast<double>(maxHeight) / src.rows), 1.0);
|
||||
resize(src, dst, Size(), scale, scale, INTER_LINEAR);
|
||||
}
|
||||
|
||||
static void showHumanReadableImg(string const &name, Mat const &img)
|
||||
{
|
||||
Mat ucharImg;
|
||||
img.convertTo(ucharImg, CV_MAKETYPE(CV_8U, img.channels()));
|
||||
rescale(ucharImg, ucharImg);
|
||||
imshow(name, ucharImg);
|
||||
}
|
||||
|
||||
static void showFht(Mat const &fht)
|
||||
{
|
||||
double minv(0), maxv(0);
|
||||
minMaxLoc(fht, &minv, &maxv);
|
||||
Mat ucharFht;
|
||||
fht.convertTo(ucharFht, CV_MAKETYPE(CV_8U, fht.channels()),
|
||||
255.0 / (maxv + minv), minv / (maxv + minv));
|
||||
rescale(ucharFht, ucharFht);
|
||||
imshow("fast hough transform", ucharFht);
|
||||
}
|
||||
|
||||
static void showLines(Mat const &src, vector<Vec4i> const &lines)
|
||||
{
|
||||
Mat bgrSrc;
|
||||
cvtColor(src, bgrSrc, COLOR_GRAY2BGR);
|
||||
|
||||
for (size_t i = 0; i < lines.size(); ++i)
|
||||
{
|
||||
Vec4i const &l = lines[i];
|
||||
line(bgrSrc, Point(l[0], l[1]), Point(l[2], l[3]),
|
||||
Scalar(0, 0, 255), 1, LINE_AA);
|
||||
}
|
||||
|
||||
rescale(bgrSrc, bgrSrc);
|
||||
imshow("lines", bgrSrc);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
Mat src;
|
||||
int depth(0);
|
||||
int angleRange(0);
|
||||
int op(0);
|
||||
int skew(0);
|
||||
|
||||
if (!parseArgs(argc, argv, src, depth, angleRange, op, skew))
|
||||
{
|
||||
help();
|
||||
return -1;
|
||||
}
|
||||
showHumanReadableImg("src", src);
|
||||
|
||||
Mat canny;
|
||||
if (!getEdges(src, canny))
|
||||
{
|
||||
cout << "Failed to select canny edges";
|
||||
return -2;
|
||||
}
|
||||
showHumanReadableImg("canny", canny);
|
||||
|
||||
Mat hough;
|
||||
if (!fht(canny, hough, depth, angleRange, op, skew))
|
||||
{
|
||||
cout << "Failed to compute Fast Hough Transform";
|
||||
return -2;
|
||||
}
|
||||
showFht(hough);
|
||||
|
||||
vector<Vec4i> lines;
|
||||
if (!getLocalExtr(lines, canny, hough,
|
||||
static_cast<float>(255 * 0.3 * min(src.rows, src.cols)),
|
||||
50))
|
||||
{
|
||||
cout << "Failed to find local maximums on FHT image";
|
||||
return -2;
|
||||
}
|
||||
showLines(canny, lines);
|
||||
|
||||
waitKey();
|
||||
|
||||
return 0;
|
||||
}
|
1031
modules/ximgproc/src/fast_hough_transform.cpp
Normal file
1031
modules/ximgproc/src/fast_hough_transform.cpp
Normal file
File diff suppressed because it is too large
Load Diff
472
modules/ximgproc/test/test_fast_hough_transform.cpp
Normal file
472
modules/ximgproc/test/test_fast_hough_transform.cpp
Normal file
@@ -0,0 +1,472 @@
|
||||
/*M///////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//
|
||||
// License Agreement
|
||||
// For Open Source Computer Vision Library
|
||||
//
|
||||
// Copyright (C) 2015, Smart Engines Ltd, all rights reserved.
|
||||
// Copyright (C) 2015, Institute for Information Transmission Problems of the Russian Academy of Sciences (Kharkevich Institute), all rights reserved.
|
||||
// Copyright (C) 2015, Dmitry Nikolaev, Simon Karpenko, Michail Aliev, Elena Kuznetsova, all rights reserved.
|
||||
// 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.
|
||||
//
|
||||
// * 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.
|
||||
//
|
||||
//M*/
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "fast_hough_transform.hpp"
|
||||
|
||||
namespace cvtest
|
||||
{
|
||||
using namespace cv;
|
||||
using namespace cv::ximgproc;
|
||||
using namespace std;
|
||||
using namespace testing;
|
||||
|
||||
using std::tr1::make_tuple;
|
||||
using std::tr1::get;
|
||||
|
||||
//----------------------utils---------------------------------------------------
|
||||
|
||||
template <typename T> struct Eps
|
||||
{
|
||||
static T get() { return 1; }
|
||||
};
|
||||
template <> struct Eps<float> { static float get() { return float(1e-3); } };
|
||||
template <> struct Eps<double> { static double get() { return 1e-6; } };
|
||||
|
||||
template <typename T> struct MinPos
|
||||
{
|
||||
static T get() { return Eps<T>::get(); }
|
||||
};
|
||||
|
||||
template <typename T> struct Max { static T get()
|
||||
{
|
||||
return saturate_cast<T>(numeric_limits<T>::max()); }
|
||||
};
|
||||
|
||||
template <typename T> struct Rand
|
||||
{
|
||||
static T get(T _min = MinPos<T>::get(), T _max = Max<T>::get())
|
||||
{
|
||||
RNG& rng = TS::ptr()->get_rng();
|
||||
return saturate_cast<T>(rng.uniform(int(std::max(MinPos<T>::get(),
|
||||
_min)),
|
||||
int(std::min(Max<T>::get(),
|
||||
_max))));
|
||||
}
|
||||
};
|
||||
template <> struct Rand <float>
|
||||
{
|
||||
static float get(float _min = MinPos<float>::get(),
|
||||
float _max = Max<float>::get())
|
||||
{
|
||||
RNG& rng = TS::ptr()->get_rng();
|
||||
return rng.uniform(std::max(MinPos<float>::get(), _min),
|
||||
std::min(Max<float>::get(), _max));
|
||||
}
|
||||
};
|
||||
template <> struct Rand <double>
|
||||
{
|
||||
static double get(double _min = MinPos<double>::get(),
|
||||
double _max = Max<double>::get())
|
||||
{
|
||||
RNG& rng = TS::ptr()->get_rng();
|
||||
return rng.uniform(std::max(MinPos<double>::get(), _min),
|
||||
std::min(Max<double>::get(), _max));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T> struct Eq
|
||||
{
|
||||
static bool get(T a, T b)
|
||||
{
|
||||
return a < b ? b - a < Eps<T>::get() : a - b < Eps<T>::get();
|
||||
}
|
||||
};
|
||||
|
||||
//----------------------TestFHT-------------------------------------------------
|
||||
class TestFHT
|
||||
{
|
||||
public:
|
||||
TestFHT() : ts(TS::ptr()) {}
|
||||
|
||||
void run_n_tests(int depth,
|
||||
int channels,
|
||||
int pts_count,
|
||||
int n_per_test);
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
int run_n_tests_t(int depth,
|
||||
int channels,
|
||||
int pts_count,
|
||||
int n_per_test);
|
||||
|
||||
template <typename T>
|
||||
int run_test(int depth,
|
||||
int channels,
|
||||
int pts_count);
|
||||
|
||||
template <typename T>
|
||||
int put_random_points(Mat &img,
|
||||
int count,
|
||||
vector<Point> &pts);
|
||||
|
||||
int run_func(Mat const&src,
|
||||
Mat& fht);
|
||||
|
||||
template <typename T>
|
||||
int validate_test_results(Mat const &fht,
|
||||
Mat const &src,
|
||||
vector<Point> const& pts);
|
||||
|
||||
template <typename T> int validate_sum(Mat const& src, Mat const& fht);
|
||||
int validate_point(Mat const& fht, vector<Point> const &pts);
|
||||
int validate_line(Mat const& fht, Mat const& src, vector<Point> const& pts);
|
||||
|
||||
private:
|
||||
TS *ts;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
int TestFHT::put_random_points(Mat &img, int count, vector<Point> &pts)
|
||||
{
|
||||
int code = TS::OK;
|
||||
|
||||
pts.resize(count, Point(-1, -1));
|
||||
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
RNG rng = ts->get_rng();
|
||||
Point const pt(rng.uniform(0, img.cols),
|
||||
rng.uniform(0, img.rows));
|
||||
pts[i] = pt;
|
||||
|
||||
for (int c = 0; c < img.channels(); ++c)
|
||||
{
|
||||
T color = Rand<T>::get(MinPos<T>::get(),
|
||||
T(Max<T>::get() / count));
|
||||
|
||||
T *img_line = (T*)(img.data + img.step * pt.y);
|
||||
img_line[pt.x * img.channels() + c] = color;
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int TestFHT::validate_sum(Mat const& src, Mat const& fht)
|
||||
{
|
||||
int const channels = src.channels();
|
||||
if (fht.channels() != channels)
|
||||
return TS::FAIL_BAD_ARG_CHECK;
|
||||
|
||||
|
||||
vector<Mat> src_channels(channels);
|
||||
split(src, src_channels);
|
||||
vector<Mat> fht_channels(channels);
|
||||
split(fht, fht_channels);
|
||||
|
||||
for (int c = 0; c < channels; ++c)
|
||||
{
|
||||
T const src_sum = saturate_cast<T>(sum(src_channels[c]).val[0]);
|
||||
for (int y = 0; y < fht.rows; ++y)
|
||||
{
|
||||
T const fht_sum = saturate_cast<T>(sum(fht_channels[c].row(y)).val[0]);
|
||||
if (!Eq<T>::get(src_sum, fht_sum))
|
||||
{
|
||||
ts->printf(TS::LOG,
|
||||
"The sum of column #%d of channel #%d of the fast "
|
||||
"hough transform result and the sum of source image"
|
||||
" mismatch (=%g, should be =%g)\n",
|
||||
y, c, (float)fht_sum, (float)src_sum);
|
||||
return TS::FAIL_BAD_ACCURACY;
|
||||
}
|
||||
}
|
||||
}
|
||||
return TS::OK;
|
||||
}
|
||||
|
||||
int TestFHT::validate_point(Mat const& fht,
|
||||
vector<Point> const &pts)
|
||||
{
|
||||
if (pts.empty())
|
||||
return TS::OK;
|
||||
|
||||
for (size_t i = 1; i < pts.size(); ++i)
|
||||
{
|
||||
if (pts[0] != pts[i])
|
||||
return TS::OK;
|
||||
}
|
||||
|
||||
int const channels = fht.channels();
|
||||
vector<Mat> fht_channels(channels);
|
||||
split(fht, fht_channels);
|
||||
|
||||
for (int c = 0; c < channels; ++c)
|
||||
{
|
||||
for (int y = 0; y < fht.rows; ++y)
|
||||
{
|
||||
int cnt = countNonZero(fht_channels[c].row(y));
|
||||
if (cnt != 1)
|
||||
{
|
||||
ts->printf(TS::LOG,
|
||||
"The incorrect count of non-zero values in column "
|
||||
"#%d, channel #%d of FastHoughTransform result "
|
||||
"image (=%d, should be %d)\n",
|
||||
y, c, cnt, 1);
|
||||
return TS::FAIL_BAD_ACCURACY;
|
||||
}
|
||||
}
|
||||
}
|
||||
return TS::OK;
|
||||
}
|
||||
|
||||
static const double MAX_LDIST = 2.0;
|
||||
int TestFHT::validate_line(Mat const& fht,
|
||||
Mat const& src,
|
||||
vector<Point> const& pts)
|
||||
{
|
||||
size_t const size = (int)pts.size();
|
||||
if (size < 2)
|
||||
return TS::OK;
|
||||
size_t first_pt_i = 0, second_pt_i = 1;
|
||||
for (size_t i = first_pt_i + 1; i < size; ++i)
|
||||
{
|
||||
if (pts[i] != pts[first_pt_i])
|
||||
{
|
||||
second_pt_i = first_pt_i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pts[second_pt_i] == pts[first_pt_i])
|
||||
return TS::OK;
|
||||
for (size_t i = second_pt_i + 1; i < size; ++i)
|
||||
{
|
||||
if (pts[i] != pts[second_pt_i])
|
||||
return TS::OK;
|
||||
}
|
||||
const Point &f = pts[first_pt_i];
|
||||
const Point &s = pts[second_pt_i];
|
||||
|
||||
int const channels = fht.channels();
|
||||
vector<Mat> fht_channels(channels);
|
||||
split(fht, fht_channels);
|
||||
|
||||
for (int ch = 0; ch < channels; ++ch)
|
||||
{
|
||||
Point fht_max(-1, -1);
|
||||
minMaxLoc(fht_channels[ch], 0, 0, 0, &fht_max);
|
||||
Vec4i src_line;
|
||||
HoughPoint2Line(src_line, fht_max, src,
|
||||
ARO_315_135, HDO_DESKEW, RO_STRICT);
|
||||
|
||||
double const a = src_line[1] - src_line[3];
|
||||
double const b = src_line[2] - src_line[0];
|
||||
double const c = - (a * src_line[0] + b * src_line[1]);
|
||||
|
||||
double const fd = abs(f.x * a + f.y * b + c) / sqrt(a * a + b * b);
|
||||
double const sd = abs(s.x * a + s.y * b + c) / sqrt(a * a + b * b);
|
||||
double const dist = std::max(fd, sd);
|
||||
|
||||
if (dist > MAX_LDIST)
|
||||
{
|
||||
ts->printf(TS::LOG,
|
||||
"Failed to detect max line in channels %d (distance "
|
||||
"between point and line correspoinding of maximum in "
|
||||
"FastHoughTransform space is #%g)\n", ch, dist);
|
||||
return TS::FAIL_BAD_ACCURACY;
|
||||
}
|
||||
}
|
||||
|
||||
return TS::OK;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int TestFHT::validate_test_results(Mat const &fht,
|
||||
Mat const &src,
|
||||
vector<Point> const& pts)
|
||||
{
|
||||
int code = validate_sum<T>(src, fht);
|
||||
if (code == TS::OK)
|
||||
code = validate_point(fht, pts);
|
||||
if (code == TS::OK)
|
||||
code = validate_line(fht, src, pts);
|
||||
return code;
|
||||
}
|
||||
|
||||
int TestFHT::run_func(Mat const&src,
|
||||
Mat& fht)
|
||||
{
|
||||
int code = TS::OK;
|
||||
FastHoughTransform(src, fht, src.depth());
|
||||
return code;
|
||||
}
|
||||
|
||||
static Size random_size(int const max_size_log,
|
||||
int const elem_size)
|
||||
{
|
||||
RNG& rng = TS::ptr()->get_rng();
|
||||
return randomSize(rng, std::max(1,
|
||||
max_size_log - cvRound(log(double(elem_size)))));
|
||||
}
|
||||
|
||||
static const int FHT_MAX_SIZE_LOG = 9;
|
||||
|
||||
template <typename T>
|
||||
int TestFHT::run_test(int depth,
|
||||
int channels,
|
||||
int pts_count)
|
||||
{
|
||||
int code = TS::OK;
|
||||
|
||||
Size size = random_size(FHT_MAX_SIZE_LOG,
|
||||
CV_ELEM_SIZE(CV_MAKE_TYPE(depth, channels)));
|
||||
Mat src = Mat::zeros(size, CV_MAKETYPE(depth, channels));
|
||||
|
||||
vector<Point> pts;
|
||||
code = put_random_points<T>(src, pts_count, pts);
|
||||
if (code != TS::OK)
|
||||
return code;
|
||||
|
||||
Mat fht;
|
||||
code = run_func(src, fht);
|
||||
if (code != TS::OK)
|
||||
return code;
|
||||
|
||||
code = validate_test_results<T>(fht, src, pts);
|
||||
return code;
|
||||
}
|
||||
|
||||
void TestFHT::run_n_tests(int depth,
|
||||
int channels,
|
||||
int pts_count,
|
||||
int n)
|
||||
{
|
||||
try
|
||||
{
|
||||
int code = TS::OK;
|
||||
|
||||
switch (depth)
|
||||
{
|
||||
case CV_8U:
|
||||
code = run_n_tests_t<uchar>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_8S:
|
||||
code = run_n_tests_t<schar>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_16U:
|
||||
code = run_n_tests_t<ushort>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_16S:
|
||||
code = run_n_tests_t<short>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_32S:
|
||||
code = run_n_tests_t<int>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_32F:
|
||||
code = run_n_tests_t<float>(depth, channels, pts_count, n);
|
||||
break;
|
||||
case CV_64F:
|
||||
code = run_n_tests_t<double>(depth, channels, pts_count, n);
|
||||
break;
|
||||
default:
|
||||
code = TS::FAIL_BAD_ARG_CHECK;
|
||||
ts->printf(TS::LOG, "Unknown depth %d\n", depth);
|
||||
break;
|
||||
}
|
||||
if (code != TS::OK)
|
||||
throw TS::FailureCode(code);
|
||||
}
|
||||
catch (const TS::FailureCode& fc)
|
||||
{
|
||||
std::string errorStr = TS::str_from_code(fc);
|
||||
ts->printf(TS::LOG,
|
||||
"General failure:\n\t%s (%d)\n", errorStr.c_str(), fc);
|
||||
|
||||
ts->set_failed_test_info(fc);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
ts->printf(TS::LOG, "Unknown failure\n");
|
||||
ts->set_failed_test_info(TS::FAIL_EXCEPTION);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int TestFHT::run_n_tests_t(int depth,
|
||||
int channels,
|
||||
int pts_count,
|
||||
int n)
|
||||
{
|
||||
int code = TS::OK;
|
||||
for (int iTest = 0; iTest < n; ++iTest)
|
||||
{
|
||||
code = run_test<T>(depth, channels, pts_count);
|
||||
if (code != TS::OK)
|
||||
{
|
||||
ts->printf(TS::LOG, "Test %d failed with code %d\n", iTest, code);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
//----------------------TEST_P--------------------------------------------------
|
||||
typedef std::tr1::tuple<int, int, int, int> Depth_Channels_PtsC_nPerTest;
|
||||
typedef TestWithParam<Depth_Channels_PtsC_nPerTest> FastHoughTransformTest;
|
||||
|
||||
TEST_P(FastHoughTransformTest, accuracy)
|
||||
{
|
||||
int const depth = get<0>(GetParam());
|
||||
int const channels = get<1>(GetParam());
|
||||
int const pts_count = get<2>(GetParam());
|
||||
int const n_per_test = get<3>(GetParam());
|
||||
|
||||
TestFHT testFht;
|
||||
testFht.run_n_tests(depth, channels, pts_count, n_per_test);
|
||||
}
|
||||
|
||||
#define FHT_ALL_DEPTHS CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F
|
||||
#define FHT_ALL_CHANNELS 1, 2, 3, 4
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(FullSet, FastHoughTransformTest,
|
||||
Combine(Values(FHT_ALL_DEPTHS),
|
||||
Values(FHT_ALL_CHANNELS),
|
||||
Values(1, 2),
|
||||
Values(5)));
|
||||
|
||||
#undef FHT_ALL_DEPTHS
|
||||
#undef FHT_ALL_CHANNELS
|
||||
|
||||
} // namespace cvtest
|
Reference in New Issue
Block a user