【python-opencv】15-图像阈值
【微语】立志要如山,行道要如水。不如山,不能坚定,不如水,不能曲达

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-图像阈值的更多相关文章
- 使用Python+OpenCV进行图像模板匹配(Match Template)
2017年9月22日 BY 蓝鲸 LEAVE A COMMENT 本篇文章介绍使用Python和OpenCV对图像进行模板匹配和识别.模板匹配是在图像中寻找和识别模板的一种简单的方法.以下是具体的步骤 ...
- python+opencv实现图像自适应阈值的均衡化
内容涉及:列表遍历,图像均衡化,图像通道分离与合并 import cv2 import numpy as np import os for path in open("org_junheng ...
- opencv之图像阈值化处理
一.函数简介 1.threshold-图像简单阈值化处理 函数原型:threshold(src, thresh, maxval, type, dst=None) src:图像矩阵 thresh:阈值 ...
- python+opencv实现图像缩放
x, y = img_.shape[0:2] img_ = cv2.resize(img_, (int(y/2), int(x/2))) 实现图像长宽缩小为原来的一半
- python+opencv检测图像清晰度
直接上代码,list_jian.txt为待检测图像路径列表 import cv2 import numpy as np import os for path in open("list_ji ...
- python实现遥感图像阈值分割
1.阈值分割 import os import cv2 import numpy as np import matplotlib.pyplot as plt from osgeo import gda ...
- python opencv:图像的一些属性与操作
img = cv.imread(xxx) # 常用的有以下属性 type(img) # img的数据类型 img.shape # img的结构 img.size # img的大小 img.dtype ...
- Python+opencv打开修图的正确方式get
先逼逼两句: 图像是 Web 应用中除文字外最普遍的媒体格式. 流行的 Web 静态图片有 JPEG.PNG.ICO.BMP 等.动态图片主要是 GIF 格式.为了节省图片传输流量,大型互联网公司还会 ...
- opencv学习之路(13)、图像阈值化threshold
一.图像阈值化简介 二.固定阈值 三.自适应阈值 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src ...
- opencv图像阈值操作
使用threshold方法和adaptivethreshold方法对图像进行阈值分割操作. 1.使用threshold方法,设置一个阈值,将大于阈值的值变换为最大值,小于阈值的值变换为0. #-*- ...
随机推荐
- Perl socket编程
In this article, let us discuss how to write Perl socket programming using the inbuilt socket module ...
- Nginx 域名重定向
假设 www.old.com 为旧的域名,而 www.new.com 为新的域名,要实现当我们访问 new 的时候自动重定向到 old 域名,配置如下: server { //第一种配置方法 serv ...
- 由于OBJ模型的读取引起的Release无问题Debug卡死问题
有些时候会遇到Release版本正常运行,但是Debug无法运行甚至崩溃,原因有很多种,这里记录一下由于模型文件读取引起的Debug问题. 项目中需要读取一个obj模型文件,30M左右,Debug模式 ...
- CopyTransform
// TransformCopier.cs v 1.1 // homepage: http://wiki.unity3d.com/index.php/CopyTransform using Unity ...
- 《C++标准程序库》笔记之四
本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...
- RabbitMQ笔记四:Binding,Queue,Message概念
Binding详解 黄线部分就是binding Exchange与Exchange,Queue之间的虚拟连接,Binding中可以包含Routing key或者参数 创建binding 注意: ...
- OpenCV——轮廓特征描述
检测出特定轮廓,可进一步对其特征进行描述,从而识别物体. 1. 如下函数,可以将轮廓以多种形式包围起来. // 轮廓表示为一个矩形 Rect r = boundingRect(Mat(contours ...
- Delphi中ClientDataSet的用法小结
Delphi中ClientDataSet的用法小结 TClientDataSet控件继承自TDataSet,其数据存储文件格式扩展名为 .cds,是基于文件型数据存储和操作的控件.该控件封装了对数据进 ...
- 自己实现atoi
bool myatoi(const char *s,int &num) { cout<<(&s)<<endl; num=; while (*s) { ') { ...
- 原生js--兼容获取窗口滚动条位置和窗口大小的方法
各个浏览器对获取获取窗口滚动条位置和窗口大小没有提供统一的API,以下是对其封装,解决兼容性问题 /** * 获取浏览器视口的大小(显示文档的部分) * */function getViewPort ...