学习目标:
设定最大最小阈值,实现阈值区间的图像分割
通过指针遍历图像,提高执行效率(不建议src.at()式访问图像像素)
核心代码:
提示:这里可以添加要学的内容
/*
区间阈值分割
Input:
src: 待分割图像,单通道灰度图
minThre、maxThre:前景区间赋值为255,其余赋值为0
Output:
dest: 阈值分割图像,二值图
*/
void intervalThresholdAnylize(Mat src, Mat& dest, int minThre, int maxThre)
{
dest = src.clone();
int rows = src.rows, cols = src.cols;
for (int i = 0; i < rows; i++)
{
uchar *pRow = dest.ptr<uchar>(i);
for (int j = 0; j < cols; j++)
{
int pixel = (int)pRow[j];
if ((pixel < minThre) || (pixel > maxThre))
{
pRow[j] = 0;
}
else
{
pRow[j] = 255;
}
}
}
}
效果:


499

被折叠的 条评论
为什么被折叠?



