import cv2
import numpy as np
img = cv2.imread(‘ic_launcher.jpg’)
imgHsv= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
windowName = “Color Contest”
min_Hue = “Min Hue”
max_Hue = “Max Hue”
min_Sal = “Min Sal”
max_Sal = “Max Sal”
min_Value = “Min Val”
max_Value = “Max Val”
cv2.namedWindow(windowName)
#cv2.resizeWindow(windowName,640,480)
def nothing(*args):
pass
cv2.createTrackbar(min_Hue,windowName,0,179,nothing)
cv2.createTrackbar(max_Hue,windowName,179,179,nothing)
cv2.createTrackbar(min_Sal,windowName,47,255,nothing)
cv2.createTrackbar(max_Sal,windowName,255,255,nothing)
cv2.createTrackbar(min_Value,windowName,53,255,nothing)
cv2.createTrackbar(max_Value,windowName,255,255,nothing)
def stackImages(scale,imgArray):
rows = len(imgArray)
cols = len(imgArray[0])
# & 输出一个 rows * cols 的矩阵(imgArray)
print(rows,cols)
# & 判断imgArray[0] 是不是一个list
rowsAvailable = isinstance(imgArray[0], list)
# & imgArray[][] 是什么意思呢?
# & imgArray[0][0]就是指[0,0]的那个图片(我们把图片集分为二维矩阵,第一行、第一列的那个就是第一个图片)
# & 而shape[1]就是width,shape[0]是height,shape[2]是
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
# & 例如,我们可以展示一下是什么含义
# cv2.imshow("img", imgArray[0][1])
if rowsAvailable:
for x in range (0, rows):
for y in range(0, cols):
# & 判断图像与后面那个图像的形状是否一致,若一致则进行等比例放缩;否则,先resize为一致,后进行放缩
if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
else:
imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
# & 如果是灰度图,则变成RGB图像(为了弄成一样的图像)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
# & 设置零矩阵
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
ver = np.vstack(hor)
# & 如果不是一组照片,则仅仅进行放缩 or 灰度转化为RGB
else:
for x in range(0, rows):
if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
else:
imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
ver = hor
return ver
if name == ‘main’:
while True:
h_min = cv2.getTrackbarPos(min_Hue, windowName)
h_max = cv2.getTrackbarPos(max_Hue, windowName)
s_min = cv2.getTrackbarPos(min_Sal, windowName)
s_max = cv2.getTrackbarPos(max_Sal, windowName)
v_min = cv2.getTrackbarPos(min_Value, windowName)
v_max = cv2.getTrackbarPos(max_Value, windowName)
lower = np.array([h_min,s_min,v_min])
upper = np.array([h_max,s_max,v_max])
mask = cv2.inRange(imgHsv,lower,upper)
imageResult = cv2.bitwise_and(img,img,mask=mask)
imgstack = stackImages(0.6,[[img,imgHsv],[mask,imageResult]])
print(h_min,h_max,s_min,s_max,v_min,v_max)
cv2.imshow("Result image", imgstack)
cv2.waitKey(10)