pysilcam/postprocess.py
def montage_maker(roifiles, roidir, pixel_size, msize=2048, brightness=255,
        tightpack=False, eyecandy=True):
    '''
    makes nice looking matages from a directory of extracted particle images

    use make_montage to call this function

    Args:
        roifiles                : list of roi files obtained from gen_roifiles(stats, auto_scaler=auto_scaler)
        roidir                  : location of roifiles usually defined by settings.ExportParticles.outputpath
        pixel_size              : pixel size of system defined by settings.PostProcess.pix_size
        msize=2048              : size of canvas in pixels
        brightness=255          : brighness of packaged particles
        tightpack=False         : boolean which if True packs particles using segmented alpha-shapes instead of bounding boxes. This is slow, but packs particles more tightly.
        eyecandy=True           : boolean which if True will explode the contrast of packed particles (nice for natural particles, but not so good for oil and gas).

    Returns:
        montageplot             : a nicely-made montage in the form of an image, which can be plotted using plotting.montage_plot(montage, settings.PostProcess.pix_size)
    '''

    if tightpack:
        import pysilcam.process as scpr
    # pre-allocate an empty canvas
    montage = np.zeros((msize,msize,3),dtype=np.uint8())
    # pre-allocate an empty test canvas
    immap_test = np.zeros_like(montage[:,:,0])
    logger.info('making a montage - this might take some time....')

    # loop through each extracted particle and attempt to add it to the canvas
    for files in tqdm(roifiles):
        # get the particle image from the HDF5 file
        particle_image = export_name2im(files, roidir)

        # measure the size of this image
        [height, width] = np.shape(particle_image[:,:,0])

        # sanity-check on the particle image size
        if height >= msize:
            continue
        if width >= msize:
            continue

        if eyecandy:
            # contrast exploding:
            particle_image = explode_contrast(particle_image)

            # eye-candy normalization:
            peak = np.median(particle_image.flatten())
            bm = brightness - peak
            particle_image = np.float64(particle_image) + bm
        else:
            particle_image = np.float64(particle_image)
        particle_image[particle_image>255] = 255

        # tighpack checks fitting within the canvas based on an approximation
        # of the particle area. If not tightpack, then the fitting will be done
        # based on bounding boxes instead
        if tightpack:
            imbw = scpr.image2blackwhite_accurate(np.uint8(particle_image[:,:,0]), 0.95)
            imbw = ndi.binary_fill_holes(imbw)

            for J in range(5):
                imbw = skimage.morphology.binary_dilation(imbw)

        # initialise a counter
        counter = 0

        # try five times to fit the particle to the canvas by randomly moving
        # it around
        while (counter < 5):
            r = np.random.randint(1,msize-height)
            c = np.random.randint(1,msize-width)

            # tighpack checks fitting within the canvas based on an approximation
            # of the particle area. If not tightpack, then the fitting will be done
            # based on bounding boxes instead
            if tightpack:
                test = np.max(immap_test[r:r+height,c:c+width]+imbw)
            else:
                test = np.max(immap_test[r:r+height,c:c+width,None]+1)


            # if the new particle is overlapping an existing object in the
            # canvas, then try again and increment the counter
            if (test>1):
                counter += 1
            else:
                break

        # if we reach this point and there is still an overlap, then forget
        # this particle, and move on
        if (test>1):
            continue

        # if we reach here, then the particle has found a position in the
        # canvas with no overlap, and can then be inserted into the canvas
        montage[r:r+height,c:c+width,:] = np.uint8(particle_image)

        # update the testing canvas so it is ready for the next particle
        if tightpack:
            immap_test[r:r+height,c:c+width] = imbw
        else:
            immap_test[r:r+height,c:c+width,None] = immap_test[r:r+height,c:c+width,None]+1

    # now the montage is finished
    # here are some small eye-candy scaling things to tidy up
    montageplot = np.copy(montage)
    montageplot[montage>255] = 255
    montageplot[montage==0] = 255
    logger.info('montage complete')

    return montageplot

    makes nice looking matages from a directory of extracted particle images

    use make_montage to call this function

    Args:
        roifiles                : list of roi files obtained from gen_roifiles(stats, auto_scaler=auto_scaler)
        roidir                  : location of roifiles usually defined by settings.ExportParticles.outputpath
        pixel_size              : pixel size of system defined by settings.PostProcess.pix_size
        msize=2048              : size of canvas in pixels
        brightness=255          : brighness of packaged particles
        tightpack=False         : boolean which if True packs particles using segmented alpha-shapes instead of bounding boxes. This is slow, but packs particles more tightly.
        eyecandy=True           : boolean which if True will explode the contrast of packed particles (nice for natural particles, but not so good for oil and gas).

    Returns:
        montageplot             : a nicely-made montage in the form of an image, which can be plotted using plotting.montage_plot(montage, settings.PostProcess.pix_size)
    
