python 影像處理

來源:互聯網
上載者:User

標籤:color   block   trunc   val   部分   container   target   set   truncate   

cv2.threshold介紹,如所示,每個數字代表起本身的灰階值大小

cv2.threshod接收5種類參數形式,分別是cv2.THRESH_BINARY,cv2.THRESH_BINARY_INV,cv2.THRESH_TRUNC ,cv2.THRESH_TOZERO,cv2.THRESH_TOZERO_INV

(1)THRESH_BINARY(Binary Thresholding)

# Binary Threshold,原映像素>閾值,該像素點的值保持不變,否則為0.即大於閾值的部分變成像素最大值,小於閾值的部分變為0if src(x,y) > thresh  dst(x,y) = maxValueelse  dst(x,y) = 0

 

(2)THRESH_BINARY_INV(Inverse Binary Thresholding)

# Inverse Binary Threshold,原映像素<閾值,該像素點的值為0,否則保持不變.與(1)相反,大於閾值的部分變為0,小於閾值的部分變成像素最大值if src(x,y) > thresh  dst(x,y) = 0else  dst(x,y) = maxValue

(3)THRESH_TRUNC(Truncate Thresholding)

# Truncate Threshold,原映像素>閾值,該像素點的值等於閾值,否則保持不變.即,將所有像素變成閾值以下水平if src(x,y) > thresh  dst(x,y) = threshelse  dst(x,y) = src(x,y)

(4)THRESH_TOZERO(Threshold to Zero)

# Threshold to Zeroif src(x,y) > thresh  dst(x,y) = src(x,y)else  dst(x,y) = 0

(5)THRESH_TOZERO_INV(Inverted Threshold to Zero)

# Inverted Threshold to Zeroif src(x,y) > thresh  dst(x,y) = 0else  dst(x,y) = src(x,y)

 以為例,python代碼為:

‘‘‘     OpenCV Threshold Example  Copyright 2015 by Satya Mallick <[email protected]>‘‘‘# import opencv import cv2 # Read image src = cv2.imread("threshold.png", cv2.IMREAD_GRAYSCALE); # Basic threhold example th, dst = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY); cv2.imwrite("opencv-threshold-example.jpg", dst); # Thresholding with maxValue set to 128th, dst = cv2.threshold(src, 0, 128, cv2.THRESH_BINARY); cv2.imwrite("opencv-thresh-binary-maxval.jpg", dst); # Thresholding with threshold value set 127 th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY); cv2.imwrite("opencv-thresh-binary.jpg", dst); # Thresholding using THRESH_BINARY_INV th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY_INV); cv2.imwrite("opencv-thresh-binary-inv.jpg", dst); # Thresholding using THRESH_TRUNC th, dst = cv2.threshold(src,127,255, cv2.THRESH_TRUNC); cv2.imwrite("opencv-thresh-trunc.jpg", dst); # Thresholding using THRESH_TOZERO th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO); cv2.imwrite("opencv-thresh-tozero.jpg", dst); # Thresholding using THRESH_TOZERO_INV th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO_INV); cv2.imwrite("opencv-thresh-to-zero-inv.jpg", dst); 

 

結果圖:

                            (1)                                                           (2)                                                          (3)    

                     (4)                                                                (5)
參考串連:

http://www.learnopencv.com/opencv-threshold-python-cpp/

http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html

 

python 影像處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.