refactored generalized hough (both CPU and GPU):

removed set/get methods from Algorithm (implement owns)
removed GHT_* enumeration
This commit is contained in:
Vladislav Vinogradov
2013-06-25 17:39:05 +04:00
parent 4b234fa0a5
commit 7b87d72d80
9 changed files with 1086 additions and 1993 deletions

View File

@@ -5,13 +5,12 @@
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/gpu.hpp"
#include "opencv2/gpuimgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/contrib.hpp"
using namespace std;
using namespace cv;
using cv::gpu::GpuMat;
static Mat loadImage(const string& name)
{
@@ -29,8 +28,7 @@ int main(int argc, const char* argv[])
CommandLineParser cmd(argc, argv,
"{ image i | pic1.png | input image }"
"{ template t | templ.png | template image }"
"{ scale s | | estimate scale }"
"{ rotation r | | estimate rotation }"
"{ full | | estimate scale and rotation }"
"{ gpu | | use gpu version }"
"{ minDist | 100 | minimum distance between the centers of the detected objects }"
"{ levels | 360 | R-Table levels }"
@@ -45,7 +43,7 @@ int main(int argc, const char* argv[])
"{ minAngle | 0 | minimal rotation angle to detect in degrees }"
"{ maxAngle | 360 | maximal rotation angle to detect in degrees }"
"{ angleStep | 1 | angle step in degrees }"
"{ maxSize | 1000 | maximal size of inner buffers }"
"{ maxBufSize | 1000 | maximal size of inner buffers }"
"{ help h ? | | print help message }"
);
@@ -59,8 +57,7 @@ int main(int argc, const char* argv[])
const string templName = cmd.get<string>("template");
const string imageName = cmd.get<string>("image");
const bool estimateScale = cmd.has("scale");
const bool estimateRotation = cmd.has("rotation");
const bool full = cmd.has("full");
const bool useGpu = cmd.has("gpu");
const double minDist = cmd.get<double>("minDist");
const int levels = cmd.get<int>("levels");
@@ -75,7 +72,7 @@ int main(int argc, const char* argv[])
const double minAngle = cmd.get<double>("minAngle");
const double maxAngle = cmd.get<double>("maxAngle");
const double angleStep = cmd.get<double>("angleStep");
const int maxSize = cmd.get<int>("maxSize");
const int maxBufSize = cmd.get<int>("maxBufSize");
if (!cmd.check())
{
@@ -86,93 +83,69 @@ int main(int argc, const char* argv[])
Mat templ = loadImage(templName);
Mat image = loadImage(imageName);
int method = cv::GeneralizedHough::GHT_POSITION;
if (estimateScale)
method += cv::GeneralizedHough::GHT_SCALE;
if (estimateRotation)
method += cv::GeneralizedHough::GHT_ROTATION;
Ptr<GeneralizedHough> alg;
if (!full)
{
Ptr<GeneralizedHoughBallard> ballard = useGpu ? gpu::createGeneralizedHoughBallard() : createGeneralizedHoughBallard();
ballard->setMinDist(minDist);
ballard->setLevels(levels);
ballard->setDp(dp);
ballard->setMaxBufferSize(maxBufSize);
ballard->setVotesThreshold(votesThreshold);
alg = ballard;
}
else
{
Ptr<GeneralizedHoughGuil> guil = useGpu ? gpu::createGeneralizedHoughGuil() : createGeneralizedHoughGuil();
guil->setMinDist(minDist);
guil->setLevels(levels);
guil->setDp(dp);
guil->setMaxBufferSize(maxBufSize);
guil->setMinAngle(minAngle);
guil->setMaxAngle(maxAngle);
guil->setAngleStep(angleStep);
guil->setAngleThresh(angleThresh);
guil->setMinScale(minScale);
guil->setMaxScale(maxScale);
guil->setScaleStep(scaleStep);
guil->setScaleThresh(scaleThresh);
guil->setPosThresh(posThresh);
alg = guil;
}
vector<Vec4f> position;
cv::TickMeter tm;
TickMeter tm;
if (useGpu)
{
GpuMat d_templ(templ);
GpuMat d_image(image);
GpuMat d_position;
gpu::GpuMat d_templ(templ);
gpu::GpuMat d_image(image);
gpu::GpuMat d_position;
Ptr<gpu::GeneralizedHough> d_hough = gpu::GeneralizedHough::create(method);
d_hough->set("minDist", minDist);
d_hough->set("levels", levels);
d_hough->set("dp", dp);
d_hough->set("maxSize", maxSize);
if (estimateScale && estimateRotation)
{
d_hough->set("angleThresh", angleThresh);
d_hough->set("scaleThresh", scaleThresh);
d_hough->set("posThresh", posThresh);
}
else
{
d_hough->set("votesThreshold", votesThreshold);
}
if (estimateScale)
{
d_hough->set("minScale", minScale);
d_hough->set("maxScale", maxScale);
d_hough->set("scaleStep", scaleStep);
}
if (estimateRotation)
{
d_hough->set("minAngle", minAngle);
d_hough->set("maxAngle", maxAngle);
d_hough->set("angleStep", angleStep);
}
d_hough->setTemplate(d_templ);
alg->setTemplate(d_templ);
tm.start();
d_hough->detect(d_image, d_position);
d_hough->downloadResults(d_position, position);
alg->detect(d_image, d_position);
d_position.download(position);
tm.stop();
}
else
{
Ptr<GeneralizedHough> hough = GeneralizedHough::create(method);
hough->set("minDist", minDist);
hough->set("levels", levels);
hough->set("dp", dp);
if (estimateScale && estimateRotation)
{
hough->set("angleThresh", angleThresh);
hough->set("scaleThresh", scaleThresh);
hough->set("posThresh", posThresh);
hough->set("maxSize", maxSize);
}
else
{
hough->set("votesThreshold", votesThreshold);
}
if (estimateScale)
{
hough->set("minScale", minScale);
hough->set("maxScale", maxScale);
hough->set("scaleStep", scaleStep);
}
if (estimateRotation)
{
hough->set("minAngle", minAngle);
hough->set("maxAngle", maxAngle);
hough->set("angleStep", angleStep);
}
hough->setTemplate(templ);
alg->setTemplate(templ);
tm.start();
hough->detect(image, position);
alg->detect(image, position);
tm.stop();
}