def add_point(event,x,y,flags,param): global pVCA, pMap # Left double-click to add a point to list if event == cv2.EVENT_LBUTTONDBLCLK: pVCA.append((x,y)) # Map coordinate from the vca image to the map xm, ym = np.int32(mod.vca2map(x,y)) print x,y,xm,ym pMap.append((xm,ym)) # Draw lines or not if len(pVCA) > 1: cv2.line(imVCA,pVCA[-2],pVCA[-1],(0,255,0),1) cv2.line(imMap,pMap[-2],pMap[-1],(0,255,0),1) # Draw circles cv2.circle(imVCA,(x,y),1,(0,0,255),-1) # Calculate real area if len(pVCA)>3: areapx2 = cv2.contourArea(np.asarray(pMap)) aream2 = areapx2 / 0.8**2 print aream2
pVCA21 = cv2.findContours(m[6].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA22 = cv2.findContours(m[7].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA23 = cv2.findContours(m[8].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA24 = cv2.findContours(m[9].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA25 = cv2.findContours(m[10].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA31 = cv2.findContours(m[11].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA32 = cv2.findContours(m[12].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA33 = cv2.findContours(m[13].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA34 = cv2.findContours(m[14].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] pVCA35 = cv2.findContours(m[15].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0] # Warp masks into MAP image pMAP11 = [] for k in range(len(pVCA11)): xm, ym = np.int32(mod.vca2map(pVCA11[k,0,0],pVCA11[k,0,1],1920)) pMAP11.append((xm,ym)) pMAP12 = [] for k in range(len(pVCA12)): xm, ym = np.int32(mod.vca2map(pVCA12[k,0,0],pVCA12[k,0,1],1920)) pMAP12.append((xm,ym)) pMAP13 = [] for k in range(len(pVCA13)): xm, ym = np.int32(mod.vca2map(pVCA13[k,0,0],pVCA13[k,0,1],1920)) pMAP13.append((xm,ym)) pMAP14 = [] for k in range(len(pVCA14)): xm, ym = np.int32(mod.vca2map(pVCA14[k,0,0],pVCA14[k,0,1],1920)) pMAP14.append((xm,ym)) pMAP15 = [] for k in range(len(pVCA15)):
lanes,parts = 3,5 # Main mask m00 = cv2.imread('./resources/%d/m00.png'%(size),1)[u0:u1,v0:v1] # For each lane and part for i in range(lanes): for j in range(parts): # Load masks m.append(cv2.imread('./resources/%d/m%d%d.png'%(size,i+1,j+1),0)[u0:u1,v0:v1]) # Area of masks in VCA image aVCA.append(np.sum(np.sum(m[-1]))/255.) # Find contours in masks pVCA.append(cv2.findContours(m[-1].copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1][0]) # Warp masks into MAP image pMAPij = [] for k in range(len(pVCA[-1])): xm, ym = np.int32(mod.vca2map(pVCA[-1][k,0,0],pVCA[-1][k,0,1],size)) pMAPij.append((xm,ym)) # Convert list into an array and append pMAP.append(np.asarray(pMAPij)) # Calculate area in MAP image aMAP.append(cv2.contourArea(pMAP[-1])/px_m**2) # Center of mass of masks im MAP image M = cv2.moments(pMAP[-1]) cMAP.append(np.array([int(M['m10']/M['m00']),int(M['m01']/M['m00'])])) # Calculate distances between centers of mass of masks dMAP = [] # Distances in m # For each lane and part but the first for i in range(lanes): for j in range(1,parts): dMAP.append(np.sqrt((cMAP[i*parts][0]-cMAP[i*parts+j][0])^2+(cMAP[i*parts][1]-cMAP[i*parts+j][1])^2)/px_m)
cMAP = np.zeros((3,5,2)) for i in range(3): for j in range(5): # Load mask m.append(cv2.imread('./resources/1920/m'+str(i+1)+str(j+1)+'.png')) mij = m[-1][:,:,1].copy() # Calculate area in VCA image aVCA[i][j] = np.sum(np.sum(mij))/255. # Find Contours in Masks pVCA = cv2.findContours(mij,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[1] # Warp masks into MAP image pMAP = [] for k in range(len(pVCA)): x = pVCA[k][0] y = pVCA[k][1] xm, ym = np.int32(mod.vca2map(x,y)) pMAP.append((xm,ym)) # Calculate area in MAP image aMAP[i,j] = cv2.contourArea(np.asarray(pMAP)) / 0.8**2 # Center of mass of masks im MAP image M = cv2.moments(cnt) cMAP[i,j,0] = int(M['m10']/M['m00']) cMAP[i,j,1] = int(M['m01']/M['m00']) # Create the BKG substractor bs = cv2.createBackgroundSubtractorMOG2() bs.setDetectShadows(True) bs.setShadowValue(0) bs.setHistory(2000) bs.setVarThreshold(36)