最近老師留了幾個作業(yè),雖然用opencv很簡單一句話就出來了,但是還沒用python寫過。在官方文檔中的tutorial中的threshold里,看到可以創(chuàng)建兩個滑動條來選擇type和value,決定用python實現(xiàn)一下
注意python中的全局變量,用global聲明
開始出現(xiàn)了一些問題,因為毀掉函數(shù)每次只能傳回一個值,所以每次只能更新value,后來就弄了兩個毀掉函數(shù),這個時候,又出現(xiàn)了滑動其中一個,另一個的值就會變?yōu)槟J值的情況,這個時候猜想是全局變量的問題,根據(jù)猜想改動之后果然是。
感覺還有更簡單的方法,不需要設置兩個回調(diào)參數(shù),對python不是很熟悉,時間有限,先不折騰了
python+opencv實現(xiàn)高斯平滑濾波
python+opencv實現(xiàn)霍夫變換檢測直線
(2016-5-10)到OpenCV-Python Tutorials's documentation!可以下載
代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# -*- coding: utf-8 -*- import cv2 #兩個回調(diào)函數(shù) def thresholdType(threshold_type): global THRESHOLD_TYPE THRESHOLD_TYPE = threshold_type print threshold_TYPE, threshold_VALUE ret, dst = cv2.threshold(scr, THRESHOLD_VALUE, max_value, THRESHOLD_TYPE) cv2.imshow(window_name,dst) def thresholdValue(threshold_value): global THRESHOLD_VALUE THRESHOLD_VALUE = threshold_value print threshold_TYPE, threshold_VALUE ret, dst = cv2.threshold(scr, THRESHOLD_VALUE, max_value, THRESHOLD_TYPE) cv2.imshow(window_name,dst) #全局變量 """ "Type: 0: Binary 1: Binary Inverted 2: Truncate 3: To Zero 4: To Zero Inverted" """ THRESHOLD_VALUE = 0 THRESHOLD_TYPE = 3 max_value = 255 max_type = 4 max_BINARY_value = 255 window_name = "Threshold Demo" trackbar_type = "Type" trackbar_value = "Value" #讀入圖片,模式為灰度圖,創(chuàng)建窗口 scr = cv2.imread( "G:\homework\SmallTarget.png" , 0 ) cv2.namedWindow(window_name) #創(chuàng)建滑動條 cv2.createTrackbar( trackbar_type, window_name, \ threshold_type, max_type, thresholdType) cv2.createTrackbar( trackbar_value, window_name, \ threshold_value, max_value, thresholdValue ) #初始化 thresholdType( 0 ) if cv2.waitKey( 0 ) = = 27 : cv2.destroyAllWindows() |
執(zhí)行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import threshold >>> reload (threshold) 0 0 2 0 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 10 1 12 1 13 1 16 1 18 |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xieyi4650/article/details/51346798