예제 #1
0
def inference(d, n_iter, H, W, labels):
    """
   This function carries out the CRF inference, generating a MAP
   substrate matrix, a conservative probabilistic map based on
   frequentist principles, and a probability per substrate based on
   the CRF inference
   """
    R = []
    Q, tmp1, tmp2 = d.startInference()
    for k in range(n_iter):
        print('Iteration: %i' % k)
        d.stepInference(Q, tmp1, tmp2)
        R.append(np.argmax(Q, axis=0).reshape((H, W)))

    R = list(R)

    l, cnt = md(np.asarray(R, dtype='uint8'), axis=0)

    l = np.squeeze(l)
    cnt = np.squeeze(cnt)
    p = cnt / len(R)

    del l, cnt

    preds = np.array(Q, dtype=np.float32).reshape(
        (len(labels), H, W)).transpose(1, 2, 0)  ##labels+1

    return np.argmax(Q, axis=0).reshape(
        (H, W)), p, np.squeeze(np.expand_dims(preds, 0))
예제 #2
0
def writeout(tmp, cl, labels, outpath, thres):

    l, cnt = md(cl.flatten())
    l = np.squeeze(l)
    if cnt / len(cl.flatten()) > thres:
        outfile = id_generator() + '.jpg'
        fp = outpath + os.sep + labels[l] + os.sep + outfile
        imwrite(fp, tmp)  ##imsave(fp, tmp)
예제 #3
0
def OpenImage(image_path, im_order):
    """
    Returns the image in numpy array format
    Input:
        image_path : string
            Full or relative path to image
        config : dict
            Dictionary of parameters set in the parameters file
    Output:
        numpy array of image 2D or 3D or 4+D
    """

    with rasterio.open(image_path) as src:
        # How many bands does this image have?
        layer = src.read()

        if layer.shape[0] == 3:
            num_bands = src.count
            print('Number of bands detected in image: {n}\n'.format(n=num_bands))
            del layer

        elif layer.shape[0] == 4:

            img = tifffile.imread(image_path)
            img = img_as_ubyte(img)
            val, _ = md(img[:,:,-1].flatten()) # get mode value
            del img, layer

            if val>254: #if val is 255 (type agnositic to use >254 instead of ==255)
                num_bands = 3
            else:
                num_bands = src.count
            print('Number of bands detected in image: {n}\n'.format(n=num_bands))

    ## ok, we've dealt with every case and determined the number of bands

    if (image_path.lower()[-3:] == 'tif') | (image_path.lower()[-4:] == 'tiff'):
        if num_bands>3:
           try: #4+ band tif file
              img = tifffile.imread(image_path)
              profile = None
           except: ##<=3 band tif file
              img = cv2.imread(image_path)
              if im_order=='RGB':
                 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
              elif im_order=='BGR':
                 img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
              profile = None
        else: #3-band geotiff
           img, profile = ReadGeotiff(image_path, im_order)
    else: ###<=3 band non-tif file
        img = cv2.imread(image_path)
        if im_order=='RGB':
           img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        elif im_order=='BGR':
           img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        profile = None
    return np.squeeze(img), profile