一. 关键函数介绍
下面就介绍OpenCV中对图像进行二值化的关键函数——cvThreshold()。
函数功能:采用Canny方法对图像进行边缘检测
函数原型:
void cvThreshold(
const CvArr* src,
CvArr* dst,
double threshold,
double max_value,
int threshold_type
);
函数说明:
第一个参数表示输入图像,必须为单通道灰度图。
第二个参数表示输出的边缘图像,为单通道黑白图。
第三个参数表示阈值
第四个参数表示最大值。
第五个参数表示运算方法。
在OpenCV的imgproc\types_c.h中可以找到运算方法的定义。
/* Threshold types*/
enum
{
CV_THRESH_BINARY =0, /* value = value > threshold ? max_value :0 */
CV_THRESH_BINARY_INV =1, /* value = value > threshold ? 0 : max_value */
CV_THRESH_TRUNC =2, /* value = value > threshold ? threshold : value */
CV_THRESH_TOZERO =3, /* value = value > threshold ? value :0 */
CV_THRESH_TOZERO_INV =4, /* value = value > threshold ? 0 :value */
CV_THRESH_MASK =7,
CV_THRESH_OTSU =8 /* use Otsualgorithm to choose the optimal threshold value; combine the flag with one ofthe above CV_THRESH_* values */
};
注释已经写的很清楚了,因此不再用中文来表达了。
现象:
注意了:
其中的二值化灰度值是可以调节的,当灰度值比较小的时候,出现轮廓很大的情况,与事实不符合。所以需要调节到合适的灰度值。