OpenCV之二值化處理
二值化處理將使用到OpenCV中提供的閾值(threshold)函數:
cv2.threshold(灰度圖,閾值,設定值,方法)
其中方法有五個,如下:cv2.THRESH_BINARY、cv2.THRESH_BINARY_INV、cv2.THRESH_TRUNC、cv2.THRESH_TOZERO、cv2.THRESH_TOZERO_INV
# -*- coding: utf-8 -*-"""Created on Fri Sep 22 21:54:34 2017@author: shigan"""import cv2#相對路徑下讀取圖片doge = cv2.imread(doge.jpg)#灰度化處理grayImage = cv2.cvtColor(doge,cv2.COLOR_BGR2GRAY)#cv2.imshow(GRAY,grayImage)#二值化處理,低於閾值的像素點灰度值置為0;高於閾值的值置為參數3ret,thresh1 = cv2.threshold(grayImage,127,255,cv2.THRESH_BINARY)cv2.imshow(BINARY,thresh1)#大於閾值的像素點灰度值置為0;小於閾值置為參數3ret,thresh2 = cv2.threshold(grayImage,127,200,cv2.THRESH_BINARY_INV)cv2.imshow(BINARY_INV,thresh2)#小於閾值的像素點灰度值不變,大於閾值的像素點置為該閾值ret,thresh3 = cv2.threshold(grayImage,127,255,cv2.THRESH_TRUNC)cv2.imshow(TRUNC,thresh3)#小於閾值的像素點灰度值不變,大於閾值的像素點置為0,其中參數3任取ret,thresh4 = cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO)cv2.imshow(BINARY_TOZERO,thresh4)#大於閾值的像素點灰度值不變,小於閾值的像素點置為0,其中參數3任取ret,thresh5 = cv2.threshold(grayImage,127,255,cv2.THRESH_TOZERO_INV)cv2.imshow(BINARY_TOZERO_INV,thresh5)
原圖:
BINARY:
BINARY_INV:
TRUNC:
TOZERO:
TOZERO_INV:
歡迎交流
推薦閱讀:
※ImagePy 簡介
※【opencv學習筆記六】圖像的ROI區域選擇與複製
※opencv提供了多少個特徵點的descriptor?
※【opencv學習筆記三】opencv3.4.0數據類型解釋
TAG:OpenCV |