In [51]:
""" TITLE #######################################################################
# Counting cell in mouse brain - adaptation ofr human cortex
# VVI, New York, 2016-2019 All rights reserved
## DESCRIPTION ##################################################################
## This is a modification of the demo example from MAMBA-Image documentation
## See mamba-image.org for installation and other instructions
#################################################################################
"""
# Importing mamba
from mamba import *
from mambaDisplay.extra import *
import mambaDisplay
import os
import numpy as np

from scipy.optimize import curve_fit

import matplotlib.pyplot as plt
from PIL import Image, ImageMath
Image.MAX_IMAGE_PIXELS = 100000*100000
#
# This function cab be used to understand how watersheds algorithm works
#
def watershedByGradMinima(imIn, imOut, grid=DEFAULT_GRID):
    """
    Computes the watershed line of imIn using its gradient minima as a marker image
    """
    grad = imageMb(imIn, 8)
    marker = imageMb(imIn, 32)
    minbin = imageMb(imIn, 1)
    gradient(imIn, grad)
    minima(grad, minbin, grid=grid)
    label(minbin, marker, grid=grid)
    watershedSegment(grad, marker, grid=grid)
    copyBytePlane(marker, 3, imOut)
    
def ShowImage(mbImage, Title="Set Title", figsize=(10, 12)):
    pilImage = Mamba2PIL(mbImage)
    fig, ax = plt.subplots(figsize=figsize)
    plt.title(Title)
    ax.imshow(pilImage, cmap=plt.cm.gray)

def ShowPilImage(PilImage, Title="Set Title", figsize=(10, 12)):
    fig, ax = plt.subplots(figsize=figsize)
    plt.title(Title)
    ax.imshow(PilImage, cmap=plt.cm.gray)
    
def ShowImageColor(mbImage, Title="Set Title", figsize=(10, 12), palette = 0):
    pilImage = Mamba2PIL(mbImage)
    pilImage.putpalette(palette)
    fig, ax = plt.subplots(figsize=figsize)
    plt.title(Title)
    ax.imshow(pilImage, cmap=plt.cm.prism)
In [52]:
# Open files and create images
sourceFolder1 = "C:\\Image_Processing_Mamba\\A19_Reg_No_Repeats\\Croped_1024_1024\\"
#sourceFolder1 = "C:\\Image_Processing_Mamba\\A19_Reg_No_Repeats\\original_frag\images_1\\"
#sourceFolder1 = "N:\\Mouse_cshl\\170613_RMC_MsNT_NoMeOH\\Ch1_512x512\\"
# No background image for human samples
#sourceFolder2 = "N:\\Mouse_cshl\\170613_RMC_MsNT_NoMeOH\\Ch2_512x512\\"
destFolder = "C:\\Image_Processing_Mamba\\A19_Reg_No_Repeats\\Results_mouse_version\\"
#destFolder = "N:\\Mouse_cshl\\170613_RMC_MsNT_NoMeOH\\Segmented\\"

palette=mambaDisplay.getPalette("rainbow")
redPalette = mambaDisplay.tagOneColorPalette(255, (255,0,0))

allFiles1 = os.listdir(sourceFolder1)
#allFiles2 = os.listdir(sourceFolder2)

im0 = imageMb(sourceFolder1+allFiles1[0])
#im1 = imageMb(sourceFolder2+allFiles2[0])
imDepth = im0.getDepth()
imSize = im0.getSize()
print(imDepth, imSize)
(8, (1024, 1024))
In [53]:
# Defining working images.
#imDepth = 8

imWrk1 = imageMb(imSize[0], imSize[1], imDepth)
imWrk2 = imageMb(imSize[0], imSize[1], imDepth)
imWrk32 = imageMb(imSize[0], imSize[1], 32)
imIn = imageMb(imSize[0], imSize[1], 8)
im1 = imageMb(imSize[0], imSize[1], 8)
imWrk1_8 = imageMb(imSize[0], imSize[1], 8)
imWrk2_8 = imageMb(imSize[0], imSize[1], 8)
imWrk1_1 = imageMb(imSize[0], imSize[1], 1)
imWrk2_1 = imageMb(imSize[0], imSize[1], 1)
blobsMarkers = imageMb(imSize[0], imSize[1], 1)
backgroundMarker= imageMb(imSize[0], imSize[1], 1)
blobsContours = imageMb(imSize[0], imSize[1], 1)
finalMarkers = imageMb(imSize[0], imSize[1], 1)
In [54]:
# Set to True to save intermediate steps images
Save_Demo = True

totalNumber = 0
#for i in range(0, len(allFiles2)):
for i in range(0, 1):
    if (i > 0):
        im0 = imageMb(sourceFolder1+allFiles1[i])
        #im1 = imageMb(sourceFolder2+allFiles2[i])

    fileName, ext = os.path.splitext(destFolder+allFiles1[i])
    # Some simple threshold to avoid processing empty (blank) image
    #imVolume = computeVolume(im0)
    imVolume = 3500
    if imVolume > 3000:
        '''
        # Another way to eliminate the backgtound
        if Demo: im0.save(fileName+"_initial.png", palette=mambaDisplay.getPalette(name))
        strongLevelling(im0, imWrk1, 10, False)
        if Demo: imWrk1.save(fileName+"_levelling.png", palette=mambaDisplay.getPalette(name))
        floorSub(im0, imWrk1, imWrk2)
        if Demo: imWrk2.save(fileName+"_backgr_substr.png", palette=mambaDisplay.getPalette(name))
        '''
        # Substract background from signal channel (im0, im1)
        #sub(im0, im1, im0)
        if Save_Demo: im0.save(fileName+"_initial.png", palette=redPalette)
        convert(im0, imWrk1_8)
        ShowImage(imWrk1_8, "Initial Image", (15,15))