1. 问题描述
使用threshold进行阈值处理,遇到下面的错误:
opencv-4.0.1/modules/imgproc/src/thresh.cpp:1389: error: (-215:Assertion failed) src.type() == CV_8UC1 in function ‘threshold’
自己的调用代码为:threshold(src, dest, threshold_value, threshold_max, THRESH_OTSU | type_value);
2. solution
Threshold的输入是单通道,但是自己在这里给出了3通道的图像,将src转为单通道图像即可解决该问题。
cvtColor(src, gray_src, CV_RGB2GRAY);
threshold(gray_src, dest, threshold_value, threshold_max, THRESH_OTSU | type_value);
3. other
看到这里提示src.type() == CV_8UC1
,以为是类型不一致。
同时查看src.type()
,发现是16,所以进行了img.convertTo(src, CV_8UC1);
转换,转换后使用src.type()
查看还是16,但仍未解决问题。
这里为什么是16?自己找到了下面这些代码,但仍未弄清楚,需要后续解决。
inline
int Mat::type() const
{
return CV_MAT_TYPE(flags);
}
#define CV_MAT_TYPE_MASK (CV_DEPTH_MAX*CV_CN_MAX - 1)
#define CV_MAT_TYPE(flags) ((flags) & CV_MAT_TYPE_MASK)
#define CV_CN_MAX 512
#define CV_CN_SHIFT 3
#define CV_DEPTH_MAX (1 << CV_CN_SHIFT)
type函数的作用为:Returns the type of a matrix element.,即返回mat中的元素类型。