def main(): directories.clearFolder(directories.test) print("Running GrabCut...") for img, bbox, filename in ImagesAndBBoxes: #for img, bbox, filename in [ImagesAndBBoxes[0], ImagesAndBBoxes[5]]: bbox = map(int, bbox) bbox = tuple(bbox) #mask = openCVGrabCut(img, bbox) mask = myGrabCut(img, bbox, filename) #result = cv2.Image(mask) print("Finished one image.") directories.saveArrayAsImage(directories.output + filename + ".bmp", mask) print("GrabCut is finished :D")
def main(): directories.clearFolder(directories.test) print("Running GrabCut...") for img, bbox, filename in ImagesAndBBoxes: bbox = map(int, bbox) bbox = tuple(bbox) step = 10 img = sp.misc.imresize(img, float(1)/step) bbox = [x/step for x in bbox] #mask = calcMaskUsingOpenCVGrabCut(img, bbox) mask = calcMaskUsingMyGrabCut(img, bbox, filename) #result = cv2.Image(mask) print("Finished one image.") directories.saveArrayAsImage(directories.output + filename + ".bmp", mask) print("GrabCut is finished :D")
def calcMaskUsingMyGrabCut(img, bbox, filename): ftype = ".jpg" trimap = initTrimapFromBBox(img, bbox) pmask = np.zeros(trimap.shape) mask = np.copy(trimap) mask += 1 binaryEdges, binaryCapacities, edges = initBinaryEdges(img) iteration = 0 while differenceBetweenTwoMasks(pmask, mask) > .01 or iteration < 3: print "Difference between masks", differenceBetweenTwoMasks(pmask, mask) print("Beginning " + filename + " on iteration " + str(iteration)) pmask = np.copy(mask) fgObs = img[mask != 0] bgObs = img[mask == 0] print("Making GMMs...") fgProb = np.zeros(mask.shape) if len(fgObs) >= 0: for _ in range(settings.numGMMs): fgProb += fitGMM(fgObs).score(np.asarray(img).reshape(-1, 3)).reshape(mask.shape) bgProb = np.zeros(mask.shape) if len(bgObs) > 0: for _ in range(settings.numGMMs): bgProb += fitGMM(bgObs).score(np.asarray(img).reshape(-1, 3)).reshape(mask.shape) #Visualize the image mapped to best components of one gaussian mixture model or the other fgProb = normalizeArr(fgProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "fgProb" + ftype, fgProb) bgProb = normalizeArr(bgProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "bgProb" + ftype, bgProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "edges" + ftype, edges) binaryEdgesArr = np.zeros(mask.shape) for edge, cap in zip(binaryEdges, binaryCapacities): binaryEdgesArr[edge[0]] += cap directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "y" + ftype, binaryEdgesArr) #Create edges to source and sink unaryTerm = fgProb - bgProb unaryTerm = gaussianBlur(unaryTerm, sigma=3) #Blur the unary term to remove high frequency noise unaryTerm = normalizeArr(unaryTerm, shift=True) unaryTerm += settings.bias """ print("Binary edges range from " + str(min(binaryCapacities)) + " to " + str(max(binaryCapacities)) + "\nwith median of " + str(np.median(binaryCapacities)) + " and average of " + str(np.average(binaryCapacities))) print("Unary edges range from " + str(np.min(unaryTerm)) + " to " + str(np.max(unaryTerm)) + "\nwith median of " + str(np.median(unaryTerm)) + " and average of " + str(np.average(unaryTerm))) """ directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "unaryTerm" + ftype, unaryTerm) REALLY_BIG_CAPACITY = 10000 unaryTerm[trimap == -1] = -REALLY_BIG_CAPACITY unaryTerm[trimap == 1] = REALLY_BIG_CAPACITY assert np.min(unaryTerm) < 0 assert np.max(unaryTerm) > 0 unaryEdges = [] unaryCapacities = [] for x in range(mask.shape[1]): for y in range(mask.shape[0]): uterm = unaryTerm[y, x] if uterm > 0: unaryEdges.append(("source", (y,x))) unaryCapacities.append(uterm) elif uterm < 0: unaryEdges.append(((y,x), "sink")) unaryCapacities.append(-uterm) vertexList = [(y, x) for x in range(mask.shape[1]) for y in range(mask.shape[0])] assert max(binaryCapacities + unaryCapacities) <= REALLY_BIG_CAPACITY assert min(binaryCapacities + unaryCapacities) >= 0 cuts = minCuts(vertexList, binaryEdges + unaryEdges, binaryCapacities + unaryCapacities) if len(cuts) == 0: print("No cuts found! Trying again anyway....") print("") mask[...] = 0 mask[unaryTerm > .00001] = 1 else: cut = cuts[0] print(cut) mask[...] = 0 #zero out the mask for vertexIndex in cut[0]: if vertexIndex < len(vertexList): pt = vertexList[vertexIndex] mask[pt] = 1 directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "z" + ftype, mask) print("") iteration += 1 mask = removeAllButLargestComponent(mask) return mask
def calcMaskUsingMyGrabCut(img, bbox, filename): trimap = initTrimapFromBBox(img, bbox) pmask = np.zeros(trimap.shape) mask = np.copy(trimap) mask += 1 binaryEdges, binaryCapacities = initBinaryEdges(img) iteration = 0 while differenceBetweenTwoMasks(pmask, mask) > .005 or iteration < 3: print(differenceBetweenTwoMasks(pmask, mask)) pmask = np.copy(mask) print("Beginning " + filename + " on iteration " + str(iteration)) fgObs = img[mask == 1] bgObs = img[mask == 0] allObs = np.asarray(img).reshape(-1, 3) """ #Plot the point clouds #plt.scatter(x=allObs[::100, 2], y=allObs[::100, 0]) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') step = 10000 colorList = [0] * len(fgObs) + [1] * len(bgObs) colorArr = np.zeros((len(allObs), 3)) colorArr[:, 0] = 255 * np.asarray(colorList) colorArr[:, 1] = 255 * (1 - np.asarray(colorList)) colorArr[:, 2] = 255 ax.scatter(allObs[::step, 0], allObs[::step, 1], allObs[::step, 2], c=colorArr) plt.show() exit() """ numComponents = 10 covType = 'diag' print("Making GMMs...") if len(allObs) >= numComponents: #Make BG Components allGMM = sklearn.mixture.DPGMM(n_components=numComponents, alpha=10, covariance_type=covType, random_state=None, thresh=0.001, min_covar=0.001, n_iter=10, params='wmc', init_params='wmc') allGMM.fit(allObs) allComponents = allGMM.predict(np.reshape(img, (-1, 3))) allComponents = np.reshape(allComponents, mask.shape) agProb = allGMM.score(allObs).reshape(mask.shape) agLogProb, agResponsibilities = allGMM.eval(allObs) agLogProb = agLogProb.reshape(mask.shape) agResponsibilities = agResponsibilities.reshape((mask.shape[0], mask.shape[1], -1)) if len(fgObs) >= numComponents: #Make FG Components fgGMM = sklearn.mixture.DPGMM(n_components=numComponents, alpha=.01, covariance_type=covType, random_state=None, thresh=0.001, min_covar=None, n_iter=10, params='wmc', init_params='wmc') fgGMM.fit(fgObs) fgComponents = fgGMM.predict(np.reshape(img, (-1, 3))) fgComponents = np.reshape(fgComponents, mask.shape) fgProb = fgGMM.score(np.asarray(img).reshape(-1, 3)).reshape(mask.shape) #fgProb /= allProb if len(bgObs) >= numComponents: #Make BG Components bgGMM = sklearn.mixture.DPGMM(n_components=numComponents, alpha=.01, covariance_type=covType, random_state=None, thresh=0.001, min_covar=0.001, n_iter=10, params='wmc', init_params='wmc') bgGMM.fit(bgObs) bgComponents = bgGMM.predict(np.reshape(img, (-1, 3))) bgComponents = np.reshape(bgComponents, mask.shape) bgProb = bgGMM.score(np.asarray(img).reshape(-1, 3)).reshape(mask.shape) #bgProb /= allProb print("We found " + str(np.max(fgComponents) + 1) + " foreground components, and " + str(np.max(bgComponents) + 1) + " background components") #Visualize the image mapped to best components of one gaussian mixture model or the other agProb = normalizeArr(agProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "agProb" + ".bmp", agProb) fgProb = normalizeArr(fgProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "fgProb" + ".bmp", fgProb) bgProb = normalizeArr(bgProb) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "bgProb" + ".bmp", bgProb) binaryEdgesArr = np.zeros(mask.shape) for edge, cap in zip(binaryEdges, binaryCapacities): binaryEdgesArr[edge[0]] += cap directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "y" + ".bmp", binaryEdgesArr) #Create edges to source and sink fgWeight = 1#float(len(fgObs)) / len(allObs) bgWeight = 1#float(len(bgObs)) / len(allObs) unaryTerm = fgWeight * fgProb - bgWeight * bgProb unaryTerm -= np.median(unaryTerm) * .5 #This is the strength of the normalization print("Binary edges range from " + str(min(binaryCapacities)) + " to " + str(max(binaryCapacities)) + "\nwith median of " + str(np.median(binaryCapacities)) + " and average of " + str(np.average(binaryCapacities))) print("Unary edges range from " + str(np.min(unaryTerm)) + " to " + str(np.max(unaryTerm)) + "\nwith median of " + str(np.median(unaryTerm)) + " and average of " + str(np.average(unaryTerm))) directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "unaryTerm" + ".bmp", unaryTerm) REALLY_BIG_CAPACITY = 10000 unaryTerm[trimap == -1] = -REALLY_BIG_CAPACITY unaryTerm[trimap == 1] = REALLY_BIG_CAPACITY assert np.min(unaryTerm) < 0 assert np.max(unaryTerm) > 0 unaryEdges = [] unaryCapacities = [] for x in range(mask.shape[1]): for y in range(mask.shape[0]): uterm = unaryTerm[y, x] if uterm > 0: unaryEdges.append(("source", (y,x))) unaryCapacities.append(uterm) elif uterm < 0: unaryEdges.append(((y,x), "sink")) unaryCapacities.append(-uterm) vertexList = [(y, x) for x in range(mask.shape[1]) for y in range(mask.shape[0])] assert max(binaryCapacities + unaryCapacities) <= REALLY_BIG_CAPACITY assert min(binaryCapacities + unaryCapacities) >= 0 cuts = minCuts(vertexList, binaryEdges + unaryEdges, binaryCapacities + unaryCapacities) if len(cuts) == 0: print("No cuts found! Trying again anyway....") print("") directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "z" + ".bmp", mask) continue cut = cuts[0] print(cut) mask[...] = 0 #zero out the mask for vertexIndex in cut[0]: if vertexIndex < len(vertexList): pt = vertexList[vertexIndex] mask[pt] = 1 directories.saveArrayAsImage(directories.test + filename + "-" + str(iteration) + "z" + ".bmp", mask) print("") iteration += 1 return mask
def calcMaskUsingMyGrabCut(img, bbox, filename): ftype = ".jpg" trimap = initTrimapFromBBox(img, bbox) pmask = np.zeros(trimap.shape) mask = np.copy(trimap) mask += 1 binaryEdges, binaryCapacities, edges = initBinaryEdges(img) iteration = 0 while differenceBetweenTwoMasks(pmask, mask) > .01 or iteration < 3: print "Difference between masks", differenceBetweenTwoMasks( pmask, mask) print("Beginning " + filename + " on iteration " + str(iteration)) pmask = np.copy(mask) fgObs = img[mask != 0] bgObs = img[mask == 0] print("Making GMMs...") fgProb = np.zeros(mask.shape) if len(fgObs) >= 0: for _ in range(settings.numGMMs): fgProb += fitGMM(fgObs).score(np.asarray(img).reshape( -1, 3)).reshape(mask.shape) bgProb = np.zeros(mask.shape) if len(bgObs) > 0: for _ in range(settings.numGMMs): bgProb += fitGMM(bgObs).score(np.asarray(img).reshape( -1, 3)).reshape(mask.shape) #Visualize the image mapped to best components of one gaussian mixture model or the other fgProb = normalizeArr(fgProb) directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "fgProb" + ftype, fgProb) bgProb = normalizeArr(bgProb) directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "bgProb" + ftype, bgProb) directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "edges" + ftype, edges) binaryEdgesArr = np.zeros(mask.shape) for edge, cap in zip(binaryEdges, binaryCapacities): binaryEdgesArr[edge[0]] += cap directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "y" + ftype, binaryEdgesArr) #Create edges to source and sink unaryTerm = fgProb - bgProb unaryTerm = gaussianBlur( unaryTerm, sigma=3) #Blur the unary term to remove high frequency noise unaryTerm = normalizeArr(unaryTerm, shift=True) unaryTerm += settings.bias """ print("Binary edges range from " + str(min(binaryCapacities)) + " to " + str(max(binaryCapacities)) + "\nwith median of " + str(np.median(binaryCapacities)) + " and average of " + str(np.average(binaryCapacities))) print("Unary edges range from " + str(np.min(unaryTerm)) + " to " + str(np.max(unaryTerm)) + "\nwith median of " + str(np.median(unaryTerm)) + " and average of " + str(np.average(unaryTerm))) """ directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "unaryTerm" + ftype, unaryTerm) REALLY_BIG_CAPACITY = 10000 unaryTerm[trimap == -1] = -REALLY_BIG_CAPACITY unaryTerm[trimap == 1] = REALLY_BIG_CAPACITY assert np.min(unaryTerm) < 0 assert np.max(unaryTerm) > 0 unaryEdges = [] unaryCapacities = [] for x in range(mask.shape[1]): for y in range(mask.shape[0]): uterm = unaryTerm[y, x] if uterm > 0: unaryEdges.append(("source", (y, x))) unaryCapacities.append(uterm) elif uterm < 0: unaryEdges.append(((y, x), "sink")) unaryCapacities.append(-uterm) vertexList = [(y, x) for x in range(mask.shape[1]) for y in range(mask.shape[0])] assert max(binaryCapacities + unaryCapacities) <= REALLY_BIG_CAPACITY assert min(binaryCapacities + unaryCapacities) >= 0 cuts = minCuts(vertexList, binaryEdges + unaryEdges, binaryCapacities + unaryCapacities) if len(cuts) == 0: print("No cuts found! Trying again anyway....") print("") mask[...] = 0 mask[unaryTerm > .00001] = 1 else: cut = cuts[0] print(cut) mask[...] = 0 #zero out the mask for vertexIndex in cut[0]: if vertexIndex < len(vertexList): pt = vertexList[vertexIndex] mask[pt] = 1 directories.saveArrayAsImage( directories.test + filename + "-" + str(iteration) + "z" + ftype, mask) print("") iteration += 1 mask = removeAllButLargestComponent(mask) return mask