Ejemplo n.º 1
0
def get_pred_img_pixelwise(filename, image_idx, datatype, model, PIXEL_DEPTH,
                           NEW_DIM_TRAIN, prediction_test_dir):

    i = image_idx
    # Specify the path of the
    if (datatype == 'train'):
        imageid = "satImage_%.3d" % image_idx
        image_filename = filename + imageid + ".png"
    elif (datatype == 'test'):
        imageid = "/test_%d" % i
        image_filename = filename + imageid + imageid + ".png"
    else:
        print('Error: Enter test or train')
    # loads the image in question
    img = Image.open(image_filename)
    output_prediction = get_prediction_pixel(img, model,
                                             NEW_DIM_TRAIN)  #(1,224,224)
    output_prediction = np.transpose(output_prediction,
                                     (1, 2, 0))  #(224,224,1)
    predict_img = np.asarray(output_prediction)

    # Changes into a 3D array, to easier turn into image
    predict_img_3c = np.zeros((predict_img.shape[0], predict_img.shape[1], 3),
                              dtype=np.uint8)
    predict_img8 = np.squeeze(img_float_to_uint8(predict_img, PIXEL_DEPTH))
    predict_img8[predict_img8 >= 128] = 255
    predict_img8[predict_img8 < 128] = 0
    predict_img_3c[:, :, 0] = predict_img8
    predict_img_3c[:, :, 1] = predict_img8
    predict_img_3c[:, :, 2] = predict_img8

    imgpred = Image.fromarray(predict_img_3c)
    imgpredict = imgpred.resize((608, 608))

    return imgpredict, img
Ejemplo n.º 2
0
def get_predictionimage(filename, image_idx, datatype, model, IMG_PATCH_SIZE,
                        PIXEL_DEPTH):

    i = image_idx
    # Specify the path of the
    if (datatype == 'train'):
        imageid = "satImage_%.3d" % image_idx
        image_filename = filename + imageid + ".png"
    elif (datatype == 'test'):
        imageid = "/test_%d" % i
        image_filename = filename + imageid + imageid + ".png"
    else:
        print('Error: Enter test or train')

    # loads the image in question
    img = mpimg.imread(image_filename)
    #data = [img_patches[i][j] for i in range(len(img_patches)) for j in range(len(img_patches[i]))]

    output_prediction = get_prediction(img, model, IMG_PATCH_SIZE)
    predict_img = label_to_img(img.shape[0], img.shape[1], IMG_PATCH_SIZE,
                               IMG_PATCH_SIZE, output_prediction)

    # Changes into a 3D array, to easier turn into image
    predict_img_3c = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    predict_img8 = img_float_to_uint8(predict_img, PIXEL_DEPTH)
    predict_img_3c[:, :, 0] = predict_img8
    predict_img_3c[:, :, 1] = predict_img8
    predict_img_3c[:, :, 2] = predict_img8

    imgpred = Image.fromarray(predict_img_3c)

    return imgpred
Ejemplo n.º 3
0
def get_predictionimage_context(filename, image_idx, datatype, model,
                                IMG_PATCH_SIZE, CONTEXT_SIZE, PIXEL_DEPTH):

    if (datatype == 'train'):
        imageid = "satImage_%.3d" % image_idx
        image_filename = filename + imageid + ".png"
    elif (datatype == 'test'):
        imageid = "/test_%d" % image_idx
        image_filename = filename + imageid + imageid + ".png"
    else:
        print('Error: Enter test or train')

    img = mpimg.imread(image_filename)

    output_prediction = get_prediction_context(img, model, IMG_PATCH_SIZE,
                                               CONTEXT_SIZE)
    predict_img = label_to_img(img.shape[0], img.shape[1], IMG_PATCH_SIZE,
                               IMG_PATCH_SIZE, output_prediction)

    # Changes into a 3D array, to easier turn into image
    predict_img_3c = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    predict_img8 = img_float_to_uint8(predict_img, PIXEL_DEPTH)
    predict_img_3c[:, :, 0] = predict_img8
    predict_img_3c[:, :, 1] = predict_img8
    predict_img_3c[:, :, 2] = predict_img8

    imgpred = Image.fromarray(predict_img_3c)

    return imgpred
Ejemplo n.º 4
0
def make_img_overlay(img, predicted_img, PIXEL_DEPTH):
    w = img.shape[0]
    h = img.shape[1]
    color_mask = np.zeros((w, h, 3),
                          dtype=np.uint8)  #samme størrelse som bildet
    color_mask[:, :,
               0] = predicted_img * PIXEL_DEPTH  #0 eller 3 Endrer bare R i rgb, altså gjør bildet

    img8 = img_float_to_uint8(img, PIXEL_DEPTH)
    background = Image.fromarray(img8, 'RGB').convert("RGBA")
    overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA")
    new_img = Image.blend(background, overlay, 0.2)
    return new_img
Ejemplo n.º 5
0
def make_img_overlay_pixel(img, predicted_img, PIXEL_DEPTH):
    w, h = img.size
    predicted_img = np.asarray(predicted_img)
    color_mask = np.zeros((w, h, 3), dtype=np.uint8)
    # creating a mask of the predicted image
    color_mask[:, :, 0] = predicted_img[:, :, 0]

    img8 = img_float_to_uint8(img, PIXEL_DEPTH)
    background = Image.fromarray(img8, 'RGB').convert("RGBA")
    overlay = Image.fromarray(color_mask, 'RGB').convert("RGBA")

    new_img = Image.blend(background, overlay, 0.2)

    return new_img
Ejemplo n.º 6
0
def get_prediction_with_overlay_pixelwise(filename, image_idx, datatype, model,
                                          PIXEL_DEPTH, NEW_DIM_TRAIN,
                                          IMG_PATCH_SIZE):

    i = image_idx
    if (datatype == 'train'):
        imageid = "satImage_%.3d" % image_idx
        image_filename = filename + imageid + ".png"
    elif (datatype == 'test'):
        imageid = "/test_%d" % i
        image_filename = filename + imageid + imageid + ".png"
    else:
        print('Error: Enter test or train')

    img = Image.open(image_filename)

    # Returns a matrix with a prediction for each pixel
    output_prediction = get_prediction_pixel(img, model,
                                             NEW_DIM_TRAIN)  #(1,400,400)
    output_prediction = np.transpose(output_prediction,
                                     (1, 2, 0))  #(400,400,1)

    predict_img_3c = np.zeros(
        (output_prediction.shape[0], output_prediction.shape[1], 3),
        dtype=np.uint8)
    predict_img8 = np.squeeze(
        img_float_to_uint8(output_prediction, PIXEL_DEPTH))
    predict_img8[predict_img8 >= 128] = 255
    predict_img8[predict_img8 < 128] = 0
    predict_img_3c[:, :, 0] = predict_img8
    predict_img_3c[:, :, 1] = predict_img8
    predict_img_3c[:, :, 2] = predict_img8

    newPred = label_to_img_unet(predict_img_3c.shape[0],
                                predict_img_3c.shape[1], IMG_PATCH_SIZE,
                                IMG_PATCH_SIZE, output_prediction, datatype)
    imgpred = Image.fromarray(newPred)
    oimg = make_img_overlay_pixel(img, imgpred, PIXEL_DEPTH)

    return oimg, imgpred