二值化

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. 三个Bootstrap免费字体和图标库

    前言:Bootstrap 简洁.直观.强悍.移动设备优先的前端开发框架,让web开发更迅速.简单 ,深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快, 组件无数 ...

  2. ArcGIS中ObjectID,FID和OID字段区别

    lysc_forever 原文 ArcGIS中ObjectID,FID和OID字段有什么区别 ArcGIS Desktop 独立的表和属性表都有一个ObjectID字段.这个字段中包含一个唯一的,长整 ...

  3. js 第四章 cookie的操作

    js 第四章 cookie的操作 一.学习要点 掌握cookie的简单应用 二. js 第四章 cookie的操作 了解cookie 什么是cookie? cookie 是存储于访问者的计算机中的变量 ...

  4. 数据类型总结——Boolean类型(布尔类型)

    相关文章 简书原文:https://www.jianshu.com/p/e5c75d4be636 数据类型总结——概述:https://www.cnblogs.com/shcrk/p/9266015. ...

  5. JAVA 中无锁的线程安全整数 AtomicInteger介绍和使用

    Java 中无锁的线程安全整数 AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候, 不可避免的会用到synchron ...

  6. jquery-11 如何制作鼠标右键菜单

    jquery-11 如何制作鼠标右键菜单 一.总结 一句话总结:核心原理:找到右键菜单事件contextmenu,return false去掉默认事件,然后判断用户是否点的右键,然后在鼠标的位置显示菜 ...

  7. Ubuntu10.04下安装Qt4和创建第一个Qt程序

    1.首先安装Qt4并采用Qt Creator进行开发演示 (1)在Terminal中输入: sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfi ...

  8. AOP 专题

    Spring框架有两个核心思想:IOC和AOP Spring IOC指的是控制翻转,使用普通JAVA Bean,在运行时由Spring容器统一管理和串联,服务于不同的流程,在开发过程中对Spring ...

  9. js,jquery遍历数组,对象

    each的用法  1.数组中的each 复制代码 var arr = [ "one", "two", "three", "four ...

  10. 【54.08%】【BZOJ 1941】Hide and Seek

    Time Limit: 16 Sec  Memory Limit: 162 MB Submit: 919  Solved: 497 [Submit][Status][Discuss] Descript ...