def postprocess_image(params):
    # load the prob map from a previously generated numpy array
    print "laoding probability map"
    prob_map = np.load(pc_config.fname_segmentation_pmap_npy)

    if params["training_type"] == "segmentation":
        pass  # here one could smooth the segmentation or set a treshold

    if params["training_type"] == "objectdetection":
        # here comes the peak finder as in ciresan mitosis 2013
        p_thresh = 0.9  # 0.21
        prob_map_peaks = np.zeros((prob_map.shape[0], prob_map.shape[1]))
        coords_list = []
        radius = params["object_size"]
        while True:
            coords_max = np.unravel_index(prob_map.argmax(), prob_map.shape)
            cx = coords_max[0]
            cy = coords_max[1]
            print "peak at", cx, cy, prob_map[cx, cy]
            if prob_map[cx, cy] < p_thresh:
                break
            coords_list.append([cx, cy])
            # remove these pixels
            prob_map = pc_utilities.circle_mask(
                cx, cy, int(radius * 1.5), prob_map, 0
            )  # avoid double detection by a slightly larger circle
            # draw new prob map with the coords
            prob_map_peaks = pc_utilities.circle_mask(cx, cy, radius, prob_map_peaks, 1)

    # test
    # pc_utilities.plot_image(prob_map_peaks)

    # save png and json
    if params["training_type"] == "segmentation":
        prob_map_int = (prob_map * 255.0).astype("uint8")
        # save json prob map
        print "saving json probability map to ", pc_config.fname_segmentation_pmap_json
        json.dump(
            prob_map.tolist(),
            codecs.open(pc_config.fname_segmentation_pmap_json, "w", encoding="utf-8"),
            separators=(",", ":"),
            sort_keys=True,
            indent=4,
        )
        # save png
        print "saving png probability map to ", pc_config.fname_segmentation_pmap_imagepath + pc_dataset.fnames_images[
            pc_config.eval_img_id
        ]
        img = Image.fromarray(prob_map_int)
        img.save(pc_config.fname_segmentation_pmap_imagepath + pc_dataset.fnames_images[pc_config.eval_img_id])

    if params["training_type"] == "objectdetection":
        prob_map_int = (255.0 - (prob_map_peaks * 255.0)).astype("uint8")
        # save json of coords list
        print "saving json points to", pc_config.fname_objectdetection_coords_json
        json.dump(
            coords_list,
            codecs.open(pc_config.fname_objectdetection_coords_json, "w", encoding="utf-8"),
            separators=(",", ":"),
            sort_keys=True,
            indent=4,
        )
        # save png
        print "saving png probability map to ", pc_config.fname_segmentation_pmap_imagepath + pc_dataset.fnames_images[
            pc_config.eval_img_id
        ]
        img = Image.fromarray(prob_map_int)
        img.save(pc_config.fname_segmentation_pmap_imagepath + pc_dataset.fnames_images[pc_config.eval_img_id])
def read_imagesandlabels_fromfile(params):
    mode=params['mode']
    training_id=params['training_id']
    training_type=params['training_type']
    job_id=params['job_id']
    imgid_min=params['image_id_min']
    imgid_max=params['image_id_max']
    
    imgarray_traindata = []
    imgarray_trainlabels = []
    imgarray_evaldata = []
    global fnames_images
    
    #image filenames
    if mode=='training':
        imgpath = pc_config.datastorage_path+"trainedalgorithms/"+training_id+"/dataset/training_data/webview/"
    if mode=='evaluation':
        imgpath = pc_config.datastorage_path+"analysisjobs/"+job_id+"/input/"
    fnames_images = [ f for f in listdir(imgpath) if isfile(join(imgpath,f)) ]
    fnames_images.sort()

    #check if labels exits. if not do not consider the image
    if mode=='training':
        for i in range(len(fnames_images)):
            fname_image = os.path.splitext(os.path.basename(fnames_images[i]))[0]
            fname_label = fname_image + ".json"
            if not (os.path.isfile(fname_label)): 
                del fnames_images[i]
            
    #load images
    for i in range(imgid_min,imgid_max):
        if mode=='training':
            fname = imgpath+fnames_images[i]
            print "reading image", fname
            #with pil
            img = Image.open(fname).convert('L') 
            #img.show()
            imgarray = np.array(img,dtype="float")
            if i==imgid_min: #generate array with right x y dimensions
                imgarray_traindata = np.zeros( (imgid_max-imgid_min,imgarray.shape[0],imgarray.shape[1]) , dtype="float32")
            imgarray_traindata[i-imgid_min] = imgarray/255.
            
        if mode=='evaluation':        
            fname = imgpath+fnames_images[i]
            print "reading image", fname
            #with pil
            img = Image.open(fname).convert('L')
            #img.show()
            imgarray = np.array(img)
            if i==imgid_min: #generate array with right x y dimensions
                imgarray_evaldata = np.zeros( (imgid_max-imgid_min,imgarray.shape[0],imgarray.shape[1]) , dtype="float32")
            imgarray_evaldata[i-imgid_min] = imgarray/255.

    #test
    # img = Image.fromarray(imgarray_traindata[0]*255) 
    # img.show()
           
    #load labels
    if (training_type=='objectdetection'):
        radius = params['object_size']
        print "radius for labels:", radius
    if (mode=='training'):
        labelpath = pc_config.datastorage_path+"trainedalgorithms/"+training_id+"/dataset/training_data/label/"
        fnames2 = [ f for f in listdir(labelpath) if isfile(join(labelpath,f)) ]
        fnames2.sort()
        #print fnames2
        for i in range(imgid_min,imgid_max):
            if i==imgid_min: #generate array with right x y dimensions
                imgarray_trainlabels = np.zeros( (imgid_max-imgid_min,imgarray.shape[0],imgarray.shape[1]) , dtype="uint8")
            fname = labelpath+fnames2[i]
            print "reading labels", fname
            json_data=open(fname).read()
            
            if (training_type=='segmentation'):
                labels = json.loads(json_data)
                labels = np.array(labels)
                imgarray_trainlabels[i-imgid_min] = labels #1-labels #we want labels to be 1 and bg to be 0.
                
            if (training_type=='objectdetection'):
                coords = json.loads(json_data)
                max_x=imgarray.shape[0]
                max_y=imgarray.shape[1]
                for j in range(len(coords)):
                    #dominiks convention: x is horizontal, y is vertical, origin is left top.
                    #the swap here is because imagefromarray expects the [0] coordinate, i.e. x to be the vertical and not horizontal axis
                    #numpys first index is the row, not the colum. pil draws like print(nparray), so again x is vertical.
                    #this also maps to how np arrays are created e.g. a = [[x1y1,x1y2],[x2y1,x2y2]].
                    cx = coords[j]['y'] 
                    cy = coords[j]['x']
                    #print cx,cy
                    #imgarray_trainlabels[i-imgid_min,cx,cy] = 1 
                    imgarray_trainlabels[i-imgid_min] = pc_utilities.circle_mask(cx,cy,radius,imgarray_trainlabels[i-imgid_min],1)

                    
    #test
    # img = Image.fromarray((1-imgarray_trainlabels[0])*255)  #invert color. 255 = white, but we want labels to be black. 
    # img.show()
    # sys.exit()
            
    print "Done."
    return imgarray_traindata, imgarray_trainlabels, imgarray_evaldata