enum adaptiveMethod
{
meanFilter,
gaaussianFilter,
medianFilter
};
/**
* @description: 自适应阈值法
* @param src 输入图像
* @param dst 输出图像
* @param size 滤波尺寸
* @param c 平移量
* @param method 自适应方法
*/
void adaptivethreshold(cv::Mat &src, cv::Mat &dst, int size, double c, adaptiveMethod method = meanFilter)
{
cv::Mat smooth;
switch (method)
{
case meanFilter:
cv::blur(src, smooth, cv::Size(size, size));
break;
case gaaussianFilter:
cv::GaussianBlur(src, smooth, cv::Size(size, size), 0, 0);
break;
case medianFilter:
cv::medianBlur(src, smooth, size);
break;
default:
break;
}
smooth = smooth - c;
dst = src.clone();
for (int i = 0; i < src.rows; ++i)
{
const uchar *srcptr = src.ptr<uchar>(i);
const uchar *smoothptr = smooth.ptr<uchar>(i);
uchar *dstptr = dst.ptr<uchar>(i);
for (int j = 0; j < src.cols; ++j)
{
if (srcptr[j] > smoothptr[j])
dstptr[j] = 255;
else
dstptr[j] = 0;
}
}
}
adaptiveThreshold自适应阈值实现
于 2021-06-11 23:11:17 首次发布