Merge pull request #441 from jet47:filter-speckles-8u
This commit is contained in:
commit
6e4aeff4c9
@ -857,22 +857,20 @@ Rect getValidDisparityROI( Rect roi1, Rect roi2,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSize,
|
namespace
|
||||||
double _maxDiff, InputOutputArray __buf )
|
|
||||||
{
|
{
|
||||||
Mat img = _img.getMat();
|
template <typename T>
|
||||||
Mat temp, &_buf = __buf.needed() ? __buf.getMatRef() : temp;
|
void filterSpecklesImpl(cv::Mat& img, int newVal, int maxSpeckleSize, int maxDiff, cv::Mat& _buf)
|
||||||
CV_Assert( img.type() == CV_16SC1 );
|
{
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
int newVal = cvRound(_newval);
|
|
||||||
int maxDiff = cvRound(_maxDiff);
|
|
||||||
int width = img.cols, height = img.rows, npixels = width*height;
|
int width = img.cols, height = img.rows, npixels = width*height;
|
||||||
size_t bufSize = npixels*(int)(sizeof(Point2s) + sizeof(int) + sizeof(uchar));
|
size_t bufSize = npixels*(int)(sizeof(Point2s) + sizeof(int) + sizeof(uchar));
|
||||||
if( !_buf.isContinuous() || !_buf.data || _buf.cols*_buf.rows*_buf.elemSize() < bufSize )
|
if( !_buf.isContinuous() || !_buf.data || _buf.cols*_buf.rows*_buf.elemSize() < bufSize )
|
||||||
_buf.create(1, (int)bufSize, CV_8U);
|
_buf.create(1, (int)bufSize, CV_8U);
|
||||||
|
|
||||||
uchar* buf = _buf.data;
|
uchar* buf = _buf.data;
|
||||||
int i, j, dstep = (int)(img.step/sizeof(short));
|
int i, j, dstep = (int)(img.step/sizeof(T));
|
||||||
int* labels = (int*)buf;
|
int* labels = (int*)buf;
|
||||||
buf += npixels*sizeof(labels[0]);
|
buf += npixels*sizeof(labels[0]);
|
||||||
Point2s* wbuf = (Point2s*)buf;
|
Point2s* wbuf = (Point2s*)buf;
|
||||||
@ -885,7 +883,7 @@ void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSi
|
|||||||
|
|
||||||
for( i = 0; i < height; i++ )
|
for( i = 0; i < height; i++ )
|
||||||
{
|
{
|
||||||
short* ds = img.ptr<short>(i);
|
T* ds = img.ptr<T>(i);
|
||||||
int* ls = labels + width*i;
|
int* ls = labels + width*i;
|
||||||
|
|
||||||
for( j = 0; j < width; j++ )
|
for( j = 0; j < width; j++ )
|
||||||
@ -895,7 +893,7 @@ void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSi
|
|||||||
if( ls[j] ) // has a label, check for bad label
|
if( ls[j] ) // has a label, check for bad label
|
||||||
{
|
{
|
||||||
if( rtype[ls[j]] ) // small region, zero out disparity
|
if( rtype[ls[j]] ) // small region, zero out disparity
|
||||||
ds[j] = (short)newVal;
|
ds[j] = (T)newVal;
|
||||||
}
|
}
|
||||||
// no label, assign and propagate
|
// no label, assign and propagate
|
||||||
else
|
else
|
||||||
@ -911,8 +909,8 @@ void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSi
|
|||||||
{
|
{
|
||||||
count++;
|
count++;
|
||||||
// put neighbors onto wavefront
|
// put neighbors onto wavefront
|
||||||
short* dpp = &img.at<short>(p.y, p.x);
|
T* dpp = &img.at<T>(p.y, p.x);
|
||||||
short dp = *dpp;
|
T dp = *dpp;
|
||||||
int* lpp = labels + width*p.y + p.x;
|
int* lpp = labels + width*p.y + p.x;
|
||||||
|
|
||||||
if( p.x < width-1 && !lpp[+1] && dpp[+1] != newVal && std::abs(dp - dpp[+1]) <= maxDiff )
|
if( p.x < width-1 && !lpp[+1] && dpp[+1] != newVal && std::abs(dp - dpp[+1]) <= maxDiff )
|
||||||
@ -948,7 +946,7 @@ void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSi
|
|||||||
if( count <= maxSpeckleSize ) // speckle region
|
if( count <= maxSpeckleSize ) // speckle region
|
||||||
{
|
{
|
||||||
rtype[ls[j]] = 1; // small region label
|
rtype[ls[j]] = 1; // small region label
|
||||||
ds[j] = (short)newVal;
|
ds[j] = (T)newVal;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
rtype[ls[j]] = 0; // large region label
|
rtype[ls[j]] = 0; // large region label
|
||||||
@ -956,6 +954,23 @@ void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cv::filterSpeckles( InputOutputArray _img, double _newval, int maxSpeckleSize,
|
||||||
|
double _maxDiff, InputOutputArray __buf )
|
||||||
|
{
|
||||||
|
Mat img = _img.getMat();
|
||||||
|
Mat temp, &_buf = __buf.needed() ? __buf.getMatRef() : temp;
|
||||||
|
CV_Assert( img.type() == CV_8UC1 || img.type() == CV_16SC1 );
|
||||||
|
|
||||||
|
int newVal = cvRound(_newval);
|
||||||
|
int maxDiff = cvRound(_maxDiff);
|
||||||
|
|
||||||
|
if (img.type() == CV_8UC1)
|
||||||
|
filterSpecklesImpl<uchar>(img, newVal, maxSpeckleSize, maxDiff, _buf);
|
||||||
|
else
|
||||||
|
filterSpecklesImpl<short>(img, newVal, maxSpeckleSize, maxDiff, _buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void cv::validateDisparity( InputOutputArray _disp, InputArray _cost, int minDisparity,
|
void cv::validateDisparity( InputOutputArray _disp, InputArray _cost, int minDisparity,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user