zoukankan      html  css  js  c++  java
  • 数学思想方法-python计算战(8)-机器视觉-二值化

    二值化

    hreshold

    Applies a fixed-level threshold to each array element.

    C++: double threshold(InputArray src, OutputArray dst, double thresh, doublemaxval, int type)
    Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst
    C: double cvThreshold(const CvArr* src, CvArr* dst, double threshold, doublemax_value, int threshold_type)
    Parameters:
    • src – input array (single-channel, 8-bit or 32-bit floating point).
    • dst – output array of the same size and type as src.
    • thresh – threshold value.
    • maxval – maximum value to use with the THRESH_BINARY andTHRESH_BINARY_INV thresholding types.
    • type – thresholding type (see the details below).

    The function applies fixed-level thresholding to a single-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image (compare() could be also used for this purpose) or for removing a noise, that is, filtering out pixels with too small or too large values. There are several types of thresholding supported by the function. They are determined by type :

    • THRESH_BINARY

      	exttt{dst} (x,y) =  fork{	exttt{maxval}}{if $	exttt{src}(x,y) > 	exttt{thresh}$}{0}{otherwise}

    • THRESH_BINARY_INV

      	exttt{dst} (x,y) =  fork{0}{if $	exttt{src}(x,y) > 	exttt{thresh}$}{	exttt{maxval}}{otherwise}

    • THRESH_TRUNC

      	exttt{dst} (x,y) =  fork{	exttt{threshold}}{if $	exttt{src}(x,y) > 	exttt{thresh}$}{	exttt{src}(x,y)}{otherwise}

    • THRESH_TOZERO

      	exttt{dst} (x,y) =  fork{	exttt{src}(x,y)}{if $	exttt{src}(x,y) > 	exttt{thresh}$}{0}{otherwise}

    • THRESH_TOZERO_INV

      	exttt{dst} (x,y) =  fork{0}{if $	exttt{src}(x,y) > 	exttt{thresh}$}{	exttt{src}(x,y)}{otherwise}

    Also, the special value THRESH_OTSU may be combined with one of the above values. In this case, the function determines the optimal threshold value using the Otsu’s algorithm and uses it instead of the specified thresh . The function returns the computed threshold value. Currently, the Otsu’s method is implemented only for 8-bit images.


    import cv2
    
    fn="test3.jpg"
    myimg=cv2.imread(fn)
    img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
    
    
    
    retval, newimg=cv2.threshold(img,40,255,cv2.THRESH_BINARY)
    cv2.imshow('preview',newimg)
    cv2.waitKey()
    cv2.destroyAllWindows()

    本博客全部内容是原创,假设转载请注明来源

    http://blog.csdn.net/myhaspl/






    自适应二值化

    adaptiveThreshold函数能够二值化,也能够提取边缘:


    Python: cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst

    C: void cvAdaptiveThreshold(const CvArr* src, CvArr* dst, double max_value, intadaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, intthreshold_type=CV_THRESH_BINARY, int block_size=3, double param1=5 )


     
    • src – Source 8-bit single-channel image.
    • dst – Destination image of the same size and the same type as src .
    • maxValue – Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
    • adaptiveMethod – Adaptive thresholding algorithm to use,ADAPTIVE_THRESH_MEAN_C orADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
    • thresholdType – Thresholding type that must be eitherTHRESH_BINARY or THRESH_BINARY_INV .
    • blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
    • C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.


    • block_size參数决定局部阈值的block的大小。block非常小时。如block_size=3 or 5 or 7时,表现为边缘提取函数。当把block_size设为比較大的值时,如block_size=21、51等,便是二值化

    以下是提取边缘
    import cv2
    
    fn="test3.jpg"
    myimg=cv2.imread(fn)
    img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
    
    
    
    newimg=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,5,2)
    cv2.imshow('preview',newimg)
    cv2.waitKey()
    cv2.destroyAllWindows()


    二值化例如以下:


    import cv2
    
    fn="test3.jpg"
    myimg=cv2.imread(fn)
    img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)
    
    
    
    newimg=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,51,2)
    cv2.imshow('preview',newimg)
    cv2.waitKey()
    cv2.destroyAllWindows()






    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    自媒体
    一种流量传感器
    BUCK BOOST学习总结
    手机POS机
    几种新的MCU开发环境和语言
    谷歌浏览器Chrome播放rtsp视频流解决方案
    Three.js 保存camera(视角)设置到数据库,包括场景的缩放、旋转、移动等
    javascript 二维(多维)数组的复制问题
    技嘉 gigabyte b75m d3v 主板 定时开机无效问题解决
    Photoshop颜色出现比较大的偏差,偏色严重,显示器配置文件2351似乎有问题
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4809850.html
Copyright © 2011-2022 走看看