最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python+opencv实现阈值分割
时间:2018-12-28 编辑:猪哥 来源:一聚教程网
注意python中的全局变量,用global声明
开始出现了一些问题,因为毁掉函数每次只能传回一个值,所以每次只能更新value,后来就弄了两个毁掉函数,这个时候,又出现了滑动其中一个,另一个的值就会变为默认值的情况,这个时候猜想是全局变量的问题,根据猜想改动之后果然是。
代码
# -*- coding: utf-8 -*- import cv2 #两个回调函数 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" #读入图片,模式为灰度图,创建窗口 scr = cv2.imread("G:homeworkSmallTarget.png",0) cv2.namedWindow(window_name) #创建滑动条 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()
执行
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
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27