python+opencv實現(xiàn)閾值分割
最近老師留了幾個作業(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ù)猜想改動之后果然是。
感覺還有更簡單的方法,不需要設(shè)置兩個回調(diào)參數(shù),對python不是很熟悉,時間有限,先不折騰了
python+opencv實現(xiàn)高斯平滑濾波
python+opencv實現(xiàn)霍夫變換檢測直線
(2016-5-10)到OpenCV-Python Tutorials's documentation!可以下載
代碼
# -*- 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í)行
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python使用requests實現(xiàn)發(fā)送帶文件請求功能
這篇文章主要介紹了python使用requests實現(xiàn)發(fā)送帶文件請求,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
Python讀取excel文件中帶公式的值的實現(xiàn)
這篇文章主要介紹了Python讀取excel文件中帶公式的值的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python使用matplotlib實現(xiàn)基礎(chǔ)繪圖功能示例
這篇文章主要介紹了Python使用matplotlib實現(xiàn)基礎(chǔ)繪圖功能,結(jié)合實例形式分析了Python基于matplotlib實現(xiàn)正弦、余弦圖形及多軸圖的相關(guān)繪制操作技巧,需要的朋友可以參考下2018-07-07
Django認證系統(tǒng)user對象實現(xiàn)過程解析
這篇文章主要介紹了Django認證系統(tǒng)user對象實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03

