def findCCs(image, erasecol=0, doContinue=None, doSkip=None, bRange=0, connectivity=8): """ Finds all connected components in the image. doContinue is a function applied to the color of every new pixel in the image. If it is true, this pixel is ignored. Default: <= 128 doSkip is a function applied to every new connected component found by the function. If it is true, this component will not be included in the result. Default: do not skip anything. """ if doContinue is None: doContinue = lambda col: col <= 128 if doSkip is None: doSkip = lambda comp: False mask = cv.CreateImage((image.width + 2, image.height + 2), cv.IPL_DEPTH_8U, 1) cv.Zero(mask) components = [] for x in range(image.width): for y in range(image.height): if doContinue(image[y, x]): continue comp = cv.FloodFill(image, (x, y), 0, bRange, bRange, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), mask) # here 3rd argument is ignored region = shiftRect(comp[2], 1, 1) if not doSkip(comp): seg = cvext.getSubImage(mask, region) components.append((comp[0], comp[1], comp[2], seg)) cv.SetImageROI(image, comp[2]) cv.SetImageROI(mask, region) cv.Set(image, erasecol, mask) cv.Zero(mask) cv.ResetImageROI(image) cv.ResetImageROI(mask) return components
def cutNonBlack(comp, thresh, colthresh=0): image = comp[3] rect = comp[2] newRect = findNonBlackRect(image, thresh, colthresh) if newRect[2] == 0 or newRect[3] == 0: return None region = cvext.getSubImage(image, newRect) newRect = shiftRect(newRect, rect[0], rect[1]) return (cv.CountNonZero(region), 255.0, newRect, region)
def splitAt(image, splitters): if splitters == []: return [image] segments = [] regions = [] for i in xrange(len(splitters) - 1): region = (splitters[i], 0, splitters[i+1] - splitters[i], image.height) regions.append(region) seg = cvext.getSubImage(image, region) segments.append(seg) return segments, regions
def cutNonBlackImage(image, thresh=1, colthresh=0): newRect = findNonBlackRect(image, thresh, colthresh) if newRect[2] == 0 or newRect[3] == 0: return None region = cvext.getSubImage(image, newRect) return region