计算机视觉(1)关于cvThreshold二值化函数

 

opencv中文说明中是这样说的:

Threshold   

对数组元素进行固定阈值操作

void cvThreshold( const CvArr* src, CvArr* dst, double threshold, double max_value, int threshold_type );

src:原始数组 (单通道 , 8-bit of 32-bit 浮点数)。
dst:输出数组,必须与 src 的类型一致,或者为 8-bit。
threshold:阈值
max_value:使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值。
threshold_type:阈值类型
threshold_type=CV_THRESH_BINARY:如果 src(x,y)>threshold ,dst(x,y) = max_value; 否则,dst(x,y)=0;
threshold_type=CV_THRESH_BINARY_INV:如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value.
threshold_type=CV_THRESH_TRUNC:如果 src(x,y)>threshold,dst(x,y) = max_value; 否则dst(x,y) = src(x,y).
threshold_type=CV_THRESH_TOZERO:如果src(x,y)>threshold,dst(x,y) = src(x,y) ; 否则 dst(x,y) = 0。
threshold_type=CV_THRESH_TOZERO_INV:如果 src(x,y)>threshold,dst(x,y) = 0 ; 否则dst(x,y) = src(x,y).
 

    # cv.THRESH_OTSU  整幅图像 自动测试阀值
    # 如果 src(x,y)>threshold ,dst(x,y) = max_value; 否则,dst(x,y)=0;
    ret1,thresh1=cv.threshold(grade,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)
    # 如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value.
    ret2,thresh2=cv.threshold(grade,0,255,cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
    # adaptiveThreshold:自适应阈值二值化
    # dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block
    # Size, C)
    #
    # src: 输入图,只能输入单通道图像,通常来说为灰度图
    # dst: 输出图
    # maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
    # thresh_type: 阈值的计算方法,包含以下2种类型:cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C.
    # type:二值化操作的类型,与固定阈值函数相同,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV.
    # Block
    # Size: 图片中分块的大小
    # C :阈值计算方法中的常数项


    th1=cv.adaptiveThreshold(grade,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,3)
    th2=cv.adaptiveThreshold(grade,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,3)

 

 

 

版权声明:本文为zhml原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhml/p/12518439.html