Merge pull request #3540 from AlexanderUsentsov:good_feature

This commit is contained in:
Maksim Shabunin
2015-12-08 08:02:27 +00:00
4 changed files with 770 additions and 1 deletions

View File

@@ -163,6 +163,9 @@ CV_EXPORTS void gemm(const Mat& src1, const Mat& src2, double alpha,
const Mat& src3, double beta, Mat& dst, int flags);
CV_EXPORTS void transform( const Mat& src, Mat& dst, const Mat& transmat, const Mat& shift );
CV_EXPORTS double crossCorr(const Mat& src1, const Mat& src2);
CV_EXPORTS void threshold( const Mat& src, Mat& dst, double thresh, double maxval, int thresh_type );
CV_EXPORTS void minMaxIdx( InputArray _img, double* minVal, double* maxVal,
Point* minLoc, Point* maxLoc, InputArray _mask );
struct CV_EXPORTS MatInfo
{

View File

@@ -1,6 +1,7 @@
#include "precomp.hpp"
#include <float.h>
#include <limits.h>
#include "opencv2/imgproc/types_c.h"
#ifdef HAVE_TEGRA_OPTIMIZATION
#include "tegra.hpp"
@@ -3074,4 +3075,265 @@ void printVersionInfo(bool useStdOut)
#endif
}
void threshold( const Mat& _src, Mat& _dst,
double thresh, double maxval, int thresh_type )
{
int i, j;
int depth = _src.depth(), cn = _src.channels();
int width_n = _src.cols*cn, height = _src.rows;
int ithresh = cvFloor(thresh);
int imaxval, ithresh2;
if( depth == CV_8U )
{
ithresh2 = saturate_cast<uchar>(ithresh);
imaxval = saturate_cast<uchar>(maxval);
}
else if( depth == CV_16S )
{
ithresh2 = saturate_cast<short>(ithresh);
imaxval = saturate_cast<short>(maxval);
}
else
{
ithresh2 = cvRound(ithresh);
imaxval = cvRound(maxval);
}
assert( depth == CV_8U || depth == CV_16S || depth == CV_32F );
switch( thresh_type )
{
case CV_THRESH_BINARY:
for( i = 0; i < height; i++ )
{
if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0);
}
else if( depth == CV_16S )
{
const short* src = _src.ptr<short>(i);
short* dst = _dst.ptr<short>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (short)(src[j] > ithresh ? imaxval : 0);
}
else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (float)((double)src[j] > thresh ? maxval : 0.f);
}
}
break;
case CV_THRESH_BINARY_INV:
for( i = 0; i < height; i++ )
{
if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval);
}
else if( depth == CV_16S )
{
const short* src = _src.ptr<short>(i);
short* dst = _dst.ptr<short>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (short)(src[j] > ithresh ? 0 : imaxval);
}
else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ )
dst[j] = (float)((double)src[j] > thresh ? 0.f : maxval);
}
}
break;
case CV_THRESH_TRUNC:
for( i = 0; i < height; i++ )
{
if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (uchar)(s > ithresh ? ithresh2 : s);
}
}
else if( depth == CV_16S )
{
const short* src = _src.ptr<short>(i);
short* dst = _dst.ptr<short>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (short)(s > ithresh ? ithresh2 : s);
}
}
else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ )
{
double s = src[j];
dst[j] = (float)(s > thresh ? thresh : s);
}
}
}
break;
case CV_THRESH_TOZERO:
for( i = 0; i < height; i++ )
{
if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (uchar)(s > ithresh ? s : 0);
}
}
else if( depth == CV_16S )
{
const short* src = _src.ptr<short>(i);
short* dst = _dst.ptr<short>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (short)(s > ithresh ? s : 0);
}
}
else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ )
{
float s = src[j];
dst[j] = s > thresh ? s : 0.f;
}
}
}
break;
case CV_THRESH_TOZERO_INV:
for( i = 0; i < height; i++ )
{
if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (uchar)(s > ithresh ? 0 : s);
}
}
else if( depth == CV_16S )
{
const short* src = _src.ptr<short>(i);
short* dst = _dst.ptr<short>(i);
for( j = 0; j < width_n; j++ )
{
int s = src[j];
dst[j] = (short)(s > ithresh ? 0 : s);
}
}
else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ )
{
float s = src[j];
dst[j] = s > thresh ? 0.f : s;
}
}
}
break;
default:
assert(0);
}
}
static void
_minMaxIdx( const float* src, const uchar* mask, double* _minVal, double* _maxVal,
size_t* _minIdx, size_t* _maxIdx, int len, size_t startIdx )
{
double minVal = FLT_MAX, maxVal = -FLT_MAX;
size_t minIdx = 0, maxIdx = 0;
if( !mask )
{
for( int i = 0; i < len; i++ )
{
float val = src[i];
if( val < minVal )
{
minVal = val;
minIdx = startIdx + i;
}
if( val > maxVal )
{
maxVal = val;
maxIdx = startIdx + i;
}
}
}
else
{
for( int i = 0; i < len; i++ )
{
float val = src[i];
if( mask[i] && val < minVal )
{
minVal = val;
minIdx = startIdx + i;
}
if( mask[i] && val > maxVal )
{
maxVal = val;
maxIdx = startIdx + i;
}
}
}
if (_minIdx)
*_minIdx = minIdx;
if (_maxIdx)
*_maxIdx = maxIdx;
if (_minVal)
*_minVal = minVal;
if (_maxVal)
*_maxVal = maxVal;
}
void minMaxIdx( InputArray _img, double* minVal, double* maxVal,
Point* minLoc, Point* maxLoc, InputArray _mask )
{
Mat img = _img.getMat();
Mat mask = _mask.getMat();
CV_Assert(img.dims <= 2);
_minMaxIdx((const float*)img.data, mask.data, minVal, maxVal, (size_t*)minLoc, (size_t*)maxLoc, (int)img.total(),1);
if( minLoc )
std::swap(minLoc->x, minLoc->y);
if( maxLoc )
std::swap(maxLoc->x, maxLoc->y);
}
}