Exemplo n.º 1
0
def thresholdCenterlines(nms, tLow=0.012, tHigh=0.12, bimodal=True):
    """ Uses a continuity-preserving hysteresis thresholding to classify
    centerlines.
    
    Inputs:
    nms -- Non-maxima suppressed singularity index response

    Keyword Arguments:
    bimodal -- true if the areas of rivers in the image are sufficiently
               large that the distribution of ψ is bimodal
    tLow -- lower threshold (automatically set if bimodal=True)
    tHigh -- higher threshold (automatically set if bimodal=True)

    Returns:
    centerlines -- a binary matrix that indicates centerline locations
    """

    if bimodal:
        #Otsu's algorithm
        nms = preprocess.double2im(nms, 'uint8')
        tHigh,_ = cv2.threshold(nms, nms.min(), nms.max(), cv2.THRESH_OTSU)
        tLow = tHigh * 0.1

    strongCenterline    = nms >= tHigh
    centerlineCandidate = nms >= tLow

    # Find connected components that has at least one strong centerline pixel
    strel = np.ones((3, 3), dtype=bool)
    cclabels, numcc = ndlabel(centerlineCandidate, strel)
    sumstrong = ndsum(strongCenterline, cclabels, list(range(1, numcc+1)))
    centerlines = np.hstack((0, sumstrong > 0)).astype('bool')
    centerlines = centerlines[cclabels]

    return centerlines
Exemplo n.º 2
0
def thresholdCenterlines(nms, tLow=0.012, tHigh=0.12, bimodal=True):
    """ Uses a continuity-preserving hysteresis thresholding to classify
    centerlines.
    
    Inputs:
    nms -- Non-maxima suppressed singularity index response

    Keyword Arguments:
    bimodal -- true if the areas of rivers in the image are sufficiently
               large that the distribution of ψ is bimodal
    tLow -- lower threshold (automatically set if bimodal=True)
    tHigh -- higher threshold (automatically set if bimodal=True)

    Returns:
    centerlines -- a binary matrix that indicates centerline locations
    """

    if bimodal:
        #Otsu's algorithm
        nms = preprocess.double2im(nms, 'uint8')
        tHigh,_ = cv2.threshold(nms, nms.min(), nms.max(), cv2.THRESH_OTSU)
        tLow = tHigh * 0.1

    strongCenterline    = nms >= tHigh
    centerlineCandidate = nms >= tLow

    # Find connected components that has at least one strong centerline pixel
    strel = np.ones((3, 3), dtype=bool)
    cclabels, numcc = ndlabel(centerlineCandidate, strel)
    sumstrong = ndsum(strongCenterline, cclabels, range(1, numcc+1))
    centerlines = np.hstack((0, sumstrong > 0)).astype('bool')
    centerlines = centerlines[cclabels]

    return centerlines
Exemplo n.º 3
0
def test_double2im():
    '''
    Unit test for double2im function
    '''
    # create dummy image
    I = np.array([10. / 255, 50. / 255, 100. / 255])
    # apply function
    result = preprocess.double2im(I, 'uint8')
    # make assertion
    assert np.all(result) == np.all(np.array([10, 50, 100], dtype='uint8'))
Exemplo n.º 4
0
centerlines = delineate.thresholdCenterlines(nms)

# Generate a raster map of the extracted channels
raster = visualization.generateRasterMap(centerlines, orient, widthMap)

# Generate a vector map of the extracted channels
visualization.generateVectorMap(centerlines,
                                orient,
                                widthMap,
                                saveDest="vector.pdf")

# Generate a quiver plot
visualization.quiverPlot(psi, orient, saveDest="quiver.pdf")

# Save the images that are created at the intermediate steps
cv2.imwrite("mndwi.TIF", cv2.normalize(I1, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("psi.TIF", cv2.normalize(psi, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("nms.TIF", cv2.normalize(nms, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("centerlines.TIF", centerlines.astype(int) * 255)
cv2.imwrite("rasterMap.TIF",
            cv2.normalize(raster, None, 0, 255, cv2.NORM_MINMAX))

# An example of exporting a geotiff file
gm = georef.loadGeoMetadata("LC81380452015067LGN00_B3.TIF")
psi = preprocess.contrastStretch(psi)
psi = preprocess.double2im(psi, 'uint16')
georef.saveAsGeoTiff(gm, psi, "psi_geotagged.TIF")

# Export the (coordinate, width) pairs to a comma separated text file
georef.exportCSVfile(centerlines, widthMap, gm, "results.csv")
Exemplo n.º 5
0
    B6 = cv2.imread(B6s[i], cv2.IMREAD_UNCHANGED)
    I1 = preprocess.mndwi(B3, B6)
    psi, widthMap, orient = singularity_index.applyMMSI(I1,
                                                        filters,
                                                        togglePolarity=False)
    nms = delineate.extractCenterlines(orient, psi)
    centerlines = delineate.thresholdCenterlines(nms)

    print("creating files")
    gm = georef.loadGeoMetadata(B3s[i])
    base = os.path.basename(B3s[i])
    georef.exportCSVfile(centerlines, widthMap, gm,
                         str(base) + "_geo_points.csv")

    psi = preprocess.contrastStretch(psi)
    psi = preprocess.double2im(psi, 'uint16')
    georef.saveAsGeoTiff(gm, psi, str(base) + "_geo_psi.TIF")

    mndwi = preprocess.contrastStretch(I1)
    mndwi = preprocess.double2im(mndwi, 'uint16')
    georef.saveAsGeoTiff(gm, mndwi, str(base) + "_geo_mndwi.TIF")
    #optional other file format
    #cv2.imwrite("MNDWI.TIF", cv2.normalize(I1, None, 0, 255, cv2.NORM_MINMAX))

    centerlines = preprocess.contrastStretch(centerlines)
    centerlines = preprocess.double2im(centerlines, 'uint16')
    georef.saveAsGeoTiff(gm, centerlines, str(base) + "_geo_centerlines.TIF")
    #optional other file format
    #cv2.imwrite("centerlines.TIF", centerlines.astype(int)*255)
print('completed')
Exemplo n.º 6
0
psi, widthMap, orient = singularity_index.applyMMSI(I1, filters)

# Extract channel centerlines
nms = delineate.extractCenterlines(orient, psi)
centerlines = delineate.thresholdCenterlines(nms)

# Generate a raster map of the extracted channels
raster = visualization.generateRasterMap(centerlines, orient, widthMap)

# Generate a vector map of the extracted channels
visualization.generateVectorMap(centerlines, orient, widthMap, saveDest = "vector.pdf")

# Generate a quiver plot
visualization.quiverPlot(psi, orient, saveDest = "quiver.pdf")

# Save the images that are created at the intermediate steps
cv2.imwrite("mndwi.TIF", cv2.normalize(I1, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("psi.TIF", cv2.normalize(psi, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("nms.TIF", cv2.normalize(nms, None, 0, 255, cv2.NORM_MINMAX))
cv2.imwrite("centerlines.TIF", centerlines.astype(int)*255)
cv2.imwrite("rasterMap.TIF", cv2.normalize(raster, None, 0, 255, cv2.NORM_MINMAX))

# An example of exporting a geotiff file
gm = georef.loadGeoMetadata("LC81380452015067LGN00_B3.TIF")
psi = preprocess.contrastStretch(psi)
psi = preprocess.double2im(psi, 'uint16')
georef.saveAsGeoTiff(gm, psi, "psi_geotagged.TIF")

# Export the (coordinate, width) pairs to a comma separated text file
georef.exportCSVfile(centerlines, widthMap, gm, "results.csv")