【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. #-*- ...
随机推荐
- DDMS调试信息
1.可以使用System.out.println() 2.android.util包下的Log类 该类可以将信息以日志的形式输出到LogCat中: import android.util.Log; S ...
- centos6.4安装GCC
1. Last login: Mon Aug 4 11:46:15 2014 from 10.3.7.128 [jifeng@jifeng04 ~]$ ls hadoop jdk1.7.0_45 ...
- PS合成以及分解GIF
http://jingyan.baidu.com/article/3052f5a1c91f0497f31f862a.html 百度上的这个说明很详细了 这里就简单注明一下: PS 时间轴:用来创建动画 ...
- 微信小程序源码案例大全
微信小程序demo:足球,赛事分析 小程序简易导航 小程序demo:办公审批 小程序Demo:电魔方 小程序demo:借阅伴侣 微信小程序demo:投票 微信小程序demo:健康生活 小程序demo: ...
- 关于PHP中的 serialize () 和 unserialize () 的使用(即关于PHP中的值与已存储的表示的相互转换)
有时,我们会碰到这样的数据(字符串) 1 a:3:{i:0;s:44:"/Uploads/images/2017-07-21/5971a9a08ad57.png";i:1;s:44 ...
- 【easyswoole】 解决安装报错
在使用swoole 创建项目时候,报错 创建命令 composer create-project easyswoole/app easyswoole 错误信息: 解决办法,切换composer 源 镜 ...
- SharpGL学习笔记(四) 正射投影
上节谈到投影变换分为透视投影(perspective projection)和正射投影(orthographic projection)两种. 透视投影我们已经介绍过了, 现在谈谈正视投影. 正射投影 ...
- Linux应急处理操作手册
基础准备--命令防篡改与命令记录 很多黑客入侵到操作系统后,会做两个常见的操作unset history和替换命令文件(或者对应的链接库文件),针对这两点要做好记录shelllog并且检查链接库类文件 ...
- Centos7.0 配置docker 镜像加速
在Docker Hub官网上注册帐号,即可下载使用仓库里的全部的docker镜像.而因为网络原因,国内的开发者没办法流畅的下载镜像,经常会出现下载中断的错误.解决方法就是使用国内的容器Hub加速服务, ...
- github相关资料记录
github官方配ssh api:https://help.github.com/articles/generating-ssh-keys 简书hexo静态博客搭建:http://www.jiansh ...