Ejemplo n.º 1
1
def human_afterloop(output_directory, pre_time, fle_name, buffer_directory):
    start2 = time()

    d_c = import_edited(buffer_directory)
    rebw = load(open(buffer_directory+'-'+'DO_NOT_TOUCH_ME.dmp','rb'))
    seg_dc = (label(d_c,neighbors=4)+1)*d_c
    if np.max(seg_dc)<4:
        return 'FAILED: mask for %s looks unsegmented' % fle_name

    colormap = repaint_culsters(int(np.max(seg_dc)))

    segs = len(set(seg_dc.flatten().tolist()))-1

    # shows the result before saving the clustering and printing to the user the number of the images
    plt.subplot(1,2,1)
    plt.title(fle_name)
    plt.imshow(rebw, cmap='gray', interpolation='nearest')

    plt.subplot(1,2,2)
    plt.title('Segmentation - clusters: %s'%str(segs))
    plt.imshow(mark_boundaries(rebw, d_c))
    plt.imshow(seg_dc, cmap=colormap, interpolation='nearest', alpha=0.3)

    plt.show()

    plt.imshow(mark_boundaries(rebw, d_c))
    plt.imshow(seg_dc, cmap=colormap, interpolation='nearest', alpha=0.3)

    plt.savefig(path.join(output_directory, fle_name+'_%s_clusters.png'%str(segs)), dpi=500, bbox_inches='tight', pad_inches=0.0)

    return fle_name+'\t clusters: %s,\t total time : %s'%(segs, "{0:.2f}".format(time()-start2+pre_time))
def fun_compare_colorsegmentation_and_display(image_data, number_segments=250, compactness_factor=10):
    """
    The function is a copy of what does this link http://scikit-image.org/docs/dev/auto_examples/plot_segmentations.html
    """
    segments_fz = felzenszwalb(image_data, scale=100, sigma=0.5, min_size=50)
    segments_slic = slic(image_data, n_segments=number_segments, compactness=compactness_factor, sigma=1)
    segments_quick = quickshift(image_data, kernel_size=3, max_dist=6, ratio=0.5)

    print ("Felzenszwalb's number of segments: %d" % len(np.unique(segments_fz)))
    print ("Slic number of segments: %d" % len(np.unique(segments_slic)))
    print ("Quickshift number of segments: %d" % len(np.unique(segments_quick)))

    fig, ax = plt.subplots(1, 3, sharex=True, sharey=True, subplot_kw={"adjustable": "box-forced"})
    fig.set_size_inches(8, 3, forward=True)
    fig.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.05, 0.05)

    ax[0].imshow(mark_boundaries(image_data, segments_fz, color=(1, 0, 0)))
    ax[0].set_title("Felzenszwalbs's method")
    ax[1].imshow(mark_boundaries(image_data, segments_slic, color=(1, 0, 0)))
    ax[1].set_title("SLIC")
    ax[2].imshow(mark_boundaries(image_data, segments_quick, color=(1, 0, 0)))
    ax[2].set_title("Quickshift")
    for a in ax:
        a.set_xticks(())
        a.set_yticks(())
    plt.show()
