最近老師留了幾個(gè)作業(yè),雖然用opencv很簡(jiǎn)單一句話就出來(lái)了,但是還沒(méi)用python寫(xiě)過(guò)。在官方文檔中的tutorial中的threshold里,看到可以創(chuàng)建兩個(gè)滑動(dòng)條來(lái)選擇type和value,決定用python實(shí)現(xiàn)一下
注意python中的全局變量,用global聲明
開(kāi)始出現(xiàn)了一些問(wèn)題,因?yàn)闅У艉瘮?shù)每次只能傳回一個(gè)值,所以每次只能更新value,后來(lái)就弄了兩個(gè)毀掉函數(shù),這個(gè)時(shí)候,又出現(xiàn)了滑動(dòng)其中一個(gè),另一個(gè)的值就會(huì)變?yōu)槟J(rèn)值的情況,這個(gè)時(shí)候猜想是全局變量的問(wèn)題,根據(jù)猜想改動(dòng)之后果然是。
感覺(jué)還有更簡(jiǎn)單的方法,不需要設(shè)置兩個(gè)回調(diào)參數(shù),對(duì)python不是很熟悉,時(shí)間有限,先不折騰了
(2016-5-10)到OpenCV-Python Tutorials's documentation!可以下載
代碼
# -*- coding: utf-8 -*- import cv2#兩個(gè)回調(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 = 0THRESHOLD_TYPE = 3max_value = 255max_type = 4max_BINARY_value = 255window_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)建滑動(dò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í)行
import threshold>>> reload(threshold)0 02 01 01 11 21 31 41 51 61 71 81 101 121 131 161 18
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選