服务器之家

服务器之家 > 正文

opencv函数threshold、adaptiveThreshold、Otsu二值化的实现

时间:2021-09-19 00:44     来源/作者:假小牙

threshold:固定阈值二值化,

  1. ret, dst = cv2.threshold(src, thresh, maxval, type)
  • src: 输入图,只能输入单通道图像,通常来说为灰度图
  • dst: 输出图
  • thresh: 阈值
  • maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
  • type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV

官方文档的示例代码:

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('gradient.png',0)
  5. ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
  6. ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
  7. ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
  8. ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
  9. ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
  10. titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
  11. images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
  12. for i in xrange(6):
  13. plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
  14. plt.title(titles[i])
  15. plt.xticks([]),plt.yticks([])
  16. plt.show()

结果为:

opencv函数threshold、adaptiveThreshold、Otsu二值化的实现

adaptiveThreshold:自适应阈值二值化

自适应阈值二值化函数根据图片一小块区域的值来计算对应区域的阈值,从而得到也许更为合适的图片。

  1. dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C)
  • src: 输入图,只能输入单通道图像,通常来说为灰度图
  • dst: 输出图
  • maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值
  • thresh_type: 阈值的计算方法,包含以下2种类型:cv2.ADAPTIVE_THRESH_MEAN_C; cv2.ADAPTIVE_THRESH_GAUSSIAN_C.
  • type:二值化操作的类型,与固定阈值函数相同,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV.
  • Block Size: 图片中分块的大小
  • C :阈值计算方法中的常数项

官方文档的示例代码:

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('sudoku.png',0)
  5. img = cv2.medianBlur(img,5)
  6. ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
  7. th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
  8. cv2.THRESH_BINARY,11,2)
  9. th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
  10. cv2.THRESH_BINARY,11,2)
  11. titles = ['Original Image', 'Global Thresholding (v = 127)',
  12. 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
  13. images = [img, th1, th2, th3]
  14. for i in xrange(4):
  15. plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
  16. plt.title(titles[i])
  17. plt.xticks([]),plt.yticks([])
  18. plt.show()

结果为:

opencv函数threshold、adaptiveThreshold、Otsu二值化的实现

Otsu's Binarization: 基于直方图的二值化

Otsu's Binarization是一种基于直方图的二值化方法,它需要和threshold函数配合使用。

Otsu过程:
1. 计算图像直方图;
2. 设定一阈值,把直方图强度大于阈值的像素分成一组,把小于阈值的像素分成另外一组;
3. 分别计算两组内的偏移数,并把偏移数相加;
4. 把0~255依照顺序多为阈值,重复1-3的步骤,直到得到最小偏移数,其所对应的值即为结果阈值。

官方文档的示例代码:

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread('noisy2.png',0)
  5. # global thresholding
  6. ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
  7. # Otsu's thresholding
  8. ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  9. # Otsu's thresholding after Gaussian filtering
  10. blur = cv2.GaussianBlur(img,(5,5),0)
  11. ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  12. # plot all the images and their histograms
  13. images = [img, 0, th1,
  14. img, 0, th2,
  15. blur, 0, th3]
  16. titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
  17. 'Original Noisy Image','Histogram',"Otsu's Thresholding",
  18. 'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
  19. for i in xrange(3):
  20. plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
  21. plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
  22. plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
  23. plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
  24. plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
  25. plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
  26. plt.show()

结果为:

opencv函数threshold、adaptiveThreshold、Otsu二值化的实现

参考文献:http://docs.opencv.org/3.2.0/d7/d4d/tutorial_py_thresholding.html

到此这篇关于opencv函数threshold、adaptiveThreshold、Otsu二值化的实现的文章就介绍到这了,更多相关opencv threshold、adaptiveThreshold、Otsu内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/sinat_21258931/article/details/61418681

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部