import cv2
import numpy as np
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
def getContours(img):
contours,hierarchy= cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
x,y,w,h=0,0,0,0
for cnt in contours:
area= cv2.contourArea(cnt)
if area>50:
cv2.drawContours(img,cnt,-1,(255,255,0),3)
peri = cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,peri*0.02,True)
x,y,w,h=cv2.boundingRect(approx)
return x+w//2,y
def nothing(*args):
pass
framewidth = 640
frameheight =480
windowName=“Demo”
toolbarName = “toolbar”
cv2.namedWindow(windowName)
camera = cv2.VideoCapture(0,cv2.CAP_DSHOW)
camera.set(3,framewidth)
camera.set(4,frameheight)
#camera.set(10,130)
hmin,smin,vmin,hmax,smax,vmax=0,47,53,179,255,255
myColors = [[73,139,0,119,255,255],
[133,56,0,159,156,255],
[57,76,0,100,255,255]]
DEBUG=0
cv2.createTrackbar(“hmin”,windowName,0,129,nothing)
cv2.createTrackbar(“hmax”,windowName,129,129,nothing)
cv2.createTrackbar(“smin”,windowName,0,255,nothing)
cv2.createTrackbar(“smax”,windowName,255,255,nothing)
cv2.createTrackbar(“vmin”,windowName,0,255,nothing)
cv2.createTrackbar(“vmax”,windowName,255,255,nothing)
while True:
ret,frame= camera.read()
#######得到HSV的6个值
hmin = cv2.getTrackbarPos("hmin", windowName)
hmax = cv2.getTrackbarPos("hmax", windowName)
smin = cv2.getTrackbarPos("smin", windowName)
smax = cv2.getTrackbarPos("smax", windowName)
vmin = cv2.getTrackbarPos("vmin", windowName)
vmax = cv2.getTrackbarPos("vmax", windowName)
#cv2.imshow("camera",frame)
hsvframe = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
if DEBUG == 1:
low = np.array([hmin,smin,vmin])#(myColors[0:3])
high= np.array([hmax,smax,vmax])#(myColors[3:6])
else:
pass
#for color in myColors:
low = np.array(myColors[0][0:3])
high = np.array(myColors[0][3:6])
mask = cv2.inRange(hsvframe,low,high)
x,y=getContours(mask)
finalImg = cv2.bitwise_and(frame,frame,mask=mask)
stackImage=stackImages(0.6,[[frame,hsvframe],[mask,finalImg]])
cv2.imshow(windowName,stackImage)
if cv2.waitKey(10) & 0xff == 27:
break
cv2.destroyAllWindows()