数学思想方法-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, dsthighlight=cvthreshold#cv2.threshold" title="Permalink to this definition" style="color: rgb(101, 161, 54); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;">
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

  • THRESH_BINARY_INV

  • THRESH_TRUNC

  • THRESH_TOZERO

  • THRESH_TOZERO_INV

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 )highlight=cvthreshold#void cvAdaptiveThreshold(const CvArr* src, CvArr* dst, double max_value, int adaptive_method, int threshold_type, int block_size, double param1)" title="Permalink to this definition" style="color: rgb(101, 161, 54); text-decoration: none; visibility: hidden; font-size: 0.8em; padding: 0px 4px;">
 
  • 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()

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXloYXNwbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" />

二值化例如以下:

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()

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

时间: 2024-10-11 02:22:42

数学思想方法-python计算战(8)-机器视觉-二值化的相关文章

数学之路-python计算实战(7)-机器视觉-图像产生加性零均值高斯噪声

图像产生加性零均值高斯噪声,在灰度图上加上噪声,加上噪声的方式是每个点的灰度值加上一个噪声值,噪声值的产生方式为Box-Muller算法生成高斯噪声. 在计算机模拟中,经常需要生成正态分布的数值.最基本的一个方法是使用标准的正态累积分布函数的反函数.除此之外还有其他更加高效的方法,Box-Muller变换就是其中之一.另一个更加快捷的方法是ziggurat算法.下面将介绍这两种方法.一个简单可行的并且容易编程的方法是:求12个在(0,1)上均匀分布的和,然后减6(12的一半).这种方法可以用在很

数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波

拉普拉斯线性滤波,.边缘检测   Laplacian Calculates the Laplacian of an image. C++: void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT ) Python: cv2.Laplacian(src, ddepth[, dst[, k

数学之路-python计算实战(11)-机器视觉-图像增强

在计算机领域中,灰度(Gray scale)数字图像是每个像素只有一个采样颜色的图像.这类图像通常显示为从最暗黑色到最亮的白色的灰度,尽管理论上这个采样可以任何颜色的不同深浅,甚至可以是不同亮度上的不同颜色.灰度图像与黑白图像不同,在计算机图像领域中黑白图像只有黑白两种颜色,灰度图像在黑色与白色之间还有许多级的颜色深度.用于显示的灰度图像通常用每个采样像素8 bits的非线性尺度来保存,这样可以有256种灰度(8bits就是2的8次方=256).这种精度刚刚能够避免可见的条带失真,并且非常易于编

数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )

高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到.高斯滤波的具体操作是:用一个模板(或称卷积.掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值. #滤波去噪 lbimg=cv2.GaussianBlur(newimg,(3,3),1.8) cv2.imshow('src',newimg) cv2.imshow('dst',lbimg) cv2.waitKey() cv2.destroyAllW

数学之路-python计算实战(16)-机器视觉-滤波去噪(邻域平均法滤波)

# -*- coding: utf-8 -*- #code:[email protected] #邻域平均法滤波,半径为2 import cv2 import numpy as np fn="test3.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) #加上椒盐噪声 param=20 #灰阶范围 w=img.shape[1] h=img.shape[0] newimg=np.array(img)

数学之路-python计算实战(22)-机器视觉-sobel非线性滤波

sobel非线性滤波,采用梯度模的近似方式 Sobel Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator. C++: void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, intksize=3, double scale=1, double delta=0, int

数学之路-python计算实战(19)-机器视觉-卷积滤波

filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ddepth, InputArraykernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT ) Python: cv2.filter2D(src, ddepth, kernel[, dst[,

数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波

拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:[email protected] import cv2 import numpy as np from scipy import signal fn="test6.jpg" myimg=cv2.imread(fn) img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY) srcimg=np.array(im

数学之路-python计算实战(12)-机器视觉-图像增强

分段线性变换将图像的值域分成多个值域并进行不同线性变换计算,可以压缩某部分灰度区,扩展另一部分灰度区间,下面以2个区间为例: for m in xrange(h): for n in xrange(w): if img[m,n]>Ds_min and img[m,n]<=Ds_internal: newimg[m,n]=int((Dd_internal-Dd_min)/(Ds_internal-Ds_min)*(img[m,n]-Ds_min)+Dd_min) else: newimg[m,n