python环境下的createTrackbar

Ruff_XY 2016-05-08 04:03:58
想写一个opencv官方文档tutorial里的一个函数,实现用滑动条来控制阈值和类型。
不过官方文档里是用C++写的,我想用python改写。但是这里出了点问题,createTrackbar貌似不能改变全局变量的值
这是我的代码。
问题:执行的时候,我先拖动type的滑动条,打印输出的threshold_type值是变化的,但是当我改变完threshold_type后,再滑动trackbar_value的时候,threshold_type又变为了默认值3.想问一下这是什么情况
# -*- coding: utf-8 -*- 

import cv2

#两个回调函数
def thresholdType(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):
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:\homework\SmallTarget.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 )
#初始化
threshold(0)

if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
...全文
197 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复 1

# -*- coding: utf-8 -*-

import cv2
import numpy as np

gcontrastvalue = 80 # 对比度
gbrightvalue = 80 # 亮度


def contrast(contrastvalue):
desImage = srcImage.copy()
global gcontrastvalue
global gbrightvalue
print('gcontrastvalue:', gcontrastvalue)
print('gbrightvalue:', gbrightvalue)
table = []
for i in range(256):
data = int(i * gcontrastvalue * 0.01 + gbrightvalue)
if data < 0:
table.append(0)
elif data > 255:
table.append(255)
else:
table.append(data)
table = np.array(table, dtype=np.uint8) # 将数组转换为映射矩阵,数据类型与原图像数据保持一致
cv2.LUT(srcImage, table, desImage) # 利用系统函数LUT进行映射矩阵替换原图形数据
cv2.imshow("window1", desImage)
gcontrastvalue = contrastvalue


def bright(brightvalue):
desImage = srcImage.copy()
global gcontrastvalue
global gbrightvalue
print('gcontrastvalue:', gcontrastvalue)
print('gbrightvalue:', gbrightvalue)
table = []
for i in range(256):
data = int(i * gcontrastvalue * 0.01 + gbrightvalue)
if data < 0:
table.append(0)
elif data > 255:
table.append(255)
else:
table.append(data)

table = np.array(table, dtype=np.uint8) # 将数组转换为映射矩阵,数据类型与原图像数据保持一致
cv2.LUT(srcImage, table, desImage) # 利用系统函数LUT进行映射矩阵替换原图形数据
cv2.imshow("window1", desImage)
gbrightvalue = brightvalue


srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/8.jpg")


# print(srcImage.shape) # 获取原图像的尺寸
# print(type(srcImage[0][0][0])) # 获取原图像的数据类型,为后面做映射矩阵服务

cv2.namedWindow("window1")
cv2.createTrackbar("对比度", "window1", 80, 300, contrast)
cv2.createTrackbar("亮 度", "window1", 80, 100, bright)
cv2.imshow("window1", srcImage)
cv2.waitKey(0)
  • 打赏
  • 举报
回复
# -*- coding: utf-8 -*-

import cv2
import numpy as np

gcontrastvalue = 80 # 对比度
gbrightvalue = 80 # 亮度


def contrast(contrastvalue):
desImage = srcImage.copy()
global gcontrastvalue
global gbrightvalue
print('gcontrastvalue:', gcontrastvalue)
print('gbrightvalue:', gbrightvalue)
table = []
for i in range(256):
data = int(i * gcontrastvalue * 0.01 + gbrightvalue)
if data < 0:
table.append(0)
elif data > 255:
table.append(255)
else:
table.append(data)
table = np.array(table, dtype=np.uint8) # 将数组转换为映射矩阵,数据类型与原图像数据保持一致
cv2.LUT(srcImage, table, desImage) # 利用系统函数LUT进行映射矩阵替换原图形数据
cv2.imshow("window1", desImage)
gcontrastvalue = contrastvalue


def bright(brightvalue):
desImage = srcImage.copy()
global gcontrastvalue
global gbrightvalue
print('gcontrastvalue:', gcontrastvalue)
print('gbrightvalue:', gbrightvalue)
table = []
for i in range(256):
data = int(i * gcontrastvalue * 0.01 + gbrightvalue)
if data < 0:
table.append(0)
elif data > 255:
table.append(255)
else:
table.append(data)

table = np.array(table, dtype=np.uint8) # 将数组转换为映射矩阵,数据类型与原图像数据保持一致
cv2.LUT(srcImage, table, desImage) # 利用系统函数LUT进行映射矩阵替换原图形数据
cv2.imshow("window1", desImage)
gbrightvalue = brightvalue


srcImage = cv2.imread("E:/Study/python/OpenCV_study/img/8.jpg")


# print(srcImage.shape) # 获取原图像的尺寸
# print(type(srcImage[0][0][0])) # 获取原图像的数据类型,为后面做映射矩阵服务

cv2.namedWindow("window1")
cv2.createTrackbar("对比度", "window1", 80, 300, contrast)
cv2.createTrackbar("亮 度", "window1", 80, 100, bright)
cv2.imshow("window1", srcImage)
cv2.waitKey(0)
Ruff_XY 2016-05-08
  • 打赏
  • 举报
回复
定义全局变量

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