makes nice looking matages from a directory of extracted particle images
use make_montage to call this function
makes nice looking matages from a directory of extracted particle images use make_montage to call this function
def montage_maker(roifiles, roidir, pixel_size, msize=2048, brightness=255,
        tightpack=False, eyecandy=True):
    if tightpack:
        import pysilcam.process as scpr
    montage = np.zeros((msize,msize,3),dtype=np.uint8())
    immap_test = np.zeros_like(montage[:,:,0])
    logger.info('making a montage - this might take some time....')
    for files in tqdm(roifiles):
        particle_image = export_name2im(files, roidir)
        [height, width] = np.shape(particle_image[:,:,0])
        if height >= msize:
            continue
        if width >= msize:
            continue
        if eyecandy:
            particle_image = explode_contrast(particle_image)
            peak = np.median(particle_image.flatten())
            bm = brightness - peak
            particle_image = np.float64(particle_image) + bm
        else:
            particle_image = np.float64(particle_image)
        particle_image[particle_image>255] = 255
        if tightpack:
            imbw = scpr.image2blackwhite_accurate(np.uint8(particle_image[:,:,0]), 0.95)
            imbw = ndi.binary_fill_holes(imbw)
            for J in range(5):
                imbw = skimage.morphology.binary_dilation(imbw)
        counter = 0
        while (counter < 5):
            r = np.random.randint(1,msize-height)
            c = np.random.randint(1,msize-width)
            if tightpack:
                test = np.max(immap_test[r:r+height,c:c+width]+imbw)
            else:
                test = np.max(immap_test[r:r+height,c:c+width,None]+1)
            if (test>1):
                counter += 1
            else:
                break
        if (test>1):
            continue
        montage[r:r+height,c:c+width,:] = np.uint8(particle_image)
        if tightpack:
            immap_test[r:r+height,c:c+width] = imbw
        else:
            immap_test[r:r+height,c:c+width,None] = immap_test[r:r+height,c:c+width,None]+1
    montageplot = np.copy(montage)
    montageplot[montage>255] = 255
    montageplot[montage==0] = 255
    logger.info('montage complete')
    return montageplot
