def erosion(binimg, diskradius=1, pointcounts = 1): pixels = binimg.load() imgsize = binimg.size imgpix = [] for y in xrange(0, imgsize[1]): temp = [] for x in xrange(0, imgsize[0]): # 探测的像素点总数 totalpoints = 0 # 背景点数 bgpoints = 0 for m in xrange(x-diskradius, x+diskradius+1): for n in xrange(y-diskradius, y+diskradius+1): if (m >= 0 and m <= imgsize[0]-1) and (n >= 0 and n <= imgsize[1]-1) and (m != x and n != y): totalpoints = totalpoints + 1 if pixels[m, n] == 0: bgpoints = bgpoints + 1 if bgpoints >= pointcounts: temp.append(0) else: temp.append(1) imgpix.append(temp) tempimg = getGray('template.png').resize((imgsize[0], imgsize[1])) pixels = tempimg.load() for y in xrange(0, len(imgpix)): for x in xrange(0, len(imgpix[0])): if imgpix[y][x] == 0: pixels[x, y] = 0 else: pixels[x, y] = 255 return tempimg
def get_otsu_threshold(matrix, depth=8): maximum = np.max(matrix) minimum = np.min(matrix) img = matrix.copy() img = (img - minimum) / (maximum - minimum) * (pow(2, depth) - 1) gray = otsu.getGray(img) threshold = otsu.getThres(gray) threshold = float(threshold) / (pow(2, 8) - 1) * (maximum - minimum) + minimum return threshold
def dilation_wh(binimg, wh_dict): if not isinstance(wh_dict, dict): print 'wh_dict must be like {"width": x,"height": x}' exit(-1) # 结构元尺寸 width = wh_dict['width'] height = wh_dict['height'] # 图片像素 pixels = binimg.load() # 图片尺寸 size = binimg.size #存储结果 pix = [] for y in xrange(size[1]): temp = [] for x in xrange(size[0]): points = 0 for m in xrange(x-width, x+width+1): for n in xrange(y-height, y+height+1): if (m >= 0 and m <= size[0] -1) and ( n >= 0 and n <= size[1] -1) and ( m != x and n != y): if pixels[m, n] == 255: points += 1 if points == 0: temp.append(0) else: temp.append(1) pix.append(temp) tempimg = getGray('template.png').resize((size[0], size[1])) for y in xrange(0, len(pix)): for x in xrange(0, len(pix[0])): if pix[y][x] == 0: (tempimg.load())[x, y] = 0 else: (tempimg.load())[x, y] = 255 return tempimg
if (m >= 0 and m <= imgsize[0] -1) and (n >= 0 and n <= imgsize[1]-1) and (m != x and n != y): if pixels[m, n] == 255: points += 1 if points >= pointcounts: temp.append(1) else: temp.append(0) else: temp.append(0) pix.append(temp) for y in xrange(len(pix)): for x in xrange(len(pix[0])): if pix[y][x] == 0: pixels[x, y] = 0 else: pixels[x, y] = 255 return binaryimg if __name__ == "__main__": bimg = getGray("labels-ah/im0001.ah.ppm") # skeleton = SkeletonExtracting(binimg=bimg) # skeleton.skeletonext().show() img = MorphologicalOpening(binaryimg=bimg, diskradius=2, pointcounts=6) img = xor_extractcap(bimg, dilation_wh(img, {'width': 2, 'height': 2})) # ske = SkeletonExtracting(img) # img = ske.skeletonext() cr = ConnectionRegion(binaryimg=img) conregion = cr.regionext()
def otsu_test(matrix): img = otsu.quantify(matrix) gray = otsu.getGray(img) threshold = otsu.getThres(gray) otsu.binarize(img, threshold, 1)