Commit 27690e3b authored by Alexey Spizhevoy's avatar Alexey Spizhevoy

added minMaxLoc function into gpu module

parent d366c0b2
......@@ -422,7 +422,10 @@ namespace cv
CV_EXPORTS Scalar sum(const GpuMat& m);
//! finds global minimum and maximum array elements and returns their values
CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal = 0);
CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal=0);
//! finds global minimum and maximum array elements and returns their values with locations
CV_EXPORTS void minMaxLoc(const GpuMat& src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0);
//! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
//! destination array will have the depth type as lut and the same channels number as source
......
......@@ -66,6 +66,7 @@ double cv::gpu::norm(const GpuMat&, const GpuMat&, int) { throw_nogpu(); return
void cv::gpu::flip(const GpuMat&, GpuMat&, int) { throw_nogpu(); }
Scalar cv::gpu::sum(const GpuMat&) { throw_nogpu(); return Scalar(); }
void cv::gpu::minMax(const GpuMat&, double*, double*) { throw_nogpu(); }
void cv::gpu::minMaxLoc(const GpuMat&, double*, double*, Point*, Point*) { throw_nogpu(); }
void cv::gpu::LUT(const GpuMat&, const Mat&, GpuMat&) { throw_nogpu(); }
void cv::gpu::exp(const GpuMat&, GpuMat&) { throw_nogpu(); }
void cv::gpu::log(const GpuMat&, GpuMat&) { throw_nogpu(); }
......@@ -530,6 +531,57 @@ void cv::gpu::minMax(const GpuMat& src, double* minVal, double* maxVal)
}
}
////////////////////////////////////////////////////////////////////////
// minMaxLoc
namespace cv { namespace gpu { namespace mathfunc {
template <typename T>
void min_max_loc_caller(const DevMem2D src, double* minval, double* maxval, int* minlocx, int* minlocy,
int* maxlocx, int* maxlocy);
}}}
void cv::gpu::minMaxLoc(const GpuMat& src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc)
{
CV_Assert(src.channels() == 1);
double maxVal_;
if (!maxVal) maxVal = &maxVal_;
cv::Point minLoc_;
if (!minLoc) minLoc = &minLoc_;
cv::Point maxLoc_;
if (!maxLoc) maxLoc = &maxLoc_;
switch (src.type())
{
case CV_8U:
mathfunc::min_max_loc_caller<unsigned char>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_8S:
mathfunc::min_max_loc_caller<signed char>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_16U:
mathfunc::min_max_loc_caller<unsigned short>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_16S:
mathfunc::min_max_loc_caller<signed short>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_32S:
mathfunc::min_max_loc_caller<int>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_32F:
mathfunc::min_max_loc_caller<float>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
case CV_64F:
mathfunc::min_max_loc_caller<double>(src, minVal, maxVal, &minLoc->x, &minLoc->y, &maxLoc->x, &maxLoc->y);
break;
default:
CV_Error(CV_StsBadArg, "Unsupported type");
}
}
////////////////////////////////////////////////////////////////////////
// LUT
......
This diff is collapsed.
......@@ -733,6 +733,71 @@ struct CV_GpuMinMaxTest: public CvTest
};
////////////////////////////////////////////////////////////////////////////////
// Min max loc
struct CV_GpuMinMaxLocTest: public CvTest
{
CV_GpuMinMaxLocTest(): CvTest("GPU-MinMaxLocTest", "minMaxLoc") {}
void run(int)
{
for (int depth = CV_8U; depth <= CV_64F; ++depth)
{
int rows = 1, cols = 3;
test(rows, cols, depth);
for (int i = 0; i < 4; ++i)
{
int rows = 1 + rand() % 1000;
int cols = 1 + rand() % 1000;
test(rows, cols, depth);
}
}
}
void test(int rows, int cols, int depth)
{
cv::Mat src(rows, cols, depth);
cv::RNG rng;
for (int i = 0; i < src.rows; ++i)
{
Mat row(1, src.cols * src.elemSize(), CV_8U, src.ptr(i));
rng.fill(row, RNG::UNIFORM, Scalar(0), Scalar(255));
}
double minVal, maxVal;
cv::Point minLoc, maxLoc;
if (depth != CV_8S)
cv::minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc);
else
{
// OpenCV's minMaxLoc doesn't support CV_8S type
minVal = std::numeric_limits<double>::max();
maxVal = std::numeric_limits<double>::min();
for (int i = 0; i < src.rows; ++i)
for (int j = 0; j < src.cols; ++j)
{
char val = src.at<char>(i, j);
if (val < minVal) { minVal = val; minLoc = cv::Point(j, i); }
if (val > maxVal) { maxVal = val; maxLoc = cv::Point(j, i); }
}
}
double minVal_, maxVal_;
cv::Point minLoc_, maxLoc_;
cv::gpu::minMaxLoc(cv::gpu::GpuMat(src), &minVal_, &maxVal_, &minLoc_, &maxLoc_);
CHECK(minVal == minVal_, CvTS::FAIL_INVALID_OUTPUT);
CHECK(maxVal == maxVal_, CvTS::FAIL_INVALID_OUTPUT);
CHECK(0 == memcmp(src.ptr(minLoc.y) + minLoc.x * src.elemSize(), src.ptr(minLoc_.y) + minLoc_.x * src.elemSize(), src.elemSize()),
CvTS::FAIL_INVALID_OUTPUT);
CHECK(0 == memcmp(src.ptr(maxLoc.y) + maxLoc.x * src.elemSize(), src.ptr(maxLoc_.y) + maxLoc_.x * src.elemSize(), src.elemSize()),
CvTS::FAIL_INVALID_OUTPUT);
}
};
/////////////////////////////////////////////////////////////////////////////
/////////////////// tests registration /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
......@@ -760,3 +825,4 @@ CV_GpuNppImagePhaseTest CV_GpuNppImagePhase_test;
CV_GpuNppImageCartToPolarTest CV_GpuNppImageCartToPolar_test;
CV_GpuNppImagePolarToCartTest CV_GpuNppImagePolarToCart_test;
CV_GpuMinMaxTest CV_GpuMinMaxTest_test;
CV_GpuMinMaxLocTest CV_GpuMinMaxLocTest_test;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment