【微语】立志要如山,行道要如水。不如山,不能坚定,不如水,不能曲达

 import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt img = cv.imread(r'pictures\gradient.png')
h , w ,ch = img.shape ret , thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret , thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret , thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret , thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret , thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV) titles = ['orignal image','binary','binary_inv','trunc','tozero','tozero_inv']
images = [img,thresh1,thresh2,thresh3,thresh4,thresh5] for i in range(len(images)):
plt.subplot(2,3,i+1),plt.imshow(images[i])
plt.title(titles[i])
plt.xticks(())
plt.yticks(()) #隐藏y轴 plt.show()


import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt img = cv.imread(r'pictures\noisy2.png')
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) #global thresholding
ret1,binary1 = cv.threshold(gray,127,255,cv.THRESH_BINARY)
#Otsu's thresholding
ret2,binary2 = cv.threshold(gray,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
#Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(gray,(5,5),0)
ret3,binary3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# cv.imshow('binary1',binary1)
# cv.imshow('binary2',binary2)
# cv.imshow('blur',blur)
# cv.imshow('binary3',binary3)
#Plot all the images and their histograms

images = [img,0,binary1,
img,0,binary2,
blur,0,binary3]
titles = ['original noisy image','histogram','global threshold(val=127)',
'original noisy image','histogram',"Otsu's threshold",
'Gaussian filter image','histogram',"Otsu's threshold(Gaussian)"]
"""
使用pyplot中画直方图的方法plt.hist(),注意它的参数是一维数组
故使用numpy的ravel()方法或者flatten()方法, 将多维数组转为一维数组
#for循环每次打印出一行3幅图
"""
for i in range(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray') #plt,imshow(img,'gray') 灰度图
plt.title(titles[i*3]),plt.xticks(()),plt.yticks(()) plt.subplot(3,3,i*3+2),plt.hist(images[i*3].flatten(),256)
plt.title(titles[1]),plt.xticks(()),plt.yticks(()) plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[2]),plt.xticks(()),plt.yticks(()) plt.show()

【python-opencv】15-图像阈值的更多相关文章

  1. 使用Python+OpenCV进行图像模板匹配(Match Template)

    2017年9月22日 BY 蓝鲸 LEAVE A COMMENT 本篇文章介绍使用Python和OpenCV对图像进行模板匹配和识别.模板匹配是在图像中寻找和识别模板的一种简单的方法.以下是具体的步骤 ...

  2. python+opencv实现图像自适应阈值的均衡化

    内容涉及:列表遍历,图像均衡化,图像通道分离与合并 import cv2 import numpy as np import os for path in open("org_junheng ...

  3. opencv之图像阈值化处理

    一.函数简介 1.threshold-图像简单阈值化处理 函数原型:threshold(src, thresh, maxval, type, dst=None) src:图像矩阵 thresh:阈值 ...

  4. python+opencv实现图像缩放

    x, y = img_.shape[0:2] img_ = cv2.resize(img_, (int(y/2), int(x/2))) 实现图像长宽缩小为原来的一半

  5. python+opencv检测图像清晰度

    直接上代码,list_jian.txt为待检测图像路径列表 import cv2 import numpy as np import os for path in open("list_ji ...

  6. python实现遥感图像阈值分割

    1.阈值分割 import os import cv2 import numpy as np import matplotlib.pyplot as plt from osgeo import gda ...

  7. python opencv:图像的一些属性与操作

    img = cv.imread(xxx) # 常用的有以下属性 type(img) # img的数据类型 img.shape # img的结构 img.size # img的大小 img.dtype ...

  8. Python+opencv打开修图的正确方式get

    先逼逼两句: 图像是 Web 应用中除文字外最普遍的媒体格式. 流行的 Web 静态图片有 JPEG.PNG.ICO.BMP 等.动态图片主要是 GIF 格式.为了节省图片传输流量,大型互联网公司还会 ...

  9. opencv学习之路(13)、图像阈值化threshold

    一.图像阈值化简介 二.固定阈值 三.自适应阈值 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src ...

  10. opencv图像阈值操作

    使用threshold方法和adaptivethreshold方法对图像进行阈值分割操作. 1.使用threshold方法,设置一个阈值,将大于阈值的值变换为最大值,小于阈值的值变换为0. #-*- ...

随机推荐

  1. [XPath] XPath 与 lxml (三)XPath 坐标轴

    本章我们将沿用上一章的 XML 示例文档. XPath 坐标轴 坐标轴用于定义当对当前节点的节点集合. 坐标轴名称 含义 ancestor 选取当前节点的所有先辈元素及根节点. ancestor-or ...

  2. mybatis 之 resultType="Integer"

    public class EcPromoteRuleAdditionalNew extends BaseBO { private String[] promoteRuleIds; public Str ...

  3. tablayout在中间显示

    <android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_wid ...

  4. php_admin_value open_basedir 引起的上传文件失败解决方法

    为了安全,我们通常会在虚拟主机设置中,加入这一行php_admin_value open_basedir "/usr/local/apache/htdocs/www"但这会导致mo ...

  5. .NET二级域名共享Session

    ASP.NET二级域名站点共享Session状态 今天, 我要写的是如何在二级域名站点之间,主站点和二级域名站点之间共享Session. 首先, Session要共享,站点之间SessionID必须要 ...

  6. 对Android 开发者有益的 40 条优化建议(转)

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...

  7. java基础---->git的使用(一)

    这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明.人的天性便是这般凉薄,只要拿更好的来换,一定舍得. Git的一些使用 一.在码云建立好仓库之后 ...

  8. nginx(一)----ubuntu14.04下安装nginx

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 此文章中用到的软件下载地址: 链接: http://pan.baidu.com/s/ ...

  9. 去除select边框和三角-----appearance:none

    今天发现一个比较有意思的属性,appearance:none 可能有朋友不认识,但是有一个标签你肯定认识:select. 这个标签的样式是这样的: 一般情况下,我们所使用的border:0; 去除边框 ...

  10. Unity3D 加密 Assembly-CSharp.dll (Android平台) 防止反编译【转】

    转自 http://blog.csdn.net/u013108312/article/details/54234439