日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

cv2.threshholding()简单阈值、自适应阈值,Octus阈值

發布時間:2023/11/27 生活经验 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cv2.threshholding()简单阈值、自适应阈值,Octus阈值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@[TOC](cv2.threshholding()簡單閾值、自適應閾值,Octus閾值

這篇博客將延續上一篇簡單閾值處理,繼續介紹自適應閾值及Octus閾值;

  • 簡單閾值詳情見: https://blog.csdn.net/qq_40985985/article/details/115379934 采取全局閾值,并不適用于所有圖像;
  • 自適應閾值算法針對圖像中的一小部分區域計算閾值。對于同一幅圖像的不同區域,采用不同的閾值,尤其是不同光照的圖像,效果會更好;
  • Octus閾值

1. 效果圖

自適應閾值效果圖如下:
原始圖 VS 簡單閾值 VS 均值閾值 VS 高斯閾值效果圖:

Octus閾值效果圖如下:官方的效果要好一些~

2. cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)

cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)

  • img 圖像
  • cv2.ADAPTIVE_THRESH_MEAN_C : 閾值是鄰域面積的平均值。
    cv2.ADAPTIVE_THRESH_GAUSSIAN_C:閾值是鄰域值的加權和,其中權重是高斯窗口;
  • blockSize:鄰域面積的大小
  • C:常數,從計算的平均值或加權平均值中減去。

3. 源碼

3.1 自適應閾值

# 簡單閾值使用全局值作為閾值,在不同區域的不同光照條件下,它可能并不適用于所有的情況。
# 自適應閾值算法針對圖像中的一小部分區域計算閾值。因此,對于同一幅圖像的不同區域,采用不同的閾值,對于不同光照的圖像,效果會更好;import cv2
from matplotlib import pyplot as pltimg = cv2.imread('gw.jpg', 0)
img = cv2.medianBlur(img, 5)ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)titles = ['Original Image', 'Global Thresholding (v = 127)','Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]for i in range(4):plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')plt.title(titles[i])plt.xticks([]), plt.yticks([])
plt.show()

3.2 Octus閾值

import cv2
from matplotlib import pyplot as pltimg = cv2.imread('gw.jpg', 0)# 簡單閾值
ret1, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)# Otsu's閾值
ret2, th2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)# 高斯濾波后Otsu's閾值
blur = cv2.GaussianBlur(img, (5, 5), 0)
ret3, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)# 繪制所有圖像及直方圖~
images = [img, 0, th1,img, 0, th2,blur, 0, th3]
titles = ['Original Noisy Image', 'Histogram', 'Global Thresholding (v=127)','Original Noisy Image', 'Histogram', "Otsu's Thresholding",'Gaussian filtered Image', 'Histogram', "Otsu's Thresholding"]for i in range(3):plt.subplot(3, 3, i * 3 + 1), plt.imshow(images[i * 3], 'gray')plt.title(titles[i * 3]), plt.xticks([]), plt.yticks([])plt.subplot(3, 3, i * 3 + 2), plt.hist(images[i * 3].ravel(), 256)plt.title(titles[i * 3 + 1]), plt.xticks([]), plt.yticks([])plt.subplot(3, 3, i * 3 + 3), plt.imshow(images[i * 3 + 2], 'gray')plt.title(titles[i * 3 + 2]), plt.xticks([]), plt.yticks([])
plt.show()

參考

  • https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html#thresholding

總結

以上是生活随笔為你收集整理的cv2.threshholding()简单阈值、自适应阈值,Octus阈值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。