def transform(images, masks, path, out_dir, norm = False):
    try:
        os.mkdir(out_dir)
    except FileExistsError:
        pass

    fold_path = out_dir+path
    try:
        os.mkdir(fold_path)
    except FileExistsError:
        pass

    for i in range(images.shape[0]):
        stop = False
        np_file = np.zeros((256,256,5), dtype='int16')

        # add rgb channels to array
        img_int = np.array(images[i],np.int16)
        for j in range(3):
            np_file[:,:,j] = img_int[:,:,j]
        
        if norm:
            
            #Normailze stain
            try:
                Inorm, H, E = NS.normalizeStaining(np.array(np_file[:,:,:3],np.int16))
                for rgb in range(3):
                    np_file[:,:,rgb] = Inorm[:,:,rgb]

            except:
                print("file %d not normalized" % (i+1))
                stop = True
        

        # convert inst and type format for mask
        msk = masks[i]

        inst = np.zeros((256,256))
        for j in range(5):
            #copy value from new array if value is not equal 0
            inst = np.where(msk[:,:,j] != 0, msk[:,:,j], inst)
        map_inst(inst)

        types = np.zeros((256,256))
        for j in range(5):
            # write type index if mask is not equal 0 and value is still 0
            types = np.where((msk[:,:,j] != 0) & (types == 0), j+1, types)

        # add padded inst and types to array
        np_file[:,:,3] = inst
        np_file[:,:,4] = types
        if stop == False:
            np.save(fold_path +'/'+path+'_%d.npy' % (i+1), np_file)
            print('file %d saved' % (i+1))
示例#2
0
def ImageWatershed(srcImageName, dstImageName, dstMarkName):
    img = cv2.imread(srcImageName)
    '''Color Normalization'''
    img = img.astype('float32')
    img = normalizeStaining.normalizeStaining(img)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    ddepth = cv2.CV_32F
    dx = cv2.Sobel(gray, ddepth, 1, 0)
    dy = cv2.Sobel(gray, ddepth, 0, 1)
    dxabs = cv2.convertScaleAbs(dx)
    dyabs = cv2.convertScaleAbs(dy)
    thresh1 = cv2.addWeighted(dxabs, 0.5, dyabs, 0.5, 0)
    #ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                   cv2.THRESH_BINARY_INV, 501, 31)

    # thresh = cv2.addWeighted(thresh1, 0.25, thresh2, 0.75, 0)

    # cv2.imshow('gray', gray)
    cv2.imshow('binary', thresh)

    #    cv2.imwrite('binary.png', thresh);

    # noise removal
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

    cv2.imshow('opening', opening)
    #cv2.imwrite('opening.png', opening);

    # sure background area
    sure_bg = cv2.dilate(opening, kernel, iterations=3)

    cv2.imshow("background", sure_bg)
    #cv2.imwrite('bg.png', sure_bg);

    # Finding sure foreground area
    dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 3)
    cv2.imshow('dist_transform', dist_transform)

    ret, sure_fg = cv2.threshold(dist_transform, 0.25 * dist_transform.max(),
                                 255, 0)

    cv2.imshow('foreground', sure_fg)
    #cv2.imwrite('fg.png', sure_fg);

    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)

    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)

    # Add one to all labels so that sure background is not 0, but 1
    markers = markers + 1

    # Now, mark the region of unknown with zero
    markers[unknown == 255] = 0

    markers = cv2.watershed(img, markers)
    img[markers == -1] = [255, 0, 0]

    bin_mask = np.ones(markers.shape) * 255
    # bin_mask[markers == -1] = 255
    bin_mask[markers == 1] = 0
    #    bin_mask[bin_mask == -1] = 0;
    bin_mask = bin_mask.astype(np.uint8)
    #print (bin_mask.shape )

    cv2.imshow('test', img)
    cv2.imshow('bin_mask', bin_mask)
    cv2.imwrite(dstMarkName, bin_mask)

    count_holes, std_holes = CountBlackHoles(bin_mask)

    #  cv2.waitKey(0);
    #   cv2.imwrite(dstImageName, img);
    cv2.destroyAllWindows()

    return count_holes, std_holes
示例#3
0
        ic = args.tileSize * c * int(slide.level_downsamples[0])
        tile = np.array(
            slide.read_region((ic, ir), 0,
                              (args.tileSize, args.tileSize)))[..., 0:3]
        tileMask = fullMask[ic:(ic + args.tileSize), ir:(ir + args.tileSize)]
        if ((tile.mean() < args.cutoff) &
            (tile.mean() > 10)):  # remove black tiles
            tileStack.append(tile)
            maskStack.append(tileMask)
            tileCoords.append((ir, ic))

