37,720
社区成员
发帖
与我相关
我的任务
分享
# -*- 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()
# -*- 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)