Example #1
0
def generateTrainingData(image, surface_class, bandnames):
    im = image[2:5, :, :].transpose()

    ims = np.zeros([image.shape[1], image.shape[2], 3])
    for i, band in enumerate([2, 1, 0]):
        ims[:, :, i] = plot.adjust_band(image[band, :, :], kind='linear')

    # Get Water data
    fig = plt.figure()
    t = plt.gca()
    im = plt.imshow(ims)

    PD = pickData(t)

    axclear = plt.axes([0.0, 0.0, 0.1, 0.1])
    bclear = Button(plt.gca(), 'Clear')
    bclear.on_clicked(PD.clear)

    axdone = plt.axes([0.2, 0.0, 0.1, 0.1])
    bdone = Button(plt.gca(), 'Done')
    bdone.on_clicked(PD.done)

    fig.canvas.mpl_connect('button_press_event', PD)

    im.set_picker(5)  # Tolerance in points

    plt.show()

    # Convert rectangles to DF
    df = pandas.DataFrame()
    for rect in PD.rects:
        # Get indexes at bottom left
        botleft = rect.get_xy()
        botleft = [math.ceil(i) for i in botleft]

        # Get indexes at top right
        topright = [
            botleft[0] + rect.get_width(),
            botleft[1] + rect.get_height(),
        ]
        topright = [math.ceil(i) for i in topright]

        # Get image rows
        ys = [botleft[1], topright[1]]
        xs = [botleft[0], topright[0]]
        sample = image[:, min(ys):max(ys), min(xs):max(xs)]
        print(sample.shape)

        df = pandas.concat([df, dataBox2df(sample,
                                           bandnames)]).reset_index(drop=True)

    df['class'] = surface_class

    return df
Example #2
0
        img[b, :, :][img[4, :, :] == 255] = 255

# Remove map band
img = img[:4, :, :]

#scaled_img = resize(img, (int(scale * float(rows)), int(scale * float(cols))))

# reshape into long 2d array (nrow * ncol, nband) for classification
reshaped_img = reshape_as_image(img)

# Kmeans
k = 8
kmeans_predictions = KMeans(n_clusters=k, random_state=0,
                            algorithm='full').fit(reshaped_img.reshape(-1, 4))
kmeans_predictions_2d = kmeans_predictions.labels_.reshape(rows, cols)

# Visualize

rgb = img[0:3]
rgb_norm = adjust_band(rgb)  # normalize bands to range between 1.0 to 0.0
rgb_reshaped = reshape_as_image(rgb_norm)  # reshape to [rows, cols, bands]

fig, axs = plt.subplots(2, 1, figsize=(10, 20))
show(rgb_norm, ax=axs[1])
axs[1].set_title("RGB")

axs[0].imshow(kmeans_predictions_2d)
axs[0].set_title("KMEANS")

plt.show()
Example #3
0

sentinel_rgb.bounds
sentinel_rgb.crs
sentinel_rgb.transform
sentinel_rgb.indexes
sentinel_rgb.read(3)

img = sentinel_rgb.read(3)
show(img)



#producing RGB map with sentinel image
from rasterio.plot import show, adjust_band
imgdata = np.array([adjust_band(sentinel_rgb.read(i)) for i in (4,3,2)])
show(imgdata*10)  # factor 10 to increase brightness


#NDWI Bands green and NIR
band3 = rasterio.open('Desktop/Dersler/rasterio deneme/sentinel_band3.tif') #green
band8 = rasterio.open('Desktop/Dersler/rasterio deneme/sentinel_band8.tif') #NIR

band3.height
band3.width
band3.crs
band3.transform

band8.height
band8.width
band8.crs
Example #4
0
def test_plot_normalize():
    a = np.linspace(1, 6, 10)
    b = adjust_band(a, 'linear')
    np.testing.assert_array_almost_equal(np.linspace(0, 1, 10), b)
Example #5
0
def test_plot_normalize():
    a = np.linspace(1, 6, 10)
    b = adjust_band(a, 'linear')
    np.testing.assert_array_almost_equal(np.linspace(0, 1, 10), b)