nTiles = len(tileStack)

if args.doNormalize:
    print 'normalizing tile stack'
    normTiles, H, E = normalizeStaining(np.vstack(tileStack))
    #normTiles = normTiles.reshape(len(tileStack), args.tileSize, args.tileSize, 3)
    normTiles.tolist()
    #Image.fromarray(normTiles)
    tileStack = normTiles

os.makedirs(os.path.join(args.tmpDir, slideName, 'tiles'))
os.makedirs(os.path.join(args.tmpDir, slideName, 'masks'))

for i in tqdm(range(len(tileStack)), desc='saving to tmpDir'):

    #tile = normTiles[i*args.tileSize:i*args.tileSize+args.tileSize,...]
    tile = tileStack[i]

    tileMask = maskStack[i]
    ir, ic = tileCoords[i]
示例#4
0
# - Load models
f_model = open(modelCompiled_name, "rb")
model2 = cPickle.load(f_model)
#CNN
sec_classif = cPickle.load(open(sec_classif_name, "rb"))  #SVM

image_name = sys.argv.pop(0)
print(image_name)
# - Load image
a = np.zeros((num_images, num_channels, shapey, shapex), dtype=np.uint8)
img = tiff.imread(image_name)
img_temp = np.array(img, dtype=np.uint8)
img_temp = np.array(img_temp, dtype=np.float64)

# - Pre-process image (HE normalization)
img_preproc = norm.normalizeStaining(img_temp)

a[0, 0, :, :] = img_preproc[:, :, 0]
a[0, 1, :, :] = img_preproc[:, :, 1]
a[0, 2, :, :] = img_preproc[:, :, 2]
test_set_x = a

# - Split test image in patches
patches_all = np.zeros(shape=(nb_patch, num_channels, patchSize, patchSize),
                       dtype='uint8')
#    labels_all=np.zeros(shape=((nb_patch,1)));
first_lin_patch = 0
first_col_patch = 0
for p in range(0, nb_patch):  #for each patch
    patch_prov = test_set_x[0, :, first_lin_patch:first_lin_patch + patchSize,
                            first_col_patch:first_col_patch + patchSize]
