Commit 3dafdd6a authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

added 16s support to cv::threshold.

parent 7fb5b5f2
This diff is collapsed.
...@@ -75,9 +75,9 @@ void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx, ...@@ -75,9 +75,9 @@ void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx,
vector<vector<Size> >& sizes, vector<vector<int> >& types ) vector<vector<Size> >& sizes, vector<vector<int> >& types )
{ {
RNG& rng = ts->get_rng(); RNG& rng = ts->get_rng();
int depth = cvtest::randInt(rng) % 2, cn = cvtest::randInt(rng) % 4 + 1; int depth = cvtest::randInt(rng) % 3, cn = cvtest::randInt(rng) % 4 + 1;
cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types ); cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
depth = depth == 0 ? CV_8U : CV_32F; depth = depth == 0 ? CV_8U : depth == 1 ? CV_16S : CV_32F;
types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth,cn); types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_MAKETYPE(depth,cn);
thresh_type = cvtest::randInt(rng) % 5; thresh_type = cvtest::randInt(rng) % 5;
...@@ -87,7 +87,15 @@ void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx, ...@@ -87,7 +87,15 @@ void CV_ThreshTest::get_test_array_types_and_sizes( int test_case_idx,
thresh_val = (float)(cvtest::randReal(rng)*350. - 50.); thresh_val = (float)(cvtest::randReal(rng)*350. - 50.);
max_val = (float)(cvtest::randReal(rng)*350. - 50.); max_val = (float)(cvtest::randReal(rng)*350. - 50.);
if( cvtest::randInt(rng)%4 == 0 ) if( cvtest::randInt(rng)%4 == 0 )
max_val = 255; max_val = 255.f;
}
else if( depth == CV_16S )
{
float min_val = SHRT_MIN-100.f, max_val = SHRT_MAX+100.f;
thresh_val = (float)(cvtest::randReal(rng)*(max_val - min_val) + min_val);
max_val = (float)(cvtest::randReal(rng)*(max_val - min_val) + min_val);
if( cvtest::randInt(rng)%4 == 0 )
max_val = (float)SHRT_MAX;
} }
else else
{ {
...@@ -117,88 +125,177 @@ static void test_threshold( const Mat& _src, Mat& _dst, ...@@ -117,88 +125,177 @@ static void test_threshold( const Mat& _src, Mat& _dst,
int depth = _src.depth(), cn = _src.channels(); int depth = _src.depth(), cn = _src.channels();
int width_n = _src.cols*cn, height = _src.rows; int width_n = _src.cols*cn, height = _src.rows;
int ithresh = cvFloor(thresh), ithresh2, imaxval = cvRound(maxval); int ithresh = cvFloor(thresh), ithresh2, imaxval = cvRound(maxval);
const uchar* src = _src.data;
uchar* dst = _dst.data;
size_t srcstep = _src.step, dststep = _dst.step;
ithresh2 = saturate_cast<uchar>(ithresh); if( depth == CV_8U )
imaxval = saturate_cast<uchar>(imaxval); {
ithresh2 = saturate_cast<uchar>(ithresh);
imaxval = saturate_cast<uchar>(imaxval);
}
else if( depth == CV_16S )
{
ithresh2 = saturate_cast<short>(ithresh);
imaxval = saturate_cast<short>(imaxval);
}
assert( depth == CV_8U || depth == CV_32F ); assert( depth == CV_8U || depth == CV_16S || depth == CV_32F );
switch( thresh_type ) switch( thresh_type )
{ {
case CV_THRESH_BINARY: case CV_THRESH_BINARY:
for( i = 0; i < height; i++, src += srcstep, dst += dststep ) for( i = 0; i < height; i++ )
{ {
if( depth == CV_8U ) if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
dst[j] = (uchar)(src[j] > ithresh ? imaxval : 0); 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 else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
((float*)dst)[j] = ((const float*)src)[j] > thresh ? maxval : 0.f; dst[j] = src[j] > thresh ? maxval : 0.f;
}
} }
break; break;
case CV_THRESH_BINARY_INV: case CV_THRESH_BINARY_INV:
for( i = 0; i < height; i++, src += srcstep, dst += dststep ) for( i = 0; i < height; i++ )
{ {
if( depth == CV_8U ) if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
dst[j] = (uchar)(src[j] > ithresh ? 0 : imaxval); 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 else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
((float*)dst)[j] = ((const float*)src)[j] > thresh ? 0.f : maxval; dst[j] = src[j] > thresh ? 0.f : maxval;
}
} }
break; break;
case CV_THRESH_TRUNC: case CV_THRESH_TRUNC:
for( i = 0; i < height; i++, src += srcstep, dst += dststep ) for( i = 0; i < height; i++ )
{ {
if( depth == CV_8U ) if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
int s = src[j]; int s = src[j];
dst[j] = (uchar)(s > ithresh ? ithresh2 : s); 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 else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
float s = ((const float*)src)[j]; float s = src[j];
((float*)dst)[j] = s > thresh ? thresh : s; dst[j] = s > thresh ? thresh : s;
} }
}
} }
break; break;
case CV_THRESH_TOZERO: case CV_THRESH_TOZERO:
for( i = 0; i < height; i++, src += srcstep, dst += dststep ) for( i = 0; i < height; i++ )
{ {
if( depth == CV_8U ) if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
int s = src[j]; int s = src[j];
dst[j] = (uchar)(s > ithresh ? s : 0); 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 else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
float s = ((const float*)src)[j]; float s = src[j];
((float*)dst)[j] = s > thresh ? s : 0.f; dst[j] = s > thresh ? s : 0.f;
} }
}
} }
break; break;
case CV_THRESH_TOZERO_INV: case CV_THRESH_TOZERO_INV:
for( i = 0; i < height; i++, src += srcstep, dst += dststep ) for( i = 0; i < height; i++ )
{ {
if( depth == CV_8U ) if( depth == CV_8U )
{
const uchar* src = _src.ptr<uchar>(i);
uchar* dst = _dst.ptr<uchar>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
int s = src[j]; int s = src[j];
dst[j] = (uchar)(s > ithresh ? 0 : s); 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 else
{
const float* src = _src.ptr<float>(i);
float* dst = _dst.ptr<float>(i);
for( j = 0; j < width_n; j++ ) for( j = 0; j < width_n; j++ )
{ {
float s = ((const float*)src)[j]; float s = src[j];
((float*)dst)[j] = s > thresh ? 0.f : s; dst[j] = s > thresh ? 0.f : s;
} }
}
} }
break; break;
default: default:
......
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