【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. #-*- ...
随机推荐
- 使用 urllib 设置代理服务
(1) 如果我们一直用同一个IP去请求同一个网站上的网页,久了之后可能会被该网站服务器屏蔽,因此我们可以使用代理IP来发起请求,代理实际上指的就是代理服务器(2) 当我们使用代理IP发起请求时,服务器 ...
- 在eclipse里配置Android ndk环境 适用于windows mac 和linux(转)
在eclipse里配置Android ndk环境 适用于windows mac 和linux(转) 2012-02-27 13:02:16| 分类: android | 标签:java prog ...
- Unity3d OnApplicationPause与OnApplicationFocus
在手机游戏当中,会碰到“强制暂停”,如:锁屏.接电话或短信之类的.如果“强制暂停”时间过长,网络游戏有时得重新登录等事件. 而Unity3d,Android Plugins中的UnityPlayer. ...
- [NodeJS] Node.js 与 V8 的故事
要说Node.js的历史,就不得不说说V8历史.在此之前我们先一句话描述一下什么是Node.js:Node.js是一个基于Google Chrome V8 Javascript引擎之上的平台,用以创建 ...
- 第二十二篇:基于UDP的一对回射客户/服务器程序
前言 之前曾经学习过一对回射客户/服务器程序的例子,不过那个是基于TCP协议的.本文将讲解另一对回射客户/服务器程序,该程序基于UDP协议. 由于使用的协议不同,因此编写出的程序也有本质上的区别,应将 ...
- List 集合的N层遍历
package com.j1.cms.model; import java.io.Serializable; import java.util.List; /** * Created by wangc ...
- 通过java的i/o机制进行图片流的存储以及对网络图片的存储
存储内地图片思路:首先把原有的图片以流的方式读取出来,再以流的方式存储到目标文件: package imgStream; import java.io.*; public class ImgStrea ...
- 《转载》Eclipse项目上传码云
本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...
- WP8.1学习系列(第十七章)——交互UX之输入和反馈模式
如果你将 Windows 应用商店应用设计为触摸交互,则可免费获取对触摸板.鼠标.笔和键盘交互的支持.你的用户可以从一种输入法切换到另一种,而不会丧失应用体验的感觉.将键盘插入平板电脑?没问题.你的应 ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...