Ejemplo n.º 3
0
def test_mark_boundaries():
    image = np.zeros((10, 10))
    label_image = np.zeros((10, 10), dtype=np.uint8)
    label_image[2:7, 2:7] = 1

    ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
                    [0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
                    [0, 1, 1, 0, 0, 0, 1, 1, 0, 0],
                    [0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

    marked = mark_boundaries(image, label_image, color=white, mode='thick')
    result = np.mean(marked, axis=-1)
    assert_array_equal(result, ref)

    ref = np.array([[0, 2, 2, 2, 2, 2, 2, 2, 0, 0],
                    [2, 2, 1, 1, 1, 1, 1, 2, 2, 0],
                    [2, 1, 1, 1, 1, 1, 1, 1, 2, 0],
                    [2, 1, 1, 2, 2, 2, 1, 1, 2, 0],
                    [2, 1, 1, 2, 0, 2, 1, 1, 2, 0],
                    [2, 1, 1, 2, 2, 2, 1, 1, 2, 0],
                    [2, 1, 1, 1, 1, 1, 1, 1, 2, 0],
                    [2, 2, 1, 1, 1, 1, 1, 2, 2, 0],
                    [0, 2, 2, 2, 2, 2, 2, 2, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    marked = mark_boundaries(image, label_image, color=white,
                             outline_color=(2, 2, 2), mode='thick')
    result = np.mean(marked, axis=-1)
    assert_array_equal(result, ref)
Ejemplo n.º 4
0
def test_mark_boundaries():
    image = np.zeros((10, 10))
    label_image = np.zeros((10, 10))
    label_image[2:7, 2:7] = 1

    ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
                    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    result = mark_boundaries(image, label_image, color=(1, 1, 1)).mean(axis=2)
    assert_array_equal(result, ref)

    ref = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                    [0, 0, 1, 1, 1, 1, 1, 1, 2, 0],
                    [0, 0, 1, 2, 2, 2, 2, 1, 2, 0],
                    [0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
                    [0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
                    [0, 0, 1, 2, 0, 0, 0, 1, 2, 0],
                    [0, 0, 1, 1, 1, 1, 1, 2, 2, 0],
                    [0, 0, 2, 2, 2, 2, 2, 2, 0, 0],
                    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    result = mark_boundaries(image, label_image, color=(1, 1, 1),
                             outline_color=(2, 2, 2)).mean(axis=2)
    assert_array_equal(result, ref)
Ejemplo n.º 5
0
def updateParametros(val):
    global p_segmentos, p_sigma, p_compactness, segments, image, cuda_python

    if(val == "Python SLIC"):
	cuda_python = 0
    elif(val == "CUDA gSLICr"):
	cuda_python = 1

    p_segmentos = int("%d" % (slider_segmentos.val))
    p_sigma = slider_sigma.val
    p_compactness = slider_compactness.val
    
    image = c_image.copy()

    if(cuda_python == 0):
	start_time = time.time()
    	segments = slic(img_as_float(image), n_segments=p_segmentos, sigma=p_sigma, compactness=p_compactness)
	print("--- Tempo Python skikit-image SLIC: %s segundos ---" % (time.time() - start_time))
    else:
	start_time = time.time()
	gSLICrInterface.process( p_segmentos)
	print("--- Tempo C++/CUDA gSLICr:          %s segundos ---" % (time.time() - start_time))
	segments = cuda_seg

    obj.set_data(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments, outline_color=p_outline))
    draw()
Ejemplo n.º 6
0
def overlay_cells(cells, image, colors):
    "Overlay the edges of each individual cell in the provided image"

    tmp = color.gray2rgb(image)

    for k in cells.keys():
        c = cells[k]
        if c.selection_state == 1:
            col = colors[c.color_i][:3]

            for px in c.outline:
                x, y = px
                tmp[x, y] = col

            if c.sept_mask is not None:
                try:
                    x0, y0, x1, y1 = c.box
                    tmp[x0:x1, y0:y1] = mark_boundaries(tmp[x0:x1, y0:y1],
                                                        img_as_int(
                                                            c.sept_mask),
                                                        color=col)
                except IndexError:
                    c.selection_state = -1

    return tmp
Ejemplo n.º 7
0
def grabcut(img, targetness):
    u"""
    Segmenting the best target-like region from an targetness map.
    """

    mask = np.ones(img.shape[:2], np.uint8) * cv2.GC_BGD
    score_th = scoreatpercentile(targetness, 95)
    mask[targetness >= score_th] = cv2.GC_PR_FGD
    score_th = scoreatpercentile(targetness, 99)
    mask[targetness >= score_th] = cv2.GC_FGD
    mask = cv2.medianBlur(mask, 15)

    bgdModel = np.zeros((1, 65), np.float64)
    fgdModel = np.zeros((1, 65), np.float64)

    cv2.grabCut(img, mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)

    mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
    lab_mask2 = bwlabel(mask2)
    lab_list = np.unique(lab_mask2.flatten())[1:]
    lab_argmax = np.argmax(
        [np.max(targetness[lab_mask2 == i]) for i in lab_list])
    mask2[lab_mask2 != lab_list[lab_argmax]] = 0
    img2 = img.copy()
    img2[mask2 < 1, :] = [0, 43, 54]
    img2 = mark_boundaries(img2, mask2)

    return img2, mask2
Ejemplo n.º 8
0
	def onclick(event):
		#globals again
		global _clickclassi
		global bounded
		global _txt

		#If left mouse button click
		if event.button == 1:
			#calculate color for class
			color = cm(int(_clickclassi*(256.0/maxClasses)))[:3]
			#create mask containing only selected segment
			mask = np.zeros(img.shape[:2], dtype = "uint8")
			mask[segments == segments[int(event.ydata),int(event.xdata)]] = 2
			#Highlight segment on image and display
			bounded = mark_boundaries(bounded,mask,color=color,mode='thick')
			ax.imshow(bounded)
			plt.draw()
			#Add segment index and class to labels
			labels.append((segments[int(event.ydata),int(event.xdata)],_clickclassi))
		#If right mouse button clikc
		if event.button == 3:
			#Cycle through classes
			_clickclassi = (_clickclassi + 1) % maxClasses
			#Clear plot and redraw to get rid of existing text labels
			plt.cla()
			ax.imshow(bounded)
			_txt = plt.text(0,-20,labelText +str(iclasses[_clickclassi]))
			plt.draw()
 def _apply(self, img_msg, label_msg):
     bridge = cv_bridge.CvBridge()
     img = bridge.imgmsg_to_cv2(img_msg)
     label_img = bridge.imgmsg_to_cv2(label_msg)
     # publish only valid label region
     applied = img.copy()
     applied[label_img == 0] = 0
     applied_msg = bridge.cv2_to_imgmsg(applied, encoding=img_msg.encoding)
     applied_msg.header = img_msg.header
     self.pub_img.publish(applied_msg)
     # publish visualized label
     if img_msg.encoding in {'16UC1', '32SC1'}:
         # do dynamic scaling to make it look nicely
         min_value, max_value = img.min(), img.max()
         img = (img - min_value) / (max_value - min_value) * 255
         img = gray2rgb(img)
     label_viz_img = label2rgb(label_img, img, bg_label=0)
     label_viz_img = mark_boundaries(label_viz_img, label_img, (1, 0, 0))
     label_viz_img = (label_viz_img * 255).astype(np.uint8)
     label_viz_msg = bridge.cv2_to_imgmsg(label_viz_img, encoding='rgb8')
     label_viz_msg.header = img_msg.header
     self.pub_label_viz.publish(label_viz_msg)
     # publish mask
     if self._publish_mask:
         bg_mask = (label_img == 0)
         fg_mask = ~bg_mask
         bg_mask = (bg_mask * 255).astype(np.uint8)
         fg_mask = (fg_mask * 255).astype(np.uint8)
         fg_mask_msg = bridge.cv2_to_imgmsg(fg_mask, encoding='mono8')
         fg_mask_msg.header = img_msg.header
         bg_mask_msg = bridge.cv2_to_imgmsg(bg_mask, encoding='mono8')
         bg_mask_msg.header = img_msg.header
         self.pub_fg_mask.publish(fg_mask_msg)
         self.pub_bg_mask.publish(bg_mask_msg)
Ejemplo n.º 10
0
def explain(model, img, topLabels, numSamples, numFeatures, hideRest, hideColor, positiveOnly):
            
    img, oldImg = transform_img_fn(img)
    img = img*(1./255)
    prediction = model.predict(img)
    explainer = lime_image.LimeImageExplainer()
    img = np.squeeze(img)
    explanation = explainer.explain_instance(img, model.predict, top_labels=topLabels, hide_color=hideColor, num_samples=numSamples)
    temp, mask = explanation.get_image_and_mask(getTopPrediction(prediction[0]), positive_only=positiveOnly, num_features=numFeatures, hide_rest=hideRest)
    tempMask = mask * 255
    temp = Image.fromarray(np.uint8(tempMask))
    temp = temp.resize((oldImg.width, oldImg.height))
    temp = image.img_to_array(temp)
    temp = temp * 1./255
    temp = temp.astype(np.int64)
    temp = np.squeeze(temp)
    oldImgArr = image.img_to_array(oldImg)
    oldImgArr = oldImgArr * (1./255)
    oldImgArr = oldImgArr.astype(np.float64)
    imgExplained = mark_boundaries(oldImgArr, temp)
    imgFinal = np.uint8(imgExplained*255)
    img = Image.fromarray(imgFinal)
    imgByteArr = io.BytesIO()
    img.save(imgByteArr, format='JPEG')
    imgByteArr = imgByteArr.getvalue()


    return imgByteArr
Ejemplo n.º 11
0
def createFigure4(list_file, list_param, corpus_meta, prob_topic_doc, segment_dir, n_topic, output_dir):    
    for file_item in list_file:
        print file_item
        for topic in range(n_topic):
            print topic
            fig = plt.figure()
            img = img_as_float(io.imread(img_dir+file_item+'.ppm'))
            ax1 = fig.add_subplot(4,4, 1, axisbg='grey')
            ax1.set_xticks(()), ax1.set_yticks(())
            ax1.imshow(img)
            index=2
            for param in list_param:

                if 'slic' in param:
                    # print 'test', index
                    # print corpus_meta[0][1].split('-')[0]
                    segment_in_file = [item_corpus for item_corpus in corpus_meta if file_item == item_corpus[1].split('-')[0]]
                    print len(segment_in_file)
                    segments_res = csv2Array(segment_dir+'/'+file_item+'/'+file_item+'-'+param+'.sup')
                    img = img_as_float(io.imread(img_dir+file_item+'.ppm'))
                    output = np.zeros( (len(img), len(img[0])) )
                    for segment in segment_in_file:
                        # print prob_topic_doc[int(segment[0])][topic]
                        output[segments_res == int(segment[2])] = prob_topic_doc[int(segment[0])][topic]
                    output = mark_boundaries(output, segments_res)
                    ax1 = fig.add_subplot(4,4, index, axisbg='grey')
                    # ax1 = fig.add_subplot(5,10, index, axisbg='grey')
                    ax1.set_xticks(()), ax1.set_yticks(())
                    ax1.imshow(output)
                    index += 1 
            ensure_path(output_dir+'/'+file_item+'/')
            plt.savefig(output_dir+'/'+file_item+'/topic-'+str(topic)+'-'+file_item+'.pdf')
            plt.clf()
            plt.close()
Ejemplo n.º 12
0
	def remove_background(self):
		L_b = 3
		self.i_original = self.i_original[self.sub[1]:self.sub[3],self.sub[0]:self.sub[2],]
		segments = slic(self.i_original, n_segments=2, compactness=0.1,enforce_connectivity=False)
#		segments += 1
		temp = self.i_original
		if sum(sum(segments[:5,:5])) > 10:
			for ii in range(0,3):
				temp[:,:,ii] = (np.ones([self.i_original.shape[0],self.i_original.shape[1]])-segments)*self.i_original[:,:,ii]
				#Haut, Bas, Droite, Gauche
				temp[:L_b,:,ii] = 0
				temp[self.i_original.shape[0]-L_b-1:self.i_original.shape[0]-1,:,ii]=0
				temp[:,self.i_original.shape[1]-L_b-1:self.i_original.shape[1]-1,ii] = 0
				temp[:,:L_b,ii] = 0
		else:
			for ii in range(0,3):
				temp[:,:,ii] = segments*self.i_original[:,:,ii]
#				print "else"
				#Haut, Bas, Droite, Gauche
				temp[:L_b,:,ii] = 0
				temp[self.i_original.shape[0]-L_b-1:self.i_original.shape[0]-1,:,ii]=0
				temp[:,self.i_original.shape[1]-L_b-1:self.i_original.shape[1]-1,ii] = 0
				temp[:,:L_b,ii] = 0
#		pdb.set_trace()
		fig, ax = plt.subplots(1, 1)
		ax.imshow(mark_boundaries(self.i_original,segments))
		ax.imshow(temp)
		plt.show()
		p2, p98 = np.percentile(temp, (2, 98))
		temp = exposure.rescale_intensity(temp, in_range=(p2, p98))
		return temp
Ejemplo n.º 13
0
    def plot(self):
        self.ax.cla()
        if self.segment_label is None:
            self.ax.imshow(self.new_image)
        else:
            boundary_image = segmentation.mark_boundaries(self.new_image, self.segment_label)
            self.ax.imshow(boundary_image)
            self.ax.imshow(self.background_mask.astype(float), alpha=0.3)
            mask = 1 - self.background_mask
            content_on_canvas = np.zeros(list(mask.shape) + [4], np.uint8)    # Prepare the canvas
            for stitch_image in self.stitch_images:
                content = stitch_image['content']
                x = stitch_image['x']
                y = stitch_image['y']
                size = stitch_image['size']
                print(x, y, size)
                resized = misc.imresize(content, size)                      # Resize the image

                # Compute the intersecting length between canvas and content
                x_copy_width = mask.shape[1] - stitch_image['x']
                if resized.shape[1] < x_copy_width:
                    x_copy_width = resized.shape[1]
                y_copy_width = mask.shape[0] - stitch_image['y']
                if resized.shape[0] < y_copy_width:
                    y_copy_width = resized.shape[0]
                print(x_copy_width, y_copy_width)
                # Copy content onto canvas and apply mask
                new_content = np.zeros(list(mask.shape) + [4], np.uint8)
                new_content[y:y+y_copy_width, x:x+x_copy_width, :] = resized[0:y_copy_width, 0:x_copy_width, :]
                content_on_canvas = self.merge(content_on_canvas, new_content)
            content_on_canvas[:, :, 3] = np.multiply(mask, content_on_canvas[:, :, 3])
            self.ax.imshow(content_on_canvas)
        self.ax.figure.canvas.draw()
Ejemplo n.º 14
0
def visualize_pascal(plot_probabilities=False):
    data = load_pascal('val')
    ds = PascalSegmentation()
    for x, y, f, sps in zip(data.X, data.Y, data.file_names, data.superpixels):
        fig, ax = plt.subplots(2, 3)
        ax = ax.ravel()
        image = ds.get_image(f)
        y_pixel = ds.get_ground_truth(f)
        x_raw = load_kraehenbuehl(f)

        boundary_image = mark_boundaries(image, sps)

        ax[0].imshow(image)
        ax[1].imshow(y_pixel, cmap=ds.cmap)
        ax[2].imshow(boundary_image)
        ax[3].imshow(np.argmax(x_raw, axis=-1), cmap=ds.cmap, vmin=0, vmax=256)
        ax[4].imshow(y[sps], cmap=ds.cmap, vmin=0, vmax=256)
        ax[5].imshow(np.argmax(x, axis=-1)[sps], cmap=ds.cmap, vmin=0,
                     vmax=256)
        for a in ax:
            a.set_xticks(())
            a.set_yticks(())
        plt.savefig("figures_pascal_val/%s.png" % f, bbox_inches='tight')
        plt.close()
        if plot_probabilities:
            fig, ax = plt.subplots(3, 7)
            for k in range(21):
                ax.ravel()[k].matshow(x[:, :, k], vmin=0, vmax=1)
            for a in ax.ravel():
                a.set_xticks(())
                a.set_yticks(())
            plt.savefig("figures_pascal_val/%s_prob.png" % f,
                        bbox_inches='tight')
            plt.close()
    tracer()
    def update_slic(self):
        sec = self.auto_submasks_gscene.active_section

        t = time.time()
        self.slic_labelmaps[sec] = slic(self.contrast_stretched_images[sec].astype(np.float),
                                    sigma=SLIC_SIGMA, compactness=SLIC_COMPACTNESS,
                                    n_segments=SLIC_N_SEGMENTS, multichannel=False, max_iter=SLIC_MAXITER)
        sys.stderr.write('SLIC: %.2f seconds.\n' % (time.time() - t)) # 10 seconds, iter=100, nseg=1000;

        self.slic_boundary_images[sec] = img_as_ubyte(mark_boundaries(self.contrast_stretched_images[sec],
                                            label_img=self.slic_labelmaps[sec],
                                            background_label=-1, color=(1,0,0)))

        self.slic_image_feeder.set_image(sec=sec, numpy_image=self.slic_boundary_images[sec])
        self.gscene_slic.update_image(sec=sec)

        ####

        # self.ncut_labelmaps[sec] = normalized_cut_superpixels(self.contrast_stretched_images[sec], self.slic_labelmaps[sec])

        self.ncut_labelmaps[sec] = self.slic_labelmaps[sec]
        self.sp_dissim_maps[sec] = compute_sp_dissims_to_border(self.contrast_stretched_images[sec], self.ncut_labelmaps[sec])
        # self.sp_dissim_maps[sec] = compute_sp_dissims_to_border(self.thresholded_images[sec], self.ncut_labelmaps[sec])
        # self.border_dissim_images[sec] = generate_dissim_viz(self.sp_dissim_maps[sec], self.ncut_labelmaps[sec])
        # self.dissim_image_feeder.set_image(sec=sec, numpy_image=self.border_dissim_images[sec])
        # self.gscene_dissimmap.update_image(sec=sec)

        self.selected_dissim_thresholds[sec] = determine_dissim_threshold(self.sp_dissim_maps[sec], self.ncut_labelmaps[sec])
        self.ui.slider_dissimThresh.setValue(int(self.selected_dissim_thresholds[sec]/0.01))

        ######################################################

        self.update_init_submasks_image()
def main():

    filename = 'mesquitesFloat.png'
    img = io.imread(filename)

    #CONVERSIONS:
    #convert to lab color space
    #img = skimage.color.rgb2lab(img)
    #img = img_as_float(img)
    #io.imsave('mesquitesFloat.png', img)

    #print(img.shape)

    plt.figure(1)
    plt.imshow(img, cmap='gray')
    plt.axis('off')


    # loop over the number of segments
    for numSegments in (100, 200, 300):
        # apply SLIC and extract (approximately) the supplied number
        # of segments
        segments = slic(img, n_segments = numSegments, sigma = 1)

        # show the output of SLIC
        fig = plt.figure("Superpixels of -- %d segments" % (numSegments))
        subplot = fig.add_subplot(1, 1, 1)
        subplot.imshow(mark_boundaries(img, segments))
        plt.axis("off")



    plt.show()
def generateImageWithSuperPixelBoundaries(image, segmentationMask):
    # Function returns an image with superpixel boundaries displayed as lines.  It is assumed that the image was the source for the segmentation mask.
    # See [http://scikit-image.org/docs/dev/api/skimage.segmentation.html?highlight=slic#skimage.segmentation.mark_boundaries]
    # Function signature: skimage.segmentation.mark_boundaries(image, label_img, color=(1, 1, 0), outline_color=(0, 0, 0))
    
    superPixelImage = mark_boundaries(image, segmentationMask)
    return superPixelImage
	def showPredictionOutput(self):
		image_withText = self.image.copy()

		# show the output of the prediction with text
		for (i, segVal) in enumerate(np.unique(self.segments)):
			CORD = self.centerList[i]
			if self.predictionList[i] == "other":
				colorFont = (255, 0, 0) # "Blue color for other"
			else:
				colorFont = (0, 0, 255) # "Red color for ocean"

			#textOrg = CORD
			#textOrg = tuple(numpy.subtract((10, 10), (4, 4)))

			testOrg = (40,40) # need this for the if statment bellow

			# for some yet unknown reason CORD does sometime contain somthing like this [[[210 209]] [[205 213]] ...]
			# the following if statemnet is to not get a error becouse of this
			if len(CORD) == len(testOrg):
				#textOrg = tuple(np.subtract(CORD, (12, 0)))
				textOrg = CORD
				cv2.putText(self.image, self.predictionList[i], textOrg, cv2.FONT_HERSHEY_SIMPLEX, 0.1, colorFont, 3)
				markedImage = mark_boundaries(img_as_float(cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)), self.segments)
			else:
				pass

		cv2.imshow("segmented image", markedImage)
		cv2.waitKey(0)
def superpixels(image):
    """ given an input image, create super pixels on it
    """
    # we could try to limit the problem of holes in boundary by first over segmenting the image
    import matplotlib.pyplot as plt
    from skimage.segmentation import felzenszwalb, slic, quickshift
    from skimage.segmentation import mark_boundaries
    from skimage.util import img_as_float
    
    jac_float = img_as_float(image)
    plt.imshow(jac_float)
    #segments_fz = felzenszwalb(jac_float, scale=100, sigma=0.5, min_size=50)
    segments_slic = slic(jac_float, n_segments=600, compactness=0.01, sigma=0.001
        #, multichannel = False
        , max_iter=50) 
      
    fig, ax = plt.subplots(1, 1, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'})
    fig.set_size_inches(8, 3, forward=True)
    fig.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.05, 0.05)
    
    #ax[0].imshow(mark_boundaries(jac, segments_fz))
    #ax[0].set_title("Felzenszwalbs's method")
    ax.imshow(mark_boundaries(jac_float, segments_slic))
    ax.set_title("SLIC")           
    return segments_slic                     
Ejemplo n.º 20
0
def intensity_range2superpixels(im, superpixels, intMinT=0.95, intMaxT=1.05, debug=False, intMin=0, intMax=255):#, fromInt=0, toInt=255):

    superseeds = np.zeros_like(superpixels)

    #if not intMin and not intMax:
    #    hist, bins = skexp.histogram(im)
    #
    #    #zeroing values that are lower/higher than fromInt/toInt
    #    toLow = np.where(bins < fromInt)
    #    hist[toLow] = 0
    #    toHigh = np.where(bins > toInt)
    #    hist[toHigh] = 0
    #
    #    max_peakIdx = hist.argmax()
    #    intMin = intMinT * bins[max_peakIdx]
    #    intMax = intMaxT * bins[max_peakIdx]

    sp_means = np.zeros(superpixels.max()+1)
    for sp in range(superpixels.max()+1):
        values = im[np.where(superpixels==sp)]
        mean = np.mean(values)
        sp_means[sp] = mean

    idxs = np.argwhere(np.logical_and(sp_means>=intMin, sp_means<=intMax))
    for i in idxs:
        superseeds = np.where(superpixels==i[0], 1, superseeds)

    if debug:
        plt.figure(), plt.gray()
        plt.imshow(im), plt.hold(True), plt.imshow(mark_boundaries(im, superseeds, color=(1,0,0)))
        plt.axis('image')
        plt.show()

    return superseeds
Ejemplo n.º 21
0
def main():
    from pascal.pascal_helpers import load_pascal
    from datasets.pascal import PascalSegmentation
    from utils import add_edges
    from scipy.misc import imsave
    from skimage.segmentation import mark_boundaries

    ds = PascalSegmentation()
    data = load_pascal("train1")

    data = add_edges(data, independent=False)
    # X, Y, image_names, images, all_superpixels = load_data(
    # "train", independent=False)
    for x, name, sps in zip(data.X, data.file_names, data.superpixels):
        segments = get_km_segments(x, ds.get_image(name), sps, n_segments=25)
        boundary_image = mark_boundaries(mark_boundaries(ds.get_image(name), sps), segments[sps], color=[1, 0, 0])
        imsave("hierarchy_sp_own_25/%s.png" % name, boundary_image)
Ejemplo n.º 22
0
def fig_label_segments(fig, image, segments, label):
    labels, _ = ndimage.label(segments)
    image_label_overlay=label2rgb(labels, image=image)
    image_label_overlay=mark_boundaries(image, segments)
    fig.set_title(label)
    fig.axis('off')
    fig.imshow(image_label_overlay)
    print ("%s number of segments: %d" % (label, len(np.unique(segments))))
Ejemplo n.º 23
0
def showPlots(im_name, image, numSuperpixels, superpixels):
    # show sample test mean image
    fig = plt.figure("Superpixels -- %d im_sp" % (numSuperpixels))
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(image, superpixels))
    #bugged
    #ax.imsave("output/000%d.png" % (im_name),mark_boundaries(image, superpixels))
    plt.axis("off")
    plt.show()
Ejemplo n.º 24
0
def visualize_segments():
    pascal = PascalSegmentation()
    train_files = pascal.get_split()

    for image_file in train_files:
        print(image_file)
        image = pascal.get_image(image_file)
        segments, superpixels = superpixels_segments(image_file)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        new_regions, correspondences = merge_small_sp(image, superpixels)
        clean_regions = morphological_clean_sp(image, new_regions, 4)
        marked = mark_boundaries(image, clean_regions)
        edges = create_segment_sp_graph(segments, clean_regions)
        edges = np.array(edges)
        n_segments = segments.shape[2]
        segment_centers = [regionprops(segments.astype(np.int)[:, :, i],
                                       ['Centroid'])[0]['Centroid'] for i in
                           range(n_segments)]
        segment_centers = np.vstack(segment_centers)[:, ::-1]
        superpixel_centers = get_superpixel_centers(clean_regions)
        grr = min(n_segments, 10)
        fig, axes = plt.subplots(3, grr // 3, figsize=(30, 30))

        for i, ax in enumerate(axes.ravel()):
            ax.imshow(mark_boundaries(marked, segments[:, :, i], (1, 0, 0)))
            ax.scatter(segment_centers[:, 0], segment_centers[:, 1],
                       color='red')
            ax.scatter(superpixel_centers[:, 0], superpixel_centers[:, 1],
                       color='blue')
            this_edges = edges[edges[:, 1] == i]
            for edge in this_edges:
                ax.plot([superpixel_centers[edge[0]][0],
                         segment_centers[edge[1]][0]],
                        [superpixel_centers[edge[0]][1],
                         segment_centers[edge[1]][1]], c='black')
            ax.set_xlim(0, superpixels.shape[1])
            ax.set_ylim(superpixels.shape[0], 0)
            ax.set_xticks(())
            ax.set_yticks(())
        #plt.show()
        #imsave("segments_test/%s.png" % image_file, marked)
        plt.savefig("segments_test/%s.png" % image_file)
        plt.close()
Ejemplo n.º 25
0
def visualise_mask(image, mask):
    output = skseg.mark_boundaries(image, mask)
    rng = output.max()
    alpha = 0.75
    mask_color = [0.5, 0, 0]
    for c_idx in range(output.shape[2]):
        output[..., c_idx] = np.where(mask != 0,
            output[..., c_idx],
            alpha*mask_color[c_idx] + (1.0-alpha)*output[..., c_idx])
    return output
Ejemplo n.º 26
0
 def partitioning(self):
     self.clean_colored_pixel()
     self.superpixel_dbscan(12)
     
     #figure pour montrer l'effet du clustering
     self.fig_cluster = plt.figure('segmentation et clustering')
     #on y lie la fonction qui permet de faire le coloriage
     self.cid2 = self.fig_cluster.canvas.mpl_connect('button_press_event', self.onmouseclicked)
     
     self.affichage(mark_boundaries(self.img,self.clusterized))
Ejemplo n.º 27
0
def slicSegmentation(image, num_segments=600, sigma=5):
    C_8U = rgb(image)
    C_32F = to32F(C_8U)
    segments = slic(C_32F, n_segments=num_segments, sigma=sigma)

    fig = plt.figure("Superpixels -- %d segments" % (num_segments))
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(C_32F, segments))
    plt.axis("off")
    plt.show()
Ejemplo n.º 28
0
    def overlay_mask_base_image(self):
        """ Creates a new image with an overlay of the mask
        over the base image"""

        x0, y0, x1, y1 = self.clip

        self.base_w_mask = mark_boundaries(self.base_image[x0:x1, y0:y1],
                                           img_as_uint(self.mask),
                                           color=(1, 0, 1),
                                           outline_color=None)
Ejemplo n.º 29
0
def plotLabel(img, labeledImgT, outStr=''):
	fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'}, 
							figsize=(14, 6))
	ax[0].imshow(mark_boundaries(img, labeledImgT))
	ax[1].imshow(labeledImgT, cmap=cm.cubehelix)
	ax[0].set_title("Classification based on training set of multiple images")
	for a in ax:
		a.set_xticks(())
		a.set_yticks(())
	plt.savefig('./labelledImage'+outStr+'.png')
Ejemplo n.º 30
0
    def overlay_mask_optional_image(self):
        """Creates a new image with an overlay of the mask over the fluor
        image"""

        optional_image = color.rgb2gray(self.optional_image)
        optional_image = exposure.rescale_intensity(optional_image)
        optional_image = img_as_float(optional_image)

        self.optional_w_mask = mark_boundaries(optional_image, img_as_uint(
            self.mask), color=(1, 0, 1), outline_color=None)
        print(img_path)
        img = imread(img_path)
        path, img_name = os.path.split(img_path)

        # segments_EM = EM_method(img_path=img_path, sp_met='felzenszwalb', num_cuts=3, EM_iter=4, K=100)
        segments_EM = EM_method(img_path=img_path,
                                sp_met='felzenszwalb',
                                num_cuts=3,
                                EM_iter=4,
                                K=100,
                                dist_hist=True)

        # save segments label & boundry
        cv2.imwrite(join(label_dir, img_name), np.uint64(segments_EM))

        boundary = segmentation.mark_boundaries(img, segments_EM, (1, 0, 0))
        imsave(join(boundary_dir, img_name), boundary)

        # save segments label & boundry
        #         cv2.imwrite(join(label_dir, img_name), np.uint64(segments))

        #         boundary = segmentation.mark_boundaries(img, segments, (1, 0, 0))
        #         imsave(join(boundary_dir, img_name), boundary)

        if show_segmentation:
            plt.figure()
            plt.imshow(boundary)
            plt.show()
    if (num > 20):
        break
Ejemplo n.º 32
0
    def write_predictions(self, output):
        output_dir = output + '.offset'
        os.makedirs(output_dir, exist_ok=True)

        LOG.info('\nWriting offset predictions to {}'.format(output_dir))
        for key, value in self.offset_vis.items():
            if value is None:
                continue
            filename = os.path.join(output_dir, str(key) + '.png')
            image = value.astype(np.uint8)

            for bbox in self.bbox_vis[key]:
                pt1 = (int(bbox[0]), int(bbox[1]))
                pt2 = (int(bbox[0]) + int(bbox[2]),
                       int(bbox[1]) + int(bbox[3]))

                cv2.rectangle(image, pt1, pt2, (0, 0, 255))

            pred = Image.fromarray(image)

            pred.save(filename)

        output_dir = output + '.human'
        os.makedirs(output_dir, exist_ok=True)
        LOG.info('\nWriting human segmentation predictions to {}'.format(
            output_dir))
        for key, value in self.human_vis.items():
            if value is None:
                continue
            filename = os.path.join(output_dir, str(key) + '.png')
            image = value.astype(np.uint8)

            pred = Image.fromarray(image)
            pred.putpalette(palette)
            pred.save(filename)

        ins_output_dir = output + '.instance'
        os.makedirs(ins_output_dir, exist_ok=True)
        LOG.info('\nWriting instance parsing predictions to {}'.format(
            ins_output_dir))
        for key, value in self.instance_vis.items():
            if value is None:
                continue
            filename = os.path.join(ins_output_dir, str(key) + '.png')
            image = value.astype(np.uint8)

            pred = Image.fromarray(image)
            pred.putpalette(palette)
            pred.save(filename)

            # save confidence
            conf_file = open(os.path.join(ins_output_dir,
                                          str(key) + '.txt'), 'w')
            confs = self.confs_vis[key]
            for conf in confs:
                conf_file.write('{} {}\n'.format(conf[0], conf[1]))

        gt_ins_output_dir = output + '.instance.gt'
        os.makedirs(gt_ins_output_dir, exist_ok=True)
        LOG.info('\nWriting instance parsing predictions to {}'.format(
            gt_ins_output_dir))
        for key, value in self.gt_instance_vis.items():
            if value is None:
                continue
            filename = os.path.join(gt_ins_output_dir, str(key) + '.png')
            image = value.astype(np.uint8)

            pred = Image.fromarray(image)
            pred.putpalette(palette)
            pred.save(filename)

            # save confidence
            conf_file = open(
                os.path.join(gt_ins_output_dir,
                             str(key) + '.txt'), 'w')
            confs = self.gt_confs_vis[key]
            for conf in confs:
                conf_file.write('{} {} {}\n'.format(conf[0], conf[1], conf[2]))

        output_dir = output + '.superpixel'
        os.makedirs(output_dir, exist_ok=True)
        LOG.info('\nWriting superpixel predictions to {}'.format(output_dir))
        for key, value in self.superpixel_vis.items():
            if value is None:
                continue
            filename = os.path.join(output_dir, str(key) + '.png')

            pred = mark_boundaries(value[0][:, :, ::-1].astype(np.float),
                                   value[1])

            cv2.imwrite(filename, pred)

        self.ins_output_dir = ins_output_dir
        self.gt_ins_output_dir = gt_ins_output_dir
Ejemplo n.º 33
0
def explain_ddsm():
    print("Entering the explain ddsm ..... ")
    global v1model
    global v2model
    filestr = request.files['file'].read()
    model_ver = request.form['model_ver']
    print("Explain ddsm")
    if (model_ver == 'v1' and v1model is None):
        v1model = build_model(model_ver)
    elif (model_ver == 'v2' and v2model is None):
        v2model = build_model(model_ver)

    img = decodeImage(filestr)
    h, w, c = img.shape
    print("----------------------", img)
    image_array = enhance_images(img)
    image_array = image_array / 255.
    image_array = resize(image_array, (224, 224, 3))
    v = np.expand_dims(image_array, axis=0)
    imagenet_mean = np.array([0.449])
    imagenet_std = np.array([0.226])
    batch_x_non = (v - imagenet_mean) / imagenet_std
    print(batch_x_non.shape)
    explainer = lime_image.LimeImageExplainer(
        feature_selection='lasso_path'
    )  #object to explain predictions on Image data.
    if (model_ver == 'v1'):
        model = v1model
    else:
        model = v2model
    explanation_non_covid = explainer.explain_instance(batch_x_non[0],
                                                       model.predict,
                                                       top_labels=5,
                                                       hide_color=0,
                                                       num_samples=400)
    print('Explaination is obtained!')
    temp_non_1, mask_non_1 = explanation_non_covid.get_image_and_mask(
        explanation_non_covid.top_labels[0],
        positive_only=True,
        num_features=10,
        hide_rest=True)
    temp_non_2, mask_non_2 = explanation_non_covid.get_image_and_mask(
        explanation_non_covid.top_labels[0],
        positive_only=False,
        num_features=10,
        hide_rest=False)
    print("the prediction for non covid", explanation_non_covid.top_labels[0])
    class_names = [
        'Atelectasis', 'Cardiomegaly', 'Effusion', 'Infiltration', 'Mass',
        'Nodule', 'Pneumonia', 'Pneumothorax', 'Consolidation', 'Edema',
        'Emphysema', 'Fibrosis', 'Pleural_Thickening', 'Hernia', 'Covid'
    ]

    print("Predicted class: ",
          class_names[explanation_non_covid.top_labels[0]])
    exp_image = cv2.cvtColor(
        np.asarray(
            mark_boundaries(
                ((temp_non_2 * imagenet_std) + imagenet_mean) * 255,
                mask_non_2), 'uint8'), cv2.COLOR_BGR2RGB)

    img = cv2.resize(img, (w, h))
    img = Image.fromarray(exp_image.astype("uint8"))

    rawBytes = io.BytesIO()
    img.save(rawBytes, "JPEG")
    rawBytes.seek(0)
    img_base64 = base64.b64encode(rawBytes.read())
    return jsonify({'status': str(img_base64)})
Ejemplo n.º 34
0
sp = SP(img, step, nc)
sp.__init__(img, step, nc)
#print(r)
sp.generateSuperPixels()
r = sp.createConnectivity()
#cv2.imshow("cluster", slic.clusters.astype(np.uint8))
#slic.displayContours()

sp.displayContours((252, 0, 0))

#sp.getSPHCsegments(contours,img)

#cv2.imshow("superpixels", img)
SPHCsegm_grid = getSPHCsegments(r, imlab)

plt.imshow(img)
plt.show()
fig = plt.figure("%d Segments Merged" % 400)
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(imge, SPHCsegm_grid))
plt.axis("off")
plt.show()

#cv2.imshow("color",color)
#cv2.waitKey(0)
#cv2.imshow("SLICimg.jpg", slic.img)
#plt.imshow(SPHCsegm_grid)
#plt.show()

#cv2.waitKey(0)
Ejemplo n.º 35
0
for filename in file_list:
# load the image and convert it to a floating point data type
	image = img_as_float(io.imread(os.path.join(folder,filename)))
	 
	# loop over the number of segments
	numSegments = [100, 200, 300]
		# apply SLIC and extract (approximately) the supplied number
		# of segments
	segments = slic(image, n_segments = numSegments[1], sigma = 5)
	for (i, segVal) in enumerate(np.unique(segments)):
		mask = np.zeros(image.shape[:2], dtype = "uint8")
		mask[segments == segVal] = 255
		_, contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
		#pts = np.where((segments==segVal))
		for pt in kp:

		if cv2.pointPolygonTest(contours[0], pt, False):
			fg.append(segVal)





	# show the output of SLIC
	#fig = plt.figure("Superpixels -- %d segments" % (numSegments))
	#ax = fig.add_subplot(1, 1, 1)
	cv2.imshow('re',mark_boundaries(image, segments))
	#plt.axis("off")
	cv2.waitKey(0)	 
	# show the plots
	#plt.show()
Ejemplo n.º 36
0
def checkSlic():

    data = loadData()
    # Error List
    error = []

    for idx in range(159, 160):

        # Display
        print('Processing Image #', idx + 1, sep='')
        # Load Image and Ground Truth Image as Float
        img = img_as_float(io.imread('data/images/' + str(idx + 1) + '.png'))
        img_gt = img_as_float(
            io.imread('data/gt_images/' + str(idx + 1) + '.png'))

        # Perform SLIC
        img_seg = slic(img,
                       n_segments=1000,
                       compactness=8,
                       convert2lab=True,
                       min_size_factor=0.3)

        img_sample = np.zeros(img_gt.shape[:-1])

        img_data = data[np.where(data[:, 65] == (idx + 1))]

        big = np.amax(img_seg)
        roi = (img_seg == big)
        roi_idx = np.nonzero(roi)
        biglabel = img_gt[int(np.mean(roi_idx[0])),
                          int(np.mean(roi_idx[1])), 2]

        for row in range(img_gt.shape[0]):
            for col in range(img_gt.shape[1]):
                label = img_seg[row, col]
                if label == big:
                    img_sample[row, col] == biglabel
                elif img_data[label][64] == 1:
                    img_sample[row, col] = 1

        curerror = sum(sum(
            img_sample != img_gt[:, :, 2])) / img_gt.shape[0] / img_gt.shape[1]
        error.append(curerror)

        plt.figure(1)
        fig1 = plt.imshow(mark_boundaries(img, img_seg))
        fig1.axes.get_xaxis().set_visible(False)
        fig1.axes.get_yaxis().set_visible(False)
        plt.figure(2)
        fig2 = plt.imshow(mark_boundaries(img_gt, img_seg))
        fig2.axes.get_xaxis().set_visible(False)
        fig2.axes.get_yaxis().set_visible(False)
        plt.figure(3)
        fig2 = plt.imshow(img)
        fig2.axes.get_xaxis().set_visible(False)
        fig2.axes.get_yaxis().set_visible(False)
        plt.figure(4)
        fig2 = plt.imshow(img_gt)
        fig2.axes.get_xaxis().set_visible(False)
        fig2.axes.get_yaxis().set_visible(False)
        plt.figure(5)
        fig2 = plt.imshow(img_sample)
        fig2.axes.get_xaxis().set_visible(False)
        fig2.axes.get_yaxis().set_visible(False)
    return error
Ejemplo n.º 37
0
    fname = 'images/' + name + '_image.npy'
    image = np.load(fname)
    fname = 'images/' + name + '_seg.npy'
    segments = np.load(fname)

    return image, segments


def save_results(name, labelled_image):
    np.save('labelled/' + name + '_labels', labelled_image)


if __name__ == "__main__":
    # Load image to label
    name = 'DJI_0820'
    image, segments = load_image_segments(name)
    labelled_image = np.full(image[:, :, 0].shape, np.nan)

    # Set up plot
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    base_im = ax.imshow(
        mark_boundaries(image, segments, color=(1, 0, 0), mode='thick'))
    palette = copy(plt.cm.viridis)
    palette.set_bad(alpha=0.0)
    im = ax.imshow(labelled_image, alpha=0.5, vmin=1, vmax=2, cmap=palette)
    plt.show(block=False)

    # Label image, updating plot until user quits
    label_image()
Ejemplo n.º 38
0
        #get data from bars
        numSegments = cv2.getTrackbarPos('Number of Segments', 'slic')
        numSigma = cv2.getTrackbarPos('Sigma', 'slic')

        #segment
        start_time = time.time()  #  save curr time to report duration
        segments = slic(img.astype(np.float64) / 256,
                        n_segments=numSegments,
                        sigma=numSigma)
        duration = time.time() - start_time  # calculate time elapsed
        print("slic elapsed: ", duration)

        #mark segments in image
        start_time = time.time()  #  save curr time to report duration
        img = mark_boundaries(img, segments)
        duration = time.time() - start_time  # calculate time elapsed
        print("mark boundaries elapsed: ", duration)

        #make a mask
        mask = np.zeros(img.shape)
        mask = mark_boundaries(mask, segments)

        if display_mode == 0:
            cv2.imshow('slic', img)
        else:
            cv2.imshow('slic', mask)

        ch = cv2.waitKey(1)
        if ch == 27:
            break
Ejemplo n.º 39
0
    compact = [15, 25, 35, 45, 25, 25, 25, 25, 25, 25, 25, 25]
    segment = [250, 250, 250, 250, 50, 150, 250, 350, 250, 250, 250, 250]
    thresh = [45, 45, 45, 45, 45, 45, 45, 45, 25, 35, 45, 55]
    for j in range(12):
    # Region Adjacency Graph
        labels = segmentation.slic(img, compactness=compact[j], n_segments=segment[j], start_label=1)
        g = graph.rag_mean_color(img, labels)
        labels2 = graph.merge_hierarchical(labels, g, thresh=thresh[j], rag_copy=False,
                                           in_place_merge=True,
                                           merge_func=merge_mean_color,
                                           weight_func=_weight_mean_color)
        for ii in range(np.max(labels2)):
            if np.logical_or(np.sum(labels2 == ii) > 1000, np.sum(labels2 == ii) < 150):
                labels2[labels2 == ii] = 0
        out = color.label2rgb(labels2, img, kind='avg', bg_label=0)
        out = segmentation.mark_boundaries(out, labels2, (0, 0, 0))
        out = np.uint8(out * 255)

        centers = []
        for ii in range(np.max(labels2) - 1):
            xInd, yInd = np.where(labels2 == ii + 1)
            if len(xInd) == 0:
                continue
            xCenter = int(np.mean(xInd))
            yCenter = int(np.mean(yInd))
            w = max(xInd) - min(xInd)
            h = max(yInd) - min(yInd)

            # New filters, based on sizes
            # if w < 20 or w > 40:
            #     continue
def display_superpixels(rgb, superpixels):
    fig = plt.figure("SuperPixels")
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(rgb, superpixels))
    plt.axis("off")
    plt.show()
im_val = []
im_val = 0.299 * im[:, :, 0] + 0.587 * im[:, :, 1] + 0.114 * im[:, :, 2]
im_val_temp = im_val
im_val = np.array(im_val).flatten()

#每个点的像素值(一维)
im_val = []
im_val = 0.299 * im[:, :, 0] + 0.587 * im[:, :, 1] + 0.114 * im[:, :, 2]
im_val = np.array(im_val).flatten()

# flez
labels = segmentation.felzenszwalb(im,
                                   scale=args.scale,
                                   sigma=0.8,
                                   min_size=args.min_size)
boundary = segmentation.mark_boundaries(im, labels, (1, 0, 0))
# imsave('boundary_%d.png'%args.num_superpixels, boundary)
labels_temp = labels
# ave_position,red_average,green_average,blue_average,position = function.init_sp(im,labels)
labels = labels.reshape(im.shape[0] * im.shape[1])  #分割后每个超像素的Sk值
u_labels = np.unique(labels)  #将Sk作为标签
l_inds = []  #每i行表示Si超像素中每个像素的编号
for i in range(len(u_labels)):
    l_inds.append(np.where(labels == u_labels[i])[0])

mean_list = []
var_list = []
std_list = []
std_m_v = []
for i in range(len(l_inds)):
    labels_per_sp = im_val[l_inds[i]]
Ejemplo n.º 42
0
'''

# load model
model = resnets_shift.resnet18(True).cuda()
optimizer = optimizers.optimfn(args.optim, model)  # unused
model, _, _ = networktools.continue_train(model, optimizer,
                                          args.eval_model_pth, True)
model.eval()

# generate dataset from points
iterator_val = GenerateIterator_eval(metadata)
# pass through dataset
pred_mask = np.zeros_like(labels)

with torch.no_grad():
    for batch_it, (images, tile_ids) in enumerate(iterator_val):
        images = images.cuda()
        pred_ensemble = model(images)
        pred_ensemble = torch.argmax(pred_ensemble, 1).cpu().numpy()
        for tj, tile_id in enumerate(tile_ids.numpy()):
            pred_mask[metadata[tile_id]
                      ['foreground_indices']] = pred_ensemble[tj]

pred_mask_rgb = np.eye(4)[pred_mask][..., 1:]
pred_mask_rgb = Image.fromarray(pred_mask_rgb.astype(np.uint8) * 255)
pred_mask_rgb = pred_mask_rgb.resize((x // us, y // us))
pred_mask_rgb.save('slic_out_mask.png')

slic_out = mark_boundaries(image, labels, color=(0, 0, 0))
Image.fromarray((255 * slic_out).astype(np.uint8)).save('slic_out.png')
Ejemplo n.º 43
0
            yp = patch_coords[1][j]
            patch_pixels.append(image[xp, yp, 1])
        all_patch_pixels.append(patch_pixels)

    bins = 30

    features = []

    for i in range(0, len(all_patch_pixels)):
        h_inter = np.histogram(all_patch_pixels[i], bins)
        features.append(h_inter[1].tolist())

    fake_class = (np.array(zerolistmaker(len(features))) * [2]).tolist()
    p_label, accuracy, p_val = svm_predict(fake_class, features, model)

    # p_lane_labels=np.where(np.asarray(p_label)==1)
    p_lane_labels = np.where(np.asarray(p_val) > p_threshold)
    p_lane_labels = p_lane_labels[0].tolist()

    lane_segments = segments * 0

    for i in range(0, len(p_lane_labels)):
        x, y = (np.where(segments == p_lane_labels[i]))
        lane_segments[x, y] = i + 1

    fig = plt.figure("Superpixels")
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(image, lane_segments))
    plt.axis("off")
    plt.show()
Ejemplo n.º 44
0
    centers, colors_hists, segments, neighbors = superpixels_histograms_neighbors(
        img)
    fg_segments, bg_segments = find_superpixels_under_marking(
        img_marking, segments)

    # get cumulative BG/FG histograms, before normalization
    fg_cumulative_hist = cumulative_histogram_for_superpixels(
        fg_segments, colors_hists)
    bg_cumulative_hist = cumulative_histogram_for_superpixels(
        bg_segments, colors_hists)

    norm_hists = normalize_histograms(colors_hists)

    graph_cut = do_graph_cut((fg_cumulative_hist, bg_cumulative_hist),
                             (fg_segments, bg_segments), norm_hists, neighbors)

    plt.subplot(1, 2, 2), plt.xticks([]), plt.yticks([])
    plt.title('segmentation')
    segmask = pixels_for_segment_selection(segments, np.nonzero(graph_cut))
    cv2.imwrite("img/output_segmentation.png", np.uint8(segmask * 255))
    plt.imshow(segmask)

    plt.subplot(1, 2, 1), plt.xticks([]), plt.yticks([])
    img = mark_boundaries(img, segments)
    img[img_marking[:, :, 0] != 255] = (1, 0, 0)
    img[img_marking[:, :, 2] != 255] = (0, 0, 1)
    plt.imshow(img)
    plt.title("SLIC + markings")
    plt.savefig("img/segmentation.png", bbox_inches='tight', dpi=96)
Ejemplo n.º 45
0
Segmentation contours
=====================

Visualize segmentation contours on original grayscale image.
"""

from skimage import data, segmentation
from skimage import filters
import matplotlib.pyplot as plt
import numpy as np

coins = data.coins()
mask = coins > filters.threshold_otsu(coins)
clean_border = segmentation.clear_border(mask).astype(np.int)

coins_edges = segmentation.mark_boundaries(coins, clean_border)

plt.figure(figsize=(8, 3.5))
plt.subplot(121)
plt.imshow(clean_border, cmap='gray')
plt.axis('off')
plt.subplot(122)
plt.imshow(coins_edges)
plt.axis('off')

plt.tight_layout()
plt.show()

import os
print(os.getcwd())
    def segment(self):

        n = array.array('i')
        s = array.array('f')
        #n.append(466)
        #n.append(500)
        n.append(100)
        n.append(600)
        n.append(566)
        n.append(600)
        n.append(666)

        if pixels < 1000000:
            s.append(1.7)
            s.append(2.7)
            s.append(3.3)
            s.append(3.9)
            s.append(4.9)

        elif pixels < 7000000:

            s.append(2.275 - 1.5 + 0.0000009166667 * pixels +
                     0.0000000000001083333 * pow(pixels, 2))
            s.append(2.275 - 1 + 0.0000009166667 * pixels +
                     0.0000000000001083333 * pow(pixels, 2))
            s.append(2.275 + 0.0000009166667 * pixels +
                     0.0000000000001083333 * pow(pixels, 2))
            s.append(2.275 + 1 + 0.0000009166667 * pixels +
                     0.0000000000001083333 * pow(pixels, 2))
            s.append(2.275 + 1.5 + 0.0000009166667 * pixels +
                     0.0000000000001083333 * pow(pixels, 2))

        elif pixels > 7000000:
            #s.append(10)
            #s.append(13)
            s.append(18)
            s.append(8)
            s.append(14)
            s.append(15)
            s.append(18)

        self.imArray = []
        for i in range(0, 5):  # runs 3 times
            #numSegments = 566
            #mySigma = 6

            ts = time.time()
            st = datetime.datetime.fromtimestamp(ts).strftime(
                '%Y-%m-%d_%H-%M-%S')
            #self.out_files.append( str(n[i])+'seg_sigma'+str(s[i])+"_datetime"+st )
            self.out_files.append(str(n[i]) + "_datetime" + st)

            newIm = self.im
            #cv2.imwrite("/home/madison/Documents/41x/IMG_SET6/" + self.out_files[i] +"_origin_elaine.jpg", newIm)

            # apply SLIC and extract (approximately) the supplied number of segments
            segments = slic(self.im, n_segments=n[i], sigma=s[i])

            b = segments.tolist()  # nested lists with same data, indices

            #with open('/home/madison/Documents/41x/IMG_SET6/' + self.out_files[i] +'.json', 'w') as outfile:
            #	    json.dump(b, outfile, indent=2)

            #self.segm_lists.append(json.dumps(b, indent=2))
            self.segm_lists.append(b)

            # show the output of SLICs
            fig = plt.figure("Superpixels -- %d segments" % (n[i]))
            ax = fig.add_subplot(1, 1, 1)
            newIm = mark_boundaries(
                self.im, segments,
                color=(52, 205,
                       195))  # fn normalises img bw 1 and 0 apparently
            #ax.imshow(self.im)
            plt.axis("off")
            #cv2.waitKey(0)

            newIm = (newIm * 255.0).astype('u1')
            self.imArray.append(newIm)
Ejemplo n.º 47
0
def precision_recall():

    im1 = cv2.imread("data/2-0.jpg", cv2.IMREAD_COLOR)
    im2 = cv2.imread("data/2-15.jpg", cv2.IMREAD_COLOR)
    im3 = cv2.imread("data/2-30.jpg", cv2.IMREAD_COLOR)
    im4 = cv2.imread("data/2-45.jpg", cv2.IMREAD_COLOR)
    gt = cv2.imread("gt2.jpg", 0)

    # small image for test only
    ##	im1 = cv2.imread("0.bmp", cv2.IMREAD_COLOR)
    ##	im2 = cv2.imread("15.bmp", cv2.IMREAD_COLOR)
    ##	im3 = cv2.imread("30.bmp", cv2.IMREAD_COLOR)
    ##	im4 = cv2.imread("45.bmp", cv2.IMREAD_COLOR)
    ##	gt = cv2.imread("gt2.jpg", 0)

    thres = 3  # threshold of distance between edge pixel and g.t. edge pixel
    # Multi-channel SLIC, our method
    # print ("begin multi-channel slic")
    # img = [im1, im2, im3, im4]
    # L = mslic.mslic(img, 300, 5, 1, 5)
    # L = regions.distinct_label(L)
    # print ("after multi-channel slic, there are " + str(np.max(L)) + " superpixels")
    # L = clusters.merge_tiny_regions(im1, L, 200)
    # print ("after merging tiny regions, there are " + str(np.max(L)) + " superpixels left")
    # bond_L = segmentation.find_boundaries(L)
    # cv2.imwrite("after mslic.jpg", bond_L*255)

    # print (performance.boundary_recall(gt, bond_L, thres))
    # print (performance.precision(gt, bond_L, thres))

    # L = clusters.merge_regions(img, L, gt, iteration = 100)

    # cv2.imwrite("it=309.jpg", contour * 255)
    # print (performance.boundary_recall(gt, bond_L, thres))
    # print (performance.precision(gt, bond_L, thres))

    # QuickShift method, ECCV 2008
    ##	print "begin quickshift"
    ##	ratio = 1.0
    ##	kernel_size = 10
    ##	max_dist = 5
    ##	return_tree = False
    ##	sigma = 3
    ##	convert2lab = True
    ##	random_seed = 42
    ##	L_qs = segmentation.quickshift(max_im, ratio, kernel_size, max_dist, return_tree, sigma, convert2lab, random_seed)
    ##	print "after Quickshift, there are " + str(np.max(L_qs)) + " superpixels"
    ##	L = clusters.merge_tiny_regions(im1, L_qs, 200)
    ##	print "after merging tiny regions, there are " + str(np.max(L)) + " superpixels left"
    ##	bond_qs = segmentation.find_boundaries(L)
    ##	print performance.boundary_recall(gt, bond_qs, thres)
    ##	print performance.precision(gt, bond_qs, thres)
    ##	img = [im1, im2, im3, im4]
    ##	L = clusters.merge_regions(img, L, gt, iteration = 300)
    ##	bond_L = segmentation.find_boundaries(L, mode='thick')
    ##	print performance.boundary_recall(gt, bond_L, thres)
    ##	print performance.precision(gt, bond_L, thres)

    # Efficient graph-based image segmentation, IJCV 2004
    # print "begin Graph-based segmentation"
    # scale = 80
    # sigma = 2
    # min_size = 5
    # L_fh = segmentation.felzenszwalb(max_im, scale, sigma, min_size)
    # L = regions.distinct_label(L_fh)
    # print "after Graph-based segmentation, there are " + str(np.max(L)) + " superpixels"
    # contour = segmentation.mark_boundaries(max_im, L, (0, 0, 1))
    # cv2.imwrite("contour_fh.jpg", contour*255)
    # cv2.waitKey(0)

    ##	L = clusters.merge_tiny_regions(im1, L, 200)
    ##	print "after merging tiny regions, there are " + str(np.max(L)) + " superpixels left"
    ##	bond_fh = segmentation.find_boundaries(L)
    ##	print performance.boundary_recall(gt, bond_fh, thres)
    ##	print performance.precision(gt, bond_fh, thres)
    ##	img = [im1, im2, im3, im4]
    ##	L = clusters.merge_regions(img, L, gt, iteration = 200)
    ##	bond_L = segmentation.find_boundaries(L, mode='thick')
    ##	print performance.boundary_recall(gt, bond_L, thres)
    ##	print performance.precision(gt, bond_L, thres)

    # SLIC superpixel method, TPAMI 2012
    print("begin slic")
    im = Image("data/2-0.jpg")
    ns = 300
    comp = 20
    L_slic = segmentation.slic(im1,
                               n_segments=ns,
                               compactness=comp,
                               sigma=3,
                               convert2lab=True)
    L = regions.distinct_label(L_slic)
    L = clusters.merge_tiny_regions(im1, L, 500)
    print("after merging tiny regions, there are " + str(np.max(L)) +
          " superpixels left")
    contour = segmentation.mark_boundaries(im1, L, (0, 0, 1))
    # cv2.imshow("hehe", contour)
    # cv2.waitKey(0)

    print("start dbscan..............")
    L = dbscan.merge_region(im, L, [14, 1])
    print("after dbscan, there are " + str(np.max(L)) + " superpixels left")
    contour = segmentation.mark_boundaries(im1, L, (0, 0, 1))
    cv2.imshow("hehe", contour)
    cv2.imwrite("feldspar_dbscan.jpg", contour * 255)
    cv2.waitKey(0)
Ejemplo n.º 48
0
# Display images.
cv2.imwrite('grey.jpg',thresh)
# cv2.imshow("Floodfilled Image", im_out)
# cv2.waitKey(0)


# load the image and apply SLIC and extract (approximately)
# the supplied number of segments
image = cv2.imread('grey.jpg')
segments = slic(img_as_float(image), n_segments = 500, compactness=10,sigma = 5)

# show the output of SLIC
fig = plt.figure("Superpixels")
ax = fig.add_subplot(1, 1, 1)
ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments))
plt.axis("off")
plt.show()

counter_red = 0
lower_blue = np.array([110, 50, 50])

# loop over the unique segment values
for (i, segVal) in enumerate(np.unique(segments)):
	# construct a mask for the segment
	mask = np.zeros(image.shape[:2], dtype = "uint8")
	mask[segments == segVal] = 125

        print i

        image_mask=cv2.bitwise_or(image, image, mask = mask)
def crazy_visual():
    dataset = NYUSegmentation()
    # load training data
    data = load_nyu(n_sp=500)
    data = add_edges(data)

    for x, image_name, superpixels, y in zip(data.X, data.file_names,
                                             data.superpixels, data.Y):
        print(image_name)
        if int(image_name) != 11:
            continue
        image = dataset.get_image(image_name)
        plt.figure(figsize=(20, 20))
        bounary_image = mark_boundaries(image, superpixels)
        plt.imshow(bounary_image)
        gridx, gridy = np.mgrid[:superpixels.shape[0], :superpixels.shape[1]]

        edges = x[1]
        points_normals = dataset.get_pointcloud_normals(image_name)
        centers2d = get_superpixel_centers(superpixels)
        centers3d = [
            np.bincount(superpixels.ravel(), weights=c.ravel())
            for c in points_normals[:, :, :3].reshape(-1, 3).T
        ]
        centers3d = (np.vstack(centers3d) / np.bincount(superpixels.ravel())).T
        sp_normals = get_sp_normals(points_normals[:, :, 3:], superpixels)
        offset = centers3d[edges[:, 0]] - centers3d[edges[:, 1]]
        offset = offset / np.sqrt(np.sum(offset**2, axis=1))[:, np.newaxis]
        #mean_normal = (sp_normals[edges[:, 0]] + sp_normals[edges[:, 1]]) / 2.
        mean_normal = sp_normals[edges[:, 0]]
        #edge_features = np.arccos(np.abs((offset * mean_normal).sum(axis=1))) * 2. / np.pi
        edge_features = 1 - np.abs((offset * mean_normal).sum(axis=1))
        no_normals = (np.all(sp_normals[edges[:, 0]] == 0, axis=1) +
                      np.all(sp_normals[edges[:, 1]] == 0, axis=1))
        edge_features[no_normals] = 0  # nan normals

        if True:
            coords = points_normals[:, :, :3].reshape(-1, 3)
            perm = np.random.permutation(superpixels.max() + 1)
            mv.points3d(coords[:, 0],
                        coords[:, 1],
                        coords[:, 2],
                        perm[superpixels.ravel()],
                        mode='point')
            #mv.points3d(centers3d[:, 0], centers3d[:, 1], centers3d[:, 2], scale_factor=.04)
            mv.quiver3d(centers3d[:, 0], centers3d[:, 1], centers3d[:, 2],
                        sp_normals[:, 0], sp_normals[:, 1], sp_normals[:, 2])
            mv.show()
        from IPython.core.debugger import Tracer
        Tracer()()

        for i, edge in enumerate(edges):
            e0, e1 = edge
            #color = (dataset.colors[y[e0]] + dataset.colors[y[e1]]) / (2. * 255.)
            #f = edge_features[i]
            #if f < 0:
            #e0, e1 = e1, e0
            #f = -f

            #plt.arrow(centers[e0][0], centers[e0][1],
            #centers[e1][0] - centers[e0][0], centers[e1][1] - centers[e0][1],
            #width=f * 5
            #)
            color = "black"
            plt.plot([centers2d[e0][0], centers2d[e1][0]],
                     [centers2d[e0][1], centers2d[e1][1]],
                     c=color,
                     linewidth=edge_features[i] * 5)
        plt.scatter(centers2d[:, 0], centers2d[:, 1], s=100)
        plt.tight_layout()
        plt.xlim(0, superpixels.shape[1])
        plt.ylim(superpixels.shape[0], 0)
        plt.axis("off")
        plt.savefig("figures/normal_relative/%s.png" % image_name,
                    bbox_inches="tight")
        plt.close()
Ejemplo n.º 50
0
def create_segmented_image(image, segments):
    segmented_image = mark_boundaries(img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)), segments)

    return segmented_image
    def interpret(self, data_, visualization=True, save_outdir=None):
        if self.normlime_weights is None:
            raise ValueError(
                "Not find the correct precomputed NormLIME result. \n"
                "\t Try to call compute_normlime_weights() first or load the correct path."
            )

        g_weights = self.preparation_normlime(data_)
        lime_weights = self._lime.lime_interpreter.local_weights

        if visualization or save_outdir is not None:
            import matplotlib.pyplot as plt
            from skimage.segmentation import mark_boundaries
            l = self.labels[0]
            ln = l
            if self.label_names is not None:
                ln = self.label_names[l]

            psize = 5
            nrows = 4
            weights_choices = [0.6, 0.7, 0.75, 0.8, 0.85]
            nums_to_show = []
            ncols = len(weights_choices)

            plt.close()
            f, axes = plt.subplots(nrows,
                                   ncols,
                                   figsize=(psize * ncols, psize * nrows))
            for ax in axes.ravel():
                ax.axis("off")

            axes = axes.ravel()
            axes[0].imshow(self.image)
            prob_str = "{%.3f}" % (self.predicted_probability)
            axes[0].set_title("label {}, proba: {}".format(ln, prob_str))

            axes[1].imshow(
                mark_boundaries(self.image,
                                self._lime.lime_interpreter.segments))
            axes[1].set_title("superpixel segmentation")

            # LIME visualization
            for i, w in enumerate(weights_choices):
                num_to_show = auto_choose_num_features_to_show(
                    self._lime.lime_interpreter, l, w)
                nums_to_show.append(num_to_show)
                temp, mask = self._lime.lime_interpreter.get_image_and_mask(
                    l,
                    positive_only=True,
                    hide_rest=False,
                    num_features=num_to_show)
                axes[ncols + i].imshow(mark_boundaries(temp, mask))
                axes[ncols + i].set_title(
                    "LIME: first {} superpixels".format(num_to_show))

            # NormLIME visualization
            self._lime.lime_interpreter.local_weights = g_weights
            for i, num_to_show in enumerate(nums_to_show):
                temp, mask = self._lime.lime_interpreter.get_image_and_mask(
                    l,
                    positive_only=True,
                    hide_rest=False,
                    num_features=num_to_show)
                axes[ncols * 2 + i].imshow(mark_boundaries(temp, mask))
                axes[ncols * 2 + i].set_title(
                    "NormLIME: first {} superpixels".format(num_to_show))

            # NormLIME*LIME visualization
            combined_weights = combine_normlime_and_lime(
                lime_weights, g_weights)

            self._lime.lime_interpreter.local_weights = combined_weights
            for i, num_to_show in enumerate(nums_to_show):
                temp, mask = self._lime.lime_interpreter.get_image_and_mask(
                    l,
                    positive_only=True,
                    hide_rest=False,
                    num_features=num_to_show)
                axes[ncols * 3 + i].imshow(mark_boundaries(temp, mask))
                axes[ncols * 3 + i].set_title(
                    "Combined: first {} superpixels".format(num_to_show))

            self._lime.lime_interpreter.local_weights = lime_weights

        if save_outdir is not None:
            save_fig(data_, save_outdir, 'normlime', self.num_samples)

        if visualization:
            plt.show()
    plt.subplot(1, 5, plot_index + 1)
    plt.imshow(X_eval[good_index])
    plt.title(' Predicted: {}, Actual: {} '.format(labels_index[cnn_pred[good_index][0]], 
                                                 labels_index[y_eval[good_index]]), fontsize = 18) 
plt.savefig('results/cnn/correctly_classified.png', dpi=400)
                                                 
# lime explanation of correctly classified images
plt.figure(figsize=(50,10))
#shuffle(correctly_classified_indices)
for plot_index, good_index in enumerate(correctly_classified_indices[0:5]):
    plt.subplot(1, 5, plot_index + 1)
    explainer = lime_image.LimeImageExplainer()
    explanation = explainer.explain_instance(X_eval[good_index], cnn_classifier.predict_classes, top_labels=2, hide_color=0, num_samples=1000)
    temp, mask = explanation.get_image_and_mask(0, positive_only=False, num_features=1000, hide_rest=False)
    #temp, mask = explanation.get_image_and_mask(0, positive_only=True, num_features=1000, hide_rest=True)
    x = mark_boundaries(temp / 2 + 0.5, mask)
    plt.imshow(x, interpolation='none')
    plt.title(' Predicted: {}, Actual: {} '.format(labels_index[cnn_pred[good_index][0]], 
                                                 labels_index[y_eval[good_index]]), fontsize = 18)
plt.savefig('results/cnn/lime_correctly_classified.png', dpi=400)
                                                 
# misclassified images
plt.figure(figsize=(50,10))
#shuffle(misclassified_indices)
for plot_index, bad_index in enumerate(misclassified_indices[0:5]):
    plt.subplot(1, 5, plot_index + 1)
    plt.imshow(X_eval[bad_index])
    plt.title(' Predicted: {}, Actual: {} '.format(labels_index[cnn_pred[bad_index][0]], 
                                                 labels_index[y_eval[bad_index]]), fontsize = 18)
plt.savefig('results/cnn/mis_classified.png', dpi=400)
                                                 
Ejemplo n.º 53
0
    for i in range(0, heightL):
        for j in range(0, widthL):
            imgL3D[i, j, k] = 255 - imgL[i, j]
            imgR3D[i, j, k] = 255 - imgR[i, j]

segL = felzenszwalb(imgL3D, scale=300, sigma=0.5, min_size=100)
segR = felzenszwalb(imgR3D, scale=300, sigma=0.5, min_size=100)

fig, ax = plt.subplots(2,
                       2,
                       figsize=(10, 10),
                       sharex=True,
                       sharey=True,
                       subplot_kw={'adjustable': 'box-forced'})

ax[0, 0].imshow(mark_boundaries(imgL3D, segL))
ax[0, 1].imshow(mark_boundaries(imgR3D, segR))

plt.tight_layout()
plt.show()

TCol = 10
TGrad = 2

#colour absolute difference


def Ctadc(x, y, d):
    c = min(abs(imgL[y, x] * 1.0 - imgR[y, x - d] * 1.0), TCol)
    return c
train_gen = make_image_gen(balanced_train_df)
train_x, train_y = next(train_gen)
print('x', train_x.shape, train_x.min(), train_x.max())
print('y', train_y.shape, train_y.min(), train_y.max())

# In[ ]:

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(30, 10))
batch_rgb = montage_rgb(train_x)
batch_seg = montage(train_y[:, :, :, 0])
ax1.imshow(batch_rgb)
ax1.set_title('Images')
ax2.imshow(batch_seg)
ax2.set_title('Segmentations')
ax3.imshow(mark_boundaries(batch_rgb, batch_seg.astype(int)))
ax3.set_title('Outlined Ships')
fig.savefig('overview.png')

# # Make the Validation Set

# In[ ]:

valid_x, valid_y = next(make_image_gen(valid_df, VALID_IMG_COUNT))
print(valid_x.shape, valid_y.shape)

# # Augment Data

# In[ ]:

from keras.preprocessing.image import ImageDataGenerator
    def classifier(self, req, zero_out):
        expected_items = []
        for i in req.expected_labels:
            expected_items.append(i.data)

        try:
            hd_image = self.cv_bridge.imgmsg_to_cv2(
                req.color, desired_encoding="passthrough")
        except CvBridgeError as e:
            rospy.logerr(e)

        os.environ["MXNET_CUDNN_AUTOTUNE_DEFAULT"] = "0"

        # t0 = datetime.datetime.now()
        seg_net = net.create_infer(CLASSNUM, WORKSPACE)
        mod = mx.module.Module(seg_net,
                               data_names=('data', ),
                               label_names=(),
                               context=self.ctx)
        # t1 = datetime.datetime.now()
        # rospy.logerr('seg_net creation took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        mod.bind(data_shapes=[("data", (1, 3, 640, 640))],
                 for_training=False,
                 grad_req='null')
        # t1 = datetime.datetime.now()
        # rospy.logerr('module bind took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        mod.init_params(arg_params=self.arg_dict,
                        aux_params=self.aux_dict,
                        allow_missing=True)
        # t1 = datetime.datetime.now()
        # rospy.logerr('module parameter init took %s' % (t1 - t0))

        rgb_mean = 128
        hd_image = hd_image.astype(np.float32)
        im = hd_image - rgb_mean
        im = np.swapaxes(im, 0, 2)
        im = np.swapaxes(im, 1, 2)
        im = np.expand_dims(im, axis=0)
        im, orig_size = misc.pad_image(im, 32)

        # t0 = datetime.datetime.now()
        mod.reshape([("data", im.shape)])
        # t1 = datetime.datetime.now()
        # rospy.logerr('module reshape took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        mod.bind(data_shapes=[("data", (1, 3, 640, 640))],
                 for_training=False,
                 grad_req='null')
        # t1 = datetime.datetime.now()
        # rospy.logerr('module bind took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        mod.forward(
            mx.io.DataBatch(data=[mx.nd.array(im)],
                            label=None,
                            pad=None,
                            index=None))
        # t1 = datetime.datetime.now()
        # rospy.logerr('module forward took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        pred = mod.get_outputs()[0].asnumpy().squeeze()
        # t1 = datetime.datetime.now()
        # rospy.logerr('module prediction took %s' % (t1 - t0))

        pred = misc.crop_pred(pred, orig_size)

        # t0 = datetime.datetime.now()
        if zero_out:
            for i in self.item_list[1:]:
                j = self.item_list.index(i)
                if i in expected_items or j < 1 or j >= CLASSNUM:
                    continue
                if j < pred.shape[0]:
                    pred[j] = np.zeros_like(pred[j])
                    continue

        pred_label = pred.argmax(0)

        # t1 = datetime.datetime.now()
        # rospy.logerr('bad prediction elimination took %s' % (t1 - t0))

        # s0 = datetime.datetime.now()
        best_scores_map, second_best_scores_map, diff_conf_map = misc.confidence_maps(
            pred)
        # s1 = datetime.datetime.now()
        # rospy.logerr('confidence map creation took %s' % (s1 - s0))

        mean_conf_map = np.zeros_like(diff_conf_map)
        ccs, num_ccs = measure.label(pred_label, return_num=True)
        for cc in range(1, num_ccs + 1):
            mean_conf = diff_conf_map[np.where(ccs == cc)].mean()
            mean_conf_map[np.where(ccs == cc)] = mean_conf
        mean_conf_map = (mean_conf_map * 255).astype(np.uint8)
        mean_conf_map[mean_conf_map == 0] = 1

        # t0 = datetime.datetime.now()
        binmask = np.zeros_like(pred_label)
        segment_certainties = []
        empty_img = np.zeros_like(mean_conf_map)
        conf_map = np.zeros_like(mean_conf_map)
        pred_label_unique = np.unique(pred_label)
        if zero_out:
            for c in pred_label_unique:
                # -3 for storage, tote, and because zero indexing
                if c < 1 or c > (CLASSNUM - 3):
                    continue

                binmask = pred_label == c
                cnts = cv2.findContours(binmask.astype(np.uint8),
                                        cv2.RETR_TREE,
                                        cv2.CHAIN_APPROX_SIMPLE)[1]
                if len(cnts) != 0:
                    max_cnt = -1
                    max_certainty = 0
                    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
                    empty_img = empty_img * 0
                    for cc in range(len(cnts)):
                        col = cc + 1
                        cv2.drawContours(image=empty_img,
                                         contours=cnts,
                                         contourIdx=cc,
                                         color=col,
                                         thickness=-1)
                        certainty = (mean_conf_map * (empty_img == col)).max()
                        if certainty > max_certainty:
                            max_certainty = certainty
                            max_cnt = col
                    segment_certainties.append(max_certainty)
                    mask = empty_img == max_cnt
                    conf_map[np.where(mask)] = max_certainty
            pred_label[np.where(conf_map == 0)] = 0
        pred_label_unique = np.unique(pred_label)
        labels = []
        for i in self.item_list[1:]:
            j = self.item_list.index(i)
            if i in expected_items and j in pred_label_unique:
                labels.append(i)

        # t1 = datetime.datetime.now()
        # rospy.logerr('Took %s' % (t1 - t0))
        out_lab = np.uint8(np.copy(pred_label))

        out_lab = Image.fromarray(out_lab)
        out_lab_raw = out_lab.copy()

        out_lab.putpalette(self.cmap)
        out_lab = out_lab.convert('RGB')
        rgb_img = hd_image

        rgb_img_arr = np.array(rgb_img, dtype=np.float32) / 255

        bnd_img = mark_boundaries(rgb_img_arr, pred_label, mode='thick')

        bnd_img = Image.fromarray((bnd_img * 255).astype('uint8'))
        out_img = Image.blend(bnd_img, out_lab, 0.6)
        # t1 = datetime.datetime.now()
        # rospy.logerr('output image creation took %s' % (t1 - t0))

        # Stay at 1 because state machine code uses 1 indexing because historical reasons
        k = 1
        for c in pred_label_unique:
            # -3 for storage, tote, and because zero indexing
            if c < 1 or c > (CLASSNUM - 3):
                continue
            binmask = pred_label == c
            pred_label[np.where(pred_label == c)] = k
            k += 1
            com = ndimage.measurements.center_of_mass(binmask)

            fs = 14
            draw = ImageDraw.Draw(out_img)

            draw.text((com[1], com[0]), 'O', (0, 0, 255), font=self.font)
            draw.text((com[1] - len(self.item_list[c]) / 2 * fs / 2, com[0]),
                      self.item_list[c], (255, 255, 0),
                      font=self.font)

            draw = ImageDraw.Draw(out_img)

        # t1 = datetime.datetime.now()
        # rospy.logerr('output image creation overall took %s' % (t1 - t0))

        # t0 = datetime.datetime.now()
        res = rgbd_object_proposalResponse()
        try:
            res.classification = self.cv_bridge.cv2_to_imgmsg(
                pred_label.astype(np.uint8), 'mono8')
        except CvBridgeError as e:
            rospy.logerr(e)

        try:
            oi = np.array(out_img)
            oi = cv2.cvtColor(oi, cv2.COLOR_BGR2RGB)
            res.overlay_img = self.cv_bridge.cv2_to_imgmsg(oi, 'rgb8')
        except CvBridgeError as e:
            rospy.logerr(e)

        try:
            res.confidence_map = self.cv_bridge.cv2_to_imgmsg(
                mean_conf_map, 'mono8')
        except CvBridgeError as e:
            rospy.logerr(e)

        for i in labels:
            res.label.append(String(i))
        for i in segment_certainties:
            res.segment_certainties.append(UInt8(i))
        # t1 = datetime.datetime.now()
        # rospy.logerr('response creation took %s' % (t1 - t0))

        return res
Ejemplo n.º 56
0
seg_slice = seg.shape[0] // 8

ind = 0
seg_slice = seg.shape[0] // 4
for i in range(seg_slice, seg.shape[0] + 1, seg_slice):
    for j in range(seg_slice, seg.shape[1] + 1, seg_slice):
        seg[i - seg_slice:i, j - seg_slice:j] = ind
        ind += 1

# import pdb; pdb.set_trace()
fig, ax = pl.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)

ax[0, 0].imshow(image.array_to_img(img_orig))
# ax[0, 0].set_title("Felzenszwalbs's method")
# import pdb; pdb.set_trace()
ax[0, 1].imshow(mark_boundaries(img, seg.astype('int64')))
ax[0, 1].set_title('SLIC')
# ax[1, 0].imshow(mark_boundaries(img, segments_quick))
# ax[1, 0].set_title('Quickshift')
# ax[1, 1].imshow(mark_boundaries(img, segments_watershed))
# ax[1, 1].set_title('Compact watershed')
pl.show()


# import pdb; pdb.set_trace()
# define a function that depends on a binary mask representing if an image region is hidden
def mask_image(zs, segmentation, image, background=None):
    if background is None:
        background = image.mean((0, 1))
    out = np.zeros(
        (zs.shape[0], image.shape[0], image.shape[1], image.shape[2]))
Ejemplo n.º 57
0
    def get_explanation(self, label, num_features=20, which_exp='positive'):

        # the explanation to display:
        '''

        :param label: label to explain
        :param num_features: how many top features to display
        :param positive: want features that encourage or discourage the dicision (label)
        :return:
            this_exp: raw feature and weight values
            readable_exp: a text description of the explanation
            txt_exp_list: text part of the explanation, ready to display
            temp, mask: image part of the explanation, ready to display
        '''
        this_exp = np.array(self.local_exp[label])

        if which_exp == 'positive':
            positives = this_exp[this_exp[:, 1] >= 0]
        elif which_exp == 'negative':
            positives = this_exp[this_exp[:, 1] < 0]

        if positives.shape[0] < num_features:
            num_features = positives.shape[0]
        top_exp = positives[:num_features]
        top_exp_unique, top_idx = np.unique(top_exp[:, 0], return_index=True)

        txt_exp = top_exp[top_exp[:, 0] < self.n_txt_features]
        n_txt_exp = txt_exp.shape[0]
        txt_top_idx = []
        for txt_feature in txt_exp:
            txt_top_idx.append(top_idx[top_exp_unique == txt_feature[0]] + 1)

        img_exp = top_exp[top_exp[:, 0] >= self.n_txt_features]
        n_img_exp = img_exp.shape[0]
        img_top_idx = []
        for img_feature in img_exp:
            img_top_idx.append(top_idx[top_exp_unique == img_feature[0]] + 1)

        readable_exp = f"among the top 10 features, {n_txt_exp} are from the text (the top"
        for i in txt_top_idx:
            readable_exp += str(i)

        readable_exp += f"), {n_img_exp} are from the image (the top"
        for j in img_top_idx:
            readable_exp += str(j)

        readable_exp += f"), displayed as follows"

        txt_exp_list = np.array(self.as_list(label), dtype='object')

        # return explanations upon request
        if which_exp == 'positive':
            txt_list = txt_exp_list[txt_exp_list[:, 1] >= 0]
            temp, mask = self.get_image_and_mask(label, num_features=n_img_exp)
        else:
            txt_list = txt_exp_list[txt_exp_list[:, 1] < 0]
            temp, mask = self.get_image_and_mask(label,
                                                 num_features=n_img_exp,
                                                 positive_only=False,
                                                 negative_only=True)

        txt_list = txt_list[:n_txt_exp]
        img_boundary = mark_boundaries(temp, mask)
        im = Image.fromarray(np.uint8(img_boundary * 255))
        return this_exp, readable_exp, txt_list, im
Ejemplo n.º 58
0
    def mark_boundaries(self, pixel_buffer=50):

        return segmentation.mark_boundaries(self.rgb(pixel_buffer), self.binary(pixel_buffer))
Ejemplo n.º 59
0
    def train_on_batch(self, batch, **extras):

        self.train()

        images = batch["images"].cuda()
        counts = float(batch["counts"][0])

        logits = self.model_base(images)
        if self.exp_dict['model'].get('loss') == 'lcfcn':
            loss = lcfcn.compute_lcfcn_loss(logits, batch["points"].cuda(),
                                            None)
            probs = F.softmax(logits, 1)
            mask = probs.argmax(
                dim=1).cpu().numpy().astype('uint8').squeeze() * 255

            # img_mask=hu.save_image('tmp2.png',
            #             hu.denormalize(images, mode='rgb'), mask=mask, return_image=True)
            # hu.save_image('tmp2.png',np.array(img_mask)/255. , radius=3,
            #                 points=batch["points"])

        elif self.exp_dict['model'].get('loss') == 'glance':
            pred_counts = logits[:, 1].mean()
            loss = F.mse_loss(pred_counts.squeeze(), counts.float().squeeze())

        elif self.exp_dict['model'].get('loss') == 'att_lcfcn':
            probs = logits.sigmoid()

            # get points from attention
            att_dict = self.att_model.get_attention_dict(
                images_original=torch.FloatTensor(
                    hu.denormalize(batch['images'], mode='rgb')),
                counts=batch['counts'][0],
                probs=probs.squeeze(),
                return_roi=True)
            if 1:
                blobs = lcfcn.get_blobs(probs.squeeze().detach().cpu().numpy())
                org_img = hu.denormalize(images.squeeze(), mode='rgb')
                rgb_labels = label2rgb(
                    blobs,
                    hu.f2l(org_img),
                    bg_label=0,
                    bg_color=None,
                )
                res1 = mark_boundaries(rgb_labels, blobs)
                img_mask = hu.save_image('tmp2.png',
                                         org_img,
                                         return_image=True)
                res2 = hu.save_image('tmp.png',
                                     np.array(img_mask) / 255.,
                                     points=att_dict['points'],
                                     radius=1,
                                     return_image=True)

                hu.save_image('tmp_blobs.png',
                              np.hstack([res1, np.array(res2) / 255.]))

            loss = lcfcn.compute_loss(
                probs=probs,
                # batch["points"].cuda(),
                points=att_dict['points'].cuda(),
                roi_mask=att_dict['roi_mask'])
            # loss += .5 * F.cross_entropy(logits,
            #             torch.from_numpy(1 -
            #                 att_dict['mask_bg']).long().cuda()[None],
            #             ignore_index=1)

        self.opt.zero_grad()
        loss.backward()
        if self.exp_dict['optimizer'] == 'sps':
            self.opt.step(loss=loss)
        else:
            self.opt.step()

        return {"train_loss": float(loss)}
Ejemplo n.º 60
0

##
i = 'horse1.jpg'
img = imread(i, as_gray=False)

show_img(img)

#
labels = segmentation.slic(img, compactness=60, n_segments=4000)  #400
labels = labels + 1  # So that no labelled region is 0 and ignored by regionprops
regions = regionprops(labels)

label_rgb = color.label2rgb(labels, img, kind='avg')
show_img(label_rgb)
imsave("horse3.jpg", label_rgb)

label_rgb = segmentation.mark_boundaries(label_rgb, labels, (0, 0, 0))
show_img(label_rgb)
imsave("horse4.jpg", label_rgb)

####################################

rag = graph.rag_mean_color(img, labels)
for region in regions:
    rag.node[region['label']]['centroid'] = region['centroid']

edges_drawn_all = display_edges(label_rgb, rag, np.inf)
show_img(edges_drawn_all)
imsave("horse_graph.jpg", edges_drawn_all)