def montage_maker ( roifiles , roidir , pixel_size , msize = 2048 , brightness = 255 , tightpack = False , eyecandy = True ) : if tightpack : import pysilcam . process as scpr montage = np . zeros ( ( msize , msize , 3 ) , dtype = np . uint8 ( ) ) immap_test = np . zeros_like ( montage [ : , : , 0 ] ) logger . info ( 'making a montage - this might take some time....' ) for files in tqdm ( roifiles ) : particle_image = export_name2im ( files , roidir ) [ height , width ] = np . shape ( particle_image [ : , : , 0 ] ) if height >= msize : continue if width >= msize : continue if eyecandy : particle_image = explode_contrast ( particle_image ) peak = np . median ( particle_image . flatten ( ) ) bm = brightness - peak particle_image = np . float64 ( particle_image ) + bm else : particle_image = np . float64 ( particle_image ) particle_image [ particle_image > 255 ] = 255 if tightpack : imbw = scpr . image2blackwhite_accurate ( np . uint8 ( particle_image [ : , : , 0 ] ) , 0.95 ) imbw = ndi . binary_fill_holes ( imbw ) for J in range ( 5 ) : imbw = skimage . morphology . binary_dilation ( imbw ) counter = 0 while ( counter < 5 ) : r = np . random . randint ( 1 , msize - height ) c = np . random . randint ( 1 , msize - width ) if tightpack : test = np . max ( immap_test [ r : r + height , c : c + width ] + imbw ) else : test = np . max ( immap_test [ r : r + height , c : c + width , None ] + 1 ) if ( test > 1 ) : counter += 1 else : break if ( test > 1 ) : continue montage [ r : r + height , c : c + width , : ] = np . uint8 ( particle_image ) if tightpack : immap_test [ r : r + height , c : c + width ] = imbw else : immap_test [ r : r + height , c : c + width , None ] = immap_test [ r : r + height , c : c + width , None ] + 1 montageplot = np . copy ( montage ) montageplot [ montage > 255 ] = 255 montageplot [ montage == 0 ] = 255 logger . info ( 'montage complete' ) return montageplot
makes nice looking matages from a directory of extracted particle images
use make_montage to call this function
makes nice looking matages from a directory of extracted particle images use make_montage to call this function
'''
    makes nice looking matages from a directory of extracted particle images

    use make_montage to call this function

    Args:
        roifiles                : list of roi files obtained from gen_roifiles(stats, auto_scaler=auto_scaler)
        roidir                  : location of roifiles usually defined by settings.ExportParticles.outputpath
        pixel_size              : pixel size of system defined by settings.PostProcess.pix_size
        msize=2048              : size of canvas in pixels
        brightness=255          : brighness of packaged particles
        tightpack=False         : boolean which if True packs particles using segmented alpha-shapes instead of bounding boxes. This is slow, but packs particles more tightly.
        eyecandy=True           : boolean which if True will explode the contrast of packed particles (nice for natural particles, but not so good for oil and gas).

    Returns:
        montageplot             : a nicely-made montage in the form of an image, which can be plotted using plotting.montage_plot(montage, settings.PostProcess.pix_size)
    ''' # pre-allocate an empty canvas # pre-allocate an empty test canvas # loop through each extracted particle and attempt to add it to the canvas # get the particle image from the HDF5 file # measure the size of this image # sanity-check on the particle image size # contrast exploding: # eye-candy normalization: # tighpack checks fitting within the canvas based on an approximation # of the particle area. If not tightpack, then the fitting will be done # based on bounding boxes instead # initialise a counter # try five times to fit the particle to the canvas by randomly moving # it around # tighpack checks fitting within the canvas based on an approximation # of the particle area. If not tightpack, then the fitting will be done # based on bounding boxes instead # if the new particle is overlapping an existing object in the # canvas, then try again and increment the counter # if we reach this point and there is still an overlap, then forget # this particle, and move on # if we reach here, then the particle has found a position in the # canvas with no overlap, and can then be inserted into the canvas # update the testing canvas so it is ready for the next particle # now the montage is finished # here are some small eye-candy scaling things to tidy up
montageplot             : a nicely-made montage in the form of an image, which can be plotted using plotting.montage_plot(montage, settings.PostProcess.pix_size)
montageplot : a nicely - made montage in the form of an image which can be plotted using plotting . montage_plot ( montage settings . PostProcess . pix_size )
roifiles
roidir
pixel_size
msize
brightness
tightpack
eyecandy
roifiles                
list of roi files obtained from gen_roifiles(stats, auto_scaler=auto_scaler)
list of roi files obtained from gen_roifiles ( stats auto_scaler = auto_scaler )
roidir                  
location of roifiles usually defined by settings.ExportParticles.outputpath
location of roifiles usually defined by settings . ExportParticles . outputpath
pixel_size              
pixel size of system defined by settings.PostProcess.pix_size
pixel size of system defined by settings . PostProcess . pix_size
msize=2048              
size of canvas in pixels
size of canvas in pixels
brightness=255          
brighness of packaged particles
brighness of packaged particles
tightpack=False         
boolean which if True packs particles using segmented alpha-shapes instead of bounding boxes. This is slow, but packs particles more tightly.
boolean which if True packs particles using segmented alpha - shapes instead of bounding boxes . This is slow but packs particles more tightly .
eyecandy=True           
boolean which if True will explode the contrast of packed particles (nice for natural particles, but not so good for oil and gas).
boolean which if True will explode the contrast of packed particles ( nice for natural particles but not so good for oil and gas ) .