数学思想方法-python计算战(8)-机器视觉-二值化
- 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).
THRESH_BINARY

THRESH_BINARY_INV

THRESH_TRUNC

THRESH_TOZERO

THRESH_TOZERO_INV

二值化
hreshold
Applies a fixed-level threshold to each array element.
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 :
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"
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbXloYXNwbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" /> 二值化例如以下:
import cv2 fn="test3.jpg" |
版权声明:本文博主原创文章。博客,未经同意不得转载。
数学思想方法-python计算战(8)-机器视觉-二值化的更多相关文章
- python实现超大图像的二值化方法
一,分块处理超大图像的二值化问题 (1) 全局阈值处理 (2) 局部阈值 二,空白区域过滤 三,先缩放进行二值化,然后还原大小 np.mean() 返回数组元素的平均值 np.std() 返回数 ...
- 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波
拉普拉斯线性滤波,.边缘检測 . When ksize == 1 , the Laplacian is computed by filtering the image with the follow ...
- 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)
我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是 是图像中全部的灰度数, 是图像中全部的像素数, 实际上是图像的直方图,归一化到 . 把 作为相应于 的累计概率 ...
- 数学之路-python计算实战(9)-机器视觉-图像插值仿射
插值 Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst interpolation – interpol ...
- 数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)
Blurs an image using the median filter. C++: void medianBlur(InputArray src, OutputArray dst, int ks ...
- 数学之路-python计算实战(7)-机器视觉-图像产生加性零均值高斯噪声
图像产生加性零均值高斯噪声.在灰度图上加上噪声,加上噪声的方式是每一个点的灰度值加上一个噪声值.噪声值的产生方式为Box-Muller算法生成高斯噪声. 在计算机模拟中,常常须要生成正态分布的数值.最 ...
- 数学之路-python计算实战(20)-机器视觉-拉普拉斯算子卷积滤波
拉普拉斯算子进行二维卷积计算,线性锐化滤波 # -*- coding: utf-8 -*- #线性锐化滤波-拉普拉斯算子进行二维卷积计算 #code:myhaspl@myhaspl.com impor ...
- 数学之路-python计算实战(15)-机器视觉-滤波去噪(归一化块滤波)
# -*- coding: utf-8 -*- #code:myhaspl@myhaspl.com #归一化块滤波 import cv2 import numpy as np fn="tes ...
- 数学之路-python计算实战(19)-机器视觉-卷积滤波
filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...
随机推荐
- 【重拾Effective Java】一
之前看这本<Effective Java(第二版)>都是非常早曾经了.这本书确实是本好书.须要细嚼慢咽,每次看都有不同的体验. 在此写博客巩固一下. 第一章.创建和销毁对象 考虑用静态工厂 ...
- java回调函数这样说,应该明确了吧!
有哥们问我回调怎么用,回调怎么理解? 怎么说好呢,仅仅可意会不可言传呐,非也,回调在实际开发中使用频率事实上是非常高的,恰好我小时候也被回调函数欺负过,居然问了,那么肯定要好好分享一下我的一些经验. ...
- PHP Filesystem 函数(文件系统函数)(每日一课的内容可以从php参考手册上面来)
PHP Filesystem 函数(文件系统函数)(每日一课的内容可以从php参考手册上面来) 一.总结 1.文件路径中的正反斜杠:当在 Unix 平台上规定路径时,正斜杠 (/) 用作目录分隔符.而 ...
- 如何设计一个基于mysql的消息系统
https://segmentfault.com/a/1190000012255186
- php实现调整数组顺序使奇数位于偶数前面
php实现调整数组顺序使奇数位于偶数前面 一.总结 1.array_push()两个参数,$arr在前 2.array_merge()的返回值是数组 二.php实现调整数组顺序使奇数位于偶数前面 ...
- IntelliJ IDEA设置鼠标悬浮提示
测试代码; public interface MyInterface { /** * 我是接口方法的注释 * @param num1 我是接口方法入参的注释 * @return 我是接口方法返回值的注 ...
- Java内部抛出异常外部不能catch问题分析
今天在论坛看到一篇关于异常处理的文章,异常处理机制详解开头就搬出了这样一个例子: public class TestException { public TestException() { } boo ...
- ios开发网络学习六:设置队列请求与RunLoop
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- android的edittext设置输入限制,只能输入数字
EditText的属性里面已经封装好了相关的设置,上一篇文章里面也提到了,不熟悉的可以去查看上一篇EditText属性大全,这里着重讲输入限制的属性: android:digits="123 ...
- android安卓开发基础小笔记,添加按钮事件,打开新窗体,窗体传值,回传
给一个按钮添加onclick事件 //获取按钮对象 Button Aiyo = (Button)findViewById(R.id.button1); Aiyo.setOnClickListener( ...