二值化

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

highlight=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" alt="" />

二值化例如以下:
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()

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

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

  1. python实现超大图像的二值化方法

    一,分块处理超大图像的二值化问题   (1) 全局阈值处理  (2) 局部阈值 二,空白区域过滤 三,先缩放进行二值化,然后还原大小 np.mean() 返回数组元素的平均值 np.std() 返回数 ...

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

    拉普拉斯线性滤波,.边缘检測  . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...

  3. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  4. 数学之路-python计算实战(9)-机器视觉-图像插值仿射

    插值 Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation – interpol ...

  5. 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)

    Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...

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

    图像产生加性零均值高斯噪声.在灰度图上加上噪声,加上噪声的方式是每一个点的灰度值加上一个噪声值.噪声值的产生方式为Box-Muller算法生成高斯噪声. 在计算机模拟中,常常须要生成正态分布的数值.最 ...

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

    拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...

  8. 数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)

    # -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #归一化块滤波 import cv2 import numpy as np fn="tes ...

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

    filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...

随机推荐

  1. 链表(三)——链表删除冗余结点&插入结点到有序链表

    1.一个以递增方式排列的链表,去掉链表中的冗余值. 思路一:设有两个指针p和q.使p不动,q依次往后循环直到p->data不等于q->data,再将中间的冗余数据删除. 思路二:设有两个指 ...

  2. 【例题3-5 UVA - 1583】Digit Generator

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] for (int i = 1;i <= n;i++) { 算出i是哪一个的生成元. 假设是y. 则ans[y] = min(a ...

  3. HASH算法具体解释

    做了几年开发,一直不理解HASH算法的原理.今天偶从百度知道上看到一个牛人神一样的理解: 这个问题有点难度.不是非常好说清楚. 我来做一个比喻吧. 我们有非常多的小猪,每一个的体重都不一样,假设体重分 ...

  4. Android中图形截取的方式介绍

    在Android的应用中,有时候我们想仅仅显示一部分图像,这时候就要求图形截图. 1.随意截取图像的方法,以下我们具体介绍一下android中的重要类--Bitmap public final cla ...

  5. [LeetCode][Java] Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  6. jquery ajax 请求时间

    $.ajax({ url:'JsLongPollingMsgServlet', type:'post', dataType:'json', data:{"pageMsgNum":$ ...

  7. 【35.02%】【codeforces 734A】Vladik and flights

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. SocketChannel API用法

    java.nio.channels 类 SocketChannel java.lang.Object java.nio.channels.spi.AbstractInterruptibleChanne ...

  9. php汉字 字节数组转换

    <?php function stringToByteArray($str,$charset) { $str = iconv($charset,'UTF-16',$str); preg_matc ...

  10. 【35.00%】【z13】&&【b093】最优贸易

    [题解] 这题就是要在n个点里面选一个花费最小的点.然后找一个花费最大的点.两者之差为最大值. 但是最大值的点要在最小值的点之后出现.且走到后者之后要能够到达N号节点.为了处理掉环.先用tarjan进 ...