示例#5
0
def CropSlide(args):
    ''' preprocess slide into cropped images
    '''
    # load slide
    slide = openslide.OpenSlide(args.slideFile)
    slideName = args.slideFile.split('/')[-1].split('.')[0]

    # create save directories
    os.makedirs(args.tmpDir, exist_ok=True)
    os.makedirs(os.path.join(args.tmpDir, slideName), exist_ok=True)
    os.makedirs(args.tileDir, exist_ok=True)
    os.makedirs(args.tumorDir, exist_ok=True)
    os.makedirs(args.hetDir, exist_ok=True)
    os.makedirs('data/assemblies', exist_ok=True)

    # crop tile, save, and symlink
    nc = (slide.level_dimensions[args.level][0] // args.tileSize) - 1
    nr = (slide.level_dimensions[args.level][1] // args.tileSize) - 1

    tileStack = []
    tileCoords = []
    for c in range(nc):
        for r in range(nr):
            ir = args.tileSize * r * int(slide.level_downsamples[args.level])
            ic = args.tileSize * c * int(slide.level_downsamples[args.level])
            tile = np.array(
                slide.read_region((ic, ir), args.level,
                                  (args.tileSize, args.tileSize)))[..., 0:3]
            if tile.mean() < args.cutoff:
                tileStack.append(tile)
                tileCoords.append((ir, ic))

    if args.doNormalize:
        print('normalizing tile stack')
        normTiles, H, E = normalizeStaining(np.vstack(tileStack[1:4]))
        normTiles.tolist()
        tileStack = normTiles

    for i in range(len(tileStack)):
        tile = tileStack[i]
        ir, ic = tileCoords[i]
        saveFile = slideName + '-' + str(ir) + '_' + str(ic) + '.png'
        fullSaveFile = os.path.join(args.tmpDir, slideName, saveFile)
        Image.fromarray(tile).save(fullSaveFile)

    print('compressing to tileDir')
    with tarfile.open(os.path.join(args.tileDir, slideName + '.tar.gz'),
                      'w:gz') as tar:
        tar.add(os.path.join(args.tmpDir, slideName), arcname=slideName)

    # identify only the tumor tiles
    model = load_model(args.modelFile)

    # load tiles
    tileDir = os.path.join(args.tmpDir, slideName)
    print('loading images from', tileDir)
    x_test = []
    files = os.listdir(tileDir)
    files.sort()
    for i in files:
        x_test.append(np.array(image.load_img(os.path.join(tileDir, i))))
    x_test = np.array(x_test)
    x_test = x_test / 255
    print('x_test found with shape', x_test.shape)

    print('generating predictions')
    predictions = model.predict(x_test)

    # save predictions
    filenames = os.listdir(tileDir)
    filenames.sort()

    modelPredictions = pd.DataFrame()
    modelPredictions['filename'] = filenames
    modelPredictions['tumor'] = predictions
    modelPredictions.to_csv(os.path.join(args.hetDir,
                                         slideName + '_modelPredictions.csv'),
                            index=False)

    modelPredictions['SlideID'] = [
        int(i.split('_')[0].lstrip('0')) for i in modelPredictions.filename
    ]
    modelPredictions['RowIdx'] = [
        int(i.split('-')[1].split('_')[0]) for i in modelPredictions.filename
    ]
    modelPredictions['ColIdx'] = [
        int(i.split('-')[1].split('_')[1].split('.')[0])
        for i in modelPredictions.filename
    ]

    # make composite assembly
    rowMax = modelPredictions['RowIdx'].max()
    rowMin = modelPredictions['RowIdx'].min()
    colMax = modelPredictions['ColIdx'].max()
    colMin = modelPredictions['ColIdx'].min()

    modelPredictions['RowIdx'] = modelPredictions['RowIdx'] - rowMin
    modelPredictions['ColIdx'] = modelPredictions['ColIdx'] - colMin

    # make assembly
    assembly = np.zeros(
        (rowMax - rowMin + args.tileSize, colMax - colMin + args.tileSize, 3),
        dtype=np.uint8)
    probOver = np.zeros(
        (rowMax - rowMin + args.tileSize, colMax - colMin + args.tileSize, 3),
        dtype=np.uint8)

    tileFiles = os.listdir(os.path.join(args.tmpDir, slideName))

    # paste each tile onto image
    for t in tileFiles:
        tile = Image.open(os.path.join(args.tmpDir, slideName, t))
        npc = np.array(tile)
        try:
            rowIdx = int(
                modelPredictions['RowIdx'][modelPredictions['filename'] == t])
            colIdx = int(
                modelPredictions['ColIdx'][modelPredictions['filename'] == t])
            assembly[rowIdx:rowIdx + args.tileSize,
                     colIdx:colIdx + args.tileSize, ...] = npc[..., 0:3]

            prob = float(
                modelPredictions['tumor'][modelPredictions['filename'] == t])
            probOver[rowIdx:rowIdx + args.tileSize,
                     colIdx:colIdx + args.tileSize, :] = Thermal(prob)
        except:
            print('passing', t)

    probOver = probOver[::10, ::10, :]
    assembly = assembly[::10, ::10, :]

    blendedImg = Image.blend(Image.fromarray(probOver),
                             Image.fromarray(assembly),
                             alpha=0.5)

    blendedImg.save(
        os.path.join(args.assemblyDir, slideName + '_tumor_assembly.jpg'))

    # select tiles to save
    toDrop = predictions < args.tumorCutoff
    dropFiles = [files[i] for i in range(len(toDrop)) if toDrop[i]]

    for f in dropFiles:
        os.remove(os.path.join(args.tmpDir, slideName, f))

    with tarfile.open(os.path.join(args.tumorDir, slideName + '.tar.gz'),
                      'w:gz') as tar:
        tar.add(os.path.join(args.tmpDir, slideName), arcname=slideName)
示例#6
0
    return


block_size = range(11, 801, 10)
constants = range(0, 5, 5)

img_dir = '../Tissue images/'
result_dir = '../thres_results/'

tif_files = os.listdir(img_dir)
for file in tif_files:
    if not os.path.isdir(file):
        tiffilename = img_dir + file
        sub_result_dir = result_dir + file[0:file.find('.tif')] + '/'
        if not os.path.exists(sub_result_dir):
            os.mkdir(sub_result_dir)

        # rgb --> gray
        img = cv2.imread(tiffilename)
        '''Color Normalization'''
        img = img.astype('float32')
        img = normalizeStaining.normalizeStaining(img)

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        for blksize in block_size:
            for const in constants:
                binaryimagepath = sub_result_dir + str(blksize) + '_'
                doBinaryThreshold(blksize, const, gray, binaryimagepath)

        print(tiffilename)
        print(sub_result_dir)