数学之路-python计算实战(8)-机器视觉-图像二值化

本文介绍了机器视觉中图像二值化的概念和应用,包括固定阈值和自适应阈值两种方法。重点讲解了OpenCV库中的`threshold`函数,用于实现固定阈值二值化,并提到了THRESH_OTSU选项用于自动确定最佳阈值。此外,还提及了`adaptiveThreshold`函数,用于进行自适应二值化和边缘提取。
摘要由CSDN通过智能技术生成

二值化

hreshold

Applies a fixed-level threshold to each array element.

C++:  double  threshold (InputArray  src, OutputArray  dst, double  thresh, double maxval, int  type )
Python:   cv2. threshold (src, thresh, maxval, type [, dst ] ) → retval, dst
C:  double  cvThreshold (const CvArr*  src, CvArr*  dst, double  threshold, double max_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

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

  • THRESH_BINARY_INV

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

  • THRESH_TRUNC

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

  • THRESH_TOZERO

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

  • THRESH_TOZERO_INV

    \texttt{dst} (x,y) =  \fork{0}{if $\texttt{src}(x,y) > \texttt{thresh}$}{\texttt{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, int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, int threshold_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()






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值