コード例 #1
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()
コード例 #2
0
ファイル: cells.py プロジェクト: brunomsaraiva/eHooke_1.0
    def compute_sept_isodata(self, mask, thick, septum_base):
        """Method used to create the cell sept_mask using the threshold_isodata
        to separate the cytoplasm from the septum"""
        cell_mask = mask
        if septum_base:
            fluor_box = 1 - self.base_box
        else:
            fluor_box = self.fluor
        perim_mask = self.compute_perim_mask(cell_mask, thick)
        inner_mask = cell_mask - perim_mask
        inner_fluor = (inner_mask > 0) * fluor_box

        threshold = threshold_isodata(inner_fluor[inner_fluor > 0])
        interest_matrix = inner_mask * (inner_fluor > threshold)

        label_matrix = label(interest_matrix, connectivity=2)
        interest_label = 0
        interest_label_sum = 0

        for l in range(np.max(label_matrix)):
            if np.sum(img_as_float(label_matrix == l + 1)) > interest_label_sum:
                interest_label = l + 1
                interest_label_sum = np.sum(img_as_float(label_matrix == l + 1))

        return img_as_float(label_matrix == interest_label)
コード例 #3
0
ファイル: make_figure.py プロジェクト: tttor/lab1231-sun-prj
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()
コード例 #4
0
ファイル: from_file.py プロジェクト: algolia/color-extractor
    def get(self, uri):
        i = imread(uri)
        if len(i.shape) == 2:
            i = gray2rgb(i)
        else:
            i = i[:, :, :3]
        c = self._image_to_color.get(i)

        dbg = self._settings['debug']
        if dbg is None:
            return c

        c, imgs = c
        b = splitext(basename(uri))[0]
        imsave(join(dbg, b + '-resized.jpg'), imgs['resized'])
        imsave(join(dbg, b + '-back.jpg'), img_as_float(imgs['back']))
        imsave(join(dbg, b + '-skin.jpg'), img_as_float(imgs['skin']))
        imsave(join(dbg, b + '-clusters.jpg'), imgs['clusters'])

        return c, {
            'resized': join(dbg, b + '-resized.jpg'),
            'back': join(dbg, b + '-back.jpg'),
            'skin': join(dbg, b + '-skin.jpg'),
            'clusters': join(dbg, b + '-clusters.jpg'),
        }
コード例 #5
0
def slic_data():
   for i in range(uu_num_train+uu_num_test):

      print "data %d" %(i+1)
      img_name = ''
      if i < 10:
         img_name = '0' + str(i)
      else:
         img_name = str(i)

      #Read first 70 images as floats
      img = img_as_float(io.imread('..\data\\training\image_2\uu_0000' + img_name + '.png'))
      img_hsv = color.rgb2hsv(img)
      gt_img = img_as_float(io.imread('..\data\\training\gt_image_2\uu_road_0000' + img_name + '.png'))

      #Create superpixels for training images
      image_segment = slic(img, n_segments = numSegments, sigma = 5)
      t, train_indices = np.unique(image_segment, return_index=True)
      images_train_indices.append(train_indices)
      image = np.reshape(img,(1,(img.shape[0]*img.shape[1]),3))
      image_hsv = np.reshape(img_hsv,(1,(img_hsv.shape[0]*img_hsv.shape[1]),3))
      #images_train.append([image[0][i] for i in train_indices])
      images_train_hsv.append([image_hsv[0][i] for i in train_indices])

      #Create gt training image values index at train_indices and converted to 1 or 0
      gt_image = np.reshape(gt_img, (1,(gt_img.shape[0]*gt_img.shape[1]),3))
      gt_image = [1 if gt_image[0][i][2] > 0 else 0 for i in train_indices]
      gt_images_train.append(gt_image)
コード例 #6
0
ファイル: images.py プロジェクト: brunomsaraiva/eHooke_1.0
    def compute_mask(self, params):
        """Creates the mask for the base image.
        Needs the base image, an instance of imageloaderparams
        and the clip area, which should be already defined
        by the load_base_image method.
        Creates the mask by improving the base mask created by the
        compute_base_mask method. Applies the mask closing, dilation and
        fill holes parameters.
        """
        self.compute_base_mask(params)

        mask = np.copy(self.base_mask)
        closing_matrix = np.ones((params.mask_closing, params.mask_closing))

        if params.mask_closing > 0:
            # removes small dark spots and then small white spots
            mask = img_as_float(morphology.closing(
                mask, closing_matrix))
            mask = 1 - \
                img_as_float(morphology.closing(
                    1 - mask, closing_matrix))

        for f in range(params.mask_dilation):
            mask = morphology.erosion(mask, np.ones((3, 3)))

        if params.mask_fill_holes:
            # mask is inverted
            mask = 1 - img_as_float(ndimage.binary_fill_holes(1.0 - mask))

        self.mask = mask

        self.overlay_mask_base_image()
コード例 #7
0
def svm_predict(case,svm_classifier):
   print "svm predict"
   A = []
   b = []

   if case == 1:
      for i in range(uu_num_test):
         img_name = i + uu_num_train
         if img_name < 10:
            img_name = '0' + str(img_name)
         else:
            img_name = str(img_name)
         print "data %d " %(i+uu_num_train)
         #Read test images as floats
         img = img_as_float(io.imread('..\data\\training\image_2\uu_0000' + img_name + '.png'))
         gt_img = img_as_float(io.imread('..\data\\training\gt_image_2\uu_road_0000' + img_name + '.png'))

         #Create superpixels for test images
         image_segment = slic(img, n_segments = numSegments, sigma = 5)
         t, train_indices = np.unique(image_segment, return_index=True)
         images_train_indices.append(train_indices)
         image = np.reshape(img,(1,(img.shape[0]*img.shape[1]),3))
         images_train.append([image[0][i] for i in train_indices])

         #Create gt test image values index at train_indices and converted to 1 or 0
         gt_image = np.reshape(gt_img, (1,(gt_img.shape[0]*gt_img.shape[1]),3))
         gt_image = [1 if gt_image[0][i][2] > 0 else 0 for i in train_indices]
         print "len of gt_image: %d with ones: %d" %(len(gt_image),gt_image.count(1))

         gt_images_train.append(gt_image)

      for i in range(uu_num_test):
         for j in range(len(images_train[i])):
            A.append(images_train[i][j])
            b.append(gt_images_train[i][j])

   else:
      for i in range(uu_num_train,uu_num_train+uu_num_test):
         for j in range(len(images_train_hsv[i])):
            #val = np.zeros(6)
            #val[0:3] = images_train[i][j]
            #val[3:6] = images_train_hsv[i][j]
            #A.append(val)
            #A.append(images_train[i][j])
            A.append(images_train_hsv[i][j])
            b.append(gt_images_train[i][j])

   A = np.asarray(A)
   b = np.asarray(b)
   print "A.shape = %s, b.shape = %s" %(A.shape,b.shape)

   predicted = svm_classifier.predict(A)
   print svm_classifier.score(A,b)

   #for i in range(len(gt_images_train)):
   #   for j in range(len(gt_images_train[i])):
   #      print "%d, %d" %(gt_images_train[i][j], predicted[j])

   print("Classification report for classifier %s:\n%s\n" %(svm_classifier,metrics.classification_report(b,predicted)))
コード例 #8
0
ファイル: parse.py プロジェクト: tomasra/ga_sandbox
def run_metrics(result_dir, metrics_initial_path, noisy_image_dir):
    # Add initial metric values
    csv_file = open(metrics_initial_path, 'r')
    csv_reader = csv.DictReader(csv_file)
    metrics_initial = {}
    for row in csv_reader:
        metrics_initial[row['image']] = {
            'q': float(row['q']),
            'ocr': float(row['ocr']),
            'mse': float(row['mse'])
        }
    csv_file.close()

    results = []
    DEFAULT_TEXT = 'Lorem ipsum\ndolor sit amet,\nconsectetur\n\nadipiscing elit.\n\nDonec vel\naliquet velit,\nid congue\nposuere.'
    runs = list(xrange(1, 10 + 1, 1))
    for run in runs:
        # print "--- RUN " + run
        run_dir = os.path.join(result_dir, str(run))
        for filename in os.listdir(run_dir):
            if not filename.endswith('.png'):
                continue
            image_name = os.path.splitext(filename)[0]
            blur, noise, contrast = _parse_image_name(image_name)
            image_path = os.path.join(run_dir, filename)
            image = util.img_as_float(io.imread(image_path))
            ocr = ocr_accuracy(image, DEFAULT_TEXT)
            try:
                result = next(
                    r for r in results
                    if r['image']['name'] == image_name)
            except StopIteration:
                result = {
                    'image': {
                        'name': image_name,
                        'blur': blur,
                        'noise': noise,
                        'contrast': contrast
                    },
                    'metrics_initial': metrics_initial[image_name],
                    'ocr': [],
                    'q': [],
                    'mse': [],
                }
                results.append(result)
            result['ocr'].append(ocr)
            result['q'].append(q_py(image))

            # MSE
            ideal_image_name = 'noisy-00-00-' + str(contrast).replace('.', '') + '.png'
            ideal_image_path = os.path.join(noisy_image_dir, ideal_image_name)
            ideal_image = util.img_as_float(io.imread(ideal_image_path))
            mse_val = mse(ideal_image, image)
            result['mse'].append(mse_val)
    return results
コード例 #9
0
ファイル: run.py プロジェクト: tomasra/ga_sandbox
def get_images(noisy_image_dir, clear_image_dir):
    images = []
    for noisy_image_file in sorted(os.listdir(noisy_image_dir)):
        name, ext = os.path.splitext(noisy_image_file)
        contrast = name.split('-')[::-1][0]
        clear_image_name = 'clear-00-00-' + contrast + ext
        noisy_image_path = os.path.join(noisy_image_dir, noisy_image_file)
        clear_image_path = os.path.join(clear_image_dir, clear_image_name)
        # Read images
        noisy_image = util.img_as_float(io.imread(noisy_image_path))
        clear_image = util.img_as_float(io.imread(clear_image_path))
        images.append((noisy_image, clear_image, name))
    return images
コード例 #10
0
def create_bin(img, otsu_method=True):
    # Binary image created from Threshold, then labelling is done on this image
    if otsu_method:
        int_img = rescale(img)
        t_otsu = threshold_otsu(int_img)
        bin_img = (int_img >= t_otsu)
        float_img = img_as_float(bin_img)

        return float_img
    else:
        thresh = 400
        int_img = rescale(img)
        bin_img = (int_img >= thresh)
        float_img = img_as_float(bin_img)
        return float_img
コード例 #11
0
	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)
コード例 #12
0
def harris_ones(img, window_size, k=0.05):
    """Calculate the harris score based on a window function of diagonal ones.
    Args:
        img The image to use for corner detection.
        window_size Size of the window (NxN).
        k Weighting parameter during the final scoring (det vs. trace).
    Returns:
        Corner score image
    """
    # Gradients
    img = skiutil.img_as_float(img)
    imgy, imgx = np.gradient(img)

    imgxy = imgx * imgy
    imgxx = imgx ** 2
    imgyy = imgy ** 2

    # window function (matrix of diagonal ones)
    window = np.ones((window_size, window_size))

    # compute parts of harris matrix
    a11 = signal.correlate(imgxx, window, mode="same") / window_size
    a12 = signal.correlate(imgxy, window, mode="same") / window_size
    a21 = a12
    a22 = signal.correlate(imgyy, window, mode="same") / window_size

    # compute score per pixel
    det_a = a11 * a22 - a12 * a21
    trace_a = a11 + a22

    return det_a - k * trace_a ** 2
コード例 #13
0
ファイル: kolax.py プロジェクト: npinto/kolax
def get_image(fname):
    arr = io.imread(fname)
    if arr.ndim == 2:
        arr = color.gray2rgb(arr)
    arr = util.img_as_float(arr)
    assert arr.ndim == 3
    return arr
コード例 #14
0
def segment_selection_using_GA(name_file, param):
    global map_segment
    map_segment = generate_map_segment(name_file, param)
    global img
    img = img_as_float(io.imread(param['img_dir']+name_file+'.ppm'))
    ind_segment = do_selection(name_file)
    create_segmentfile_from_binary_string(name_file, param['segment_dir'], param['corpus_meta'], param['output_dir'], param['img_dir'], ind_segment)
コード例 #15
0
ファイル: make_figure.py プロジェクト: jogie/lab1231-sun-prj
def plot_top_N_segment_in_topic(list_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, N, output_dir):
    index = 0
    for name_file in list_file:
        index += 1
        print name_file, index
        for topic in range(n_topic):            
            fig = plt.figure()
            index_plt = 1
            ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
            val_segment = [prob_topic_doc[ind][topic] for ind in ind_segment]
            ind_of_ind_segment = get_index_bestdoc(val_segment, 12)
            for index in ind_of_ind_segment:
                segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[ind_segment[index]][1])
                no_segment = int(corpus_meta[ind_segment[index]][2])
                output = img_as_float(io.imread(img_dir+name_file+'.ppm'))
                output[segments_res != no_segment] = (0.0,0.0,0.9)
                ax1 = fig.add_subplot(4,3, index_plt, axisbg='grey')
                ax1.set_xticks(()), ax1.set_yticks(())
                ax1.set_title(prob_topic_doc[ind_segment[index]][topic])
                ax1.imshow(output)
                index_plt += 1
            ensure_path(output_dir+'/'+name_file+'/')
            plt.savefig(output_dir+'/'+name_file+'/'+str(topic)+'.pdf')
            plt.clf()
            plt.close()            
コード例 #16
0
def main():
    """Load template image (needle) and the large example image (haystack),
    generate matching score per pixel, plot the results."""
    img_haystack = skiutil.img_as_float(data.camera()) # the image in which to search
    img_needle = img_haystack[140:190, 220:270] # the template to search for
    img_sad = np.zeros(img_haystack.shape) # score image

    height_h, width_h = img_haystack.shape
    height_n, width_n = img_needle.shape

    # calculate score for each pixel
    # stop iterating over pixels when the whole template cannot any more (i.e. stop
    # at bottom and right border)
    for y in range(height_h - height_n):
        for x in range(width_h - width_n):
            patch = img_haystack[y:y+height_n, x:x+width_n]
            img_sad[y, x] = sad(img_needle, patch)
    img_sad = img_sad / np.max(img_sad)

    # add highest score to bottom and right borders
    img_sad[height_h-height_n:, :] = np.max(img_sad[0:height_h, 0:width_h])
    img_sad[:, width_h-width_n:] = np.max(img_sad[0:height_h, 0:width_h])

    # plot results
    util.plot_images_grayscale(
        [img_haystack, img_needle, img_sad],
        ["Image", "Image (Search Template)", "Matching (darkest = best match)"]
    )
コード例 #17
0
ファイル: saliency.py プロジェクト: yhenon/pyimgsaliency
def get_saliency_ft(img_path):

	# Saliency map calculation based on:

	img = skimage.io.imread(img_path)

	img_rgb = img_as_float(img)

	img_lab = skimage.color.rgb2lab(img_rgb) 

	mean_val = np.mean(img_rgb,axis=(0,1))

	kernel_h = (1.0/16.0) * np.array([[1,4,6,4,1]])
	kernel_w = kernel_h.transpose()

	blurred_l = scipy.signal.convolve2d(img_lab[:,:,0],kernel_h,mode='same')
	blurred_a = scipy.signal.convolve2d(img_lab[:,:,1],kernel_h,mode='same')
	blurred_b = scipy.signal.convolve2d(img_lab[:,:,2],kernel_h,mode='same')

	blurred_l = scipy.signal.convolve2d(blurred_l,kernel_w,mode='same')
	blurred_a = scipy.signal.convolve2d(blurred_a,kernel_w,mode='same')
	blurred_b = scipy.signal.convolve2d(blurred_b,kernel_w,mode='same')

	im_blurred = np.dstack([blurred_l,blurred_a,blurred_b])

	sal = np.linalg.norm(mean_val - im_blurred,axis = 2)
	sal_max = np.max(sal)
	sal_min = np.min(sal)
	sal = 255 * ((sal - sal_min) / (sal_max - sal_min))
	return sal
コード例 #18
0
ファイル: make_figure.py プロジェクト: jogie/lab1231-sun-prj
def create_best_segment_image(list_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir):
    for name_file in list_file:
        print name_file
        buff_segment = []
        ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
        val_segment = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment for topic in range(n_topic)]
        val_segment = sorted(val_segment,key=itemgetter(1), reverse=True)
        bag_of_best_segment = val_segment[0:n_segment]
        fig = plt.figure()
        index_plt = 1
        for index, prob in bag_of_best_segment:
            print 'jogie'
            img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
            n_col = len(img[0])
            list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            no_segment = int(corpus_meta[index][2])
            buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
            segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            img[segments_res != no_segment] = (0.0,0.0,0.9)
            ax1 = fig.add_subplot(6,5, index_plt, axisbg='grey')
            ax1.set_xticks(()), ax1.set_yticks(())
            ax1.imshow(img)
            index_plt += 1
        plt.savefig('coba/'+name_file)
        plt.clf()
        plt.close()
コード例 #19
0
ファイル: utils.py プロジェクト: tomasra/ga_sandbox
def enumerate_images(image_dir, format='png'):
    for filename in os.listdir(image_dir):
        if os.path.splitext(filename)[1].endswith(format):
            image_path = os.path.join(image_dir, filename)
            image = util.img_as_float(io.imread(image_path))
            image_name = os.path.splitext(filename)[0]
            yield image_name, image
コード例 #20
0
ファイル: parse.py プロジェクト: tomasra/ga_sandbox
def run_initial(noisy_image_dir, output_file):
    DEFAULT_TEXT = 'Lorem ipsum\ndolor sit amet,\nconsectetur\n\nadipiscing elit.\n\nDonec vel\naliquet velit,\nid congue\nposuere.'
    fieldnames = ['image', 'q', 'ocr', 'mse']
    csv_file = open(output_file, 'w')
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    clear_images = _read_clear_images()
    for noisy_image_file in sorted(os.listdir(noisy_image_dir)):
        if noisy_image_file.endswith('.png'):
            image = util.img_as_float(io.imread(
                os.path.join(noisy_image_dir, noisy_image_file)))
            image_name = os.path.splitext(noisy_image_file)[0]
            # All three metrics
            q = q_py(image)
            ocr = ocr_accuracy(image, DEFAULT_TEXT)
            _, __, contrast = _parse_image_name(image_name)
            contrast_str = str(contrast).replace('.', '')
            clear_image_name = 'clear-00-00-' + contrast_str
            mse_val = mse(clear_images[clear_image_name], image)

            result_row = {
                'image': image_name,
                'q': q,
                'ocr': ocr,
                'mse': mse_val
            }
            writer.writerow(result_row)
    csv_file.close()
    return None
コード例 #21
0
ファイル: corner.py プロジェクト: alfonsodiecko/PYTHON_DIST
def _compute_auto_correlation(image, sigma):
    """Compute auto-correlation matrix using sum of squared differences.

    Parameters
    ----------
    image : ndarray
        Input image.
    sigma : float
        Standard deviation used for the Gaussian kernel, which is used as
        weighting function for the auto-correlation matrix.

    Returns
    -------
    Axx : ndarray
        Element of the auto-correlation matrix for each pixel in input image.
    Axy : ndarray
        Element of the auto-correlation matrix for each pixel in input image.
    Ayy : ndarray
        Element of the auto-correlation matrix for each pixel in input image.

    """

    if image.ndim == 3:
        image = img_as_float(rgb2grey(image))

    imx, imy = _compute_derivatives(image)

    # structure tensore
    Axx = ndimage.gaussian_filter(imx * imx, sigma, mode='constant', cval=0)
    Axy = ndimage.gaussian_filter(imx * imy, sigma, mode='constant', cval=0)
    Ayy = ndimage.gaussian_filter(imy * imy, sigma, mode='constant', cval=0)

    return Axx, Axy, Ayy
コード例 #22
0
ファイル: parse.py プロジェクト: tomasra/ga_sandbox
def run_q(result_dir, output_file):
    csv_file = open(output_file, 'w')
    # fieldnames = ['image', 'param_set', 'final_error', 'training_time']
    fieldnames = ['image', 'param_set', 'q']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    # Enumerate images
    for image_name in sorted(os.listdir(result_dir)):
        image_dir = os.path.join(result_dir, image_name)
        if os.path.isdir(image_dir):
            print image_name
            # Enumerate parameter sets
            for result_file in sorted(os.listdir(image_dir)):
                if result_file.endswith('.png'):
                    image_path = os.path.join(image_dir, result_file)
                    image = util.img_as_float(io.imread(image_path))
                    q = q_py(image)

                    # result_json_path = os.path.join(image_dir, result_json)
                    result_ps_name = os.path.splitext(result_file)[0]
                    # result_data = read_results(result_json_path)
                    # # last_epoch_error = float(parse_epochs(result_data)[-1]['error'])
                    # last_epoch_error = float(parse_epochs(result_data)[-1])
                    # # Write into csv file
                    result_row = {
                        'image': image_name,
                        'param_set': result_ps_name,
                        'q': q,
                    }
                    writer.writerow(result_row)
    csv_file.close()
    return None
コード例 #23
0
ファイル: parse.py プロジェクト: tomasra/ga_sandbox
def run_ocr(result_dir, output_file):
    DEFAULT_TEXT = 'Lorem ipsum\ndolor sit amet,\nconsectetur\n\nadipiscing elit.\n\nDonec vel\naliquet velit,\nid congue\nposuere.'
    csv_file = open(output_file, 'w')
    fieldnames = ['image', 'param_set', 'ocr']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    # Enumerate images
    for image_name in sorted(os.listdir(result_dir)):
        image_dir = os.path.join(result_dir, image_name)
        if os.path.isdir(image_dir):
            print image_name
            # Enumerate parameter sets
            for result_file in sorted(os.listdir(image_dir)):
                if result_file.endswith('.png'):
                    image_path = os.path.join(image_dir, result_file)
                    image = util.img_as_float(io.imread(image_path))
                    ocr = ocr_accuracy(image, DEFAULT_TEXT)

                    result_ps_name = os.path.splitext(result_file)[0]
                    # # Write into csv file
                    result_row = {
                        'image': image_name,
                        'param_set': result_ps_name,
                        'ocr': ocr,
                    }
                    writer.writerow(result_row)
    csv_file.close()
    return None
コード例 #24
0
def SegmentationFelz_run_2d(rod):
    img = img_as_float(
        RodriguesToUnambiguousColor(rod["x"], rod["y"], rod["z"], maxRange=None, centerR=None).astype("uint8")
    )
    segments_slic = felzenszwalb(img, scale=100, sigma=0.0, min_size=10)
    print("Slic number of segments: %d" % len(np.unique(segments_slic)))
    return segments_slic
コード例 #25
0
ファイル: util.py プロジェクト: Hypnus1803/sunpy
def to_norm(arr):
    """
    Helper function to normalise/scale an array.  This is needed for example
    for scikit-image which uses floats between 0 and 1.

    Parameters
    ----------
    arr : `~numpy.ndarray`
        Array to normalise.

    Returns
    -------
    arr : `~numpy.ndarray`
        Array with values between 0 (min) and 1 (max)

    Examples
    --------
    >>> import numpy as np
    >>> from sunpy.image.util import to_norm
    >>> out = to_norm(np.array([-1, 0, 1]))
    >>> out
    array([ 0. ,  0.5,  1. ])
    """
    arr = np.array(arr, dtype='double')
    arr = img_as_float(arr, force_copy=True)
    if arr.min() < 0:
        arr += np.abs(arr.min())
    arr /= arr.max()
    return arr
コード例 #26
0
ファイル: rank.py プロジェクト: zverham/cs6501_project
def build_target(args):
	target = img_as_float(io.imread(args['target']))	
	target = color.rgb2gray(target)
	ratio = float(target.shape[0]) / float(target.shape[1])
	target = transform.resize(target, (sz, int(sz/ratio)))

	return target
コード例 #27
0
 def segment(self):
     self.segments = slic(img_as_float(self.img), enforce_connectivity=True)
     self.mask = np.zeros(self.img.shape[:2],dtype='int' )
     self.mask = self.mask - 1
     for (i, segVal) in enumerate(np.unique(self.segments)):
         self.mask[segVal == self.segments] = i
         self.pixel_list.append(self.Pixel(i))
コード例 #28
0
ファイル: parse.py プロジェクト: tomasra/ga_sandbox
def recreate_images(result_dir, noisy_image_dir):
    # Read noisy images first
    test_images = {}
    for image_name in os.listdir(noisy_image_dir):
        if image_name.endswith('.png'):
            image_path = os.path.join(noisy_image_dir, image_name)
            image = util.img_as_float(io.imread(image_path))
            image_name_noext = os.path.splitext(image_name)[0]
            test_images[image_name_noext] = image
    # Enumerate results - image directories
    for image_name in sorted(os.listdir(result_dir)):
        image_dir = os.path.join(result_dir, image_name)
        if os.path.isdir(image_dir):
            print image_name
            for result_file in sorted(os.listdir(image_dir)):
                if result_file.endswith('.net'):
                    # Instantiate trained ANN from .net file
                    net_path = os.path.join(image_dir, result_file)
                    ann = libfann.neural_net()
                    ann.create_from_file(net_path)
                    # Filter the same image which it was trained with
                    filtered_image = filter_fann(
                        test_images[image_name], ann)
                    param_set_name = os.path.splitext(result_file)[0]
                    io.imsave(
                        os.path.join(image_dir, param_set_name + '.png'),
                        filtered_image)
コード例 #29
0
def harris_gauss(img, sigma=1, k=0.05):
    """Calculate the harris score based on a gauss window function.
    Args:
        img The image to use for corner detection.
        sigma The sigma value for the gauss functions.
        k Weighting parameter during the final scoring (det vs. trace).
    Returns:
        Corner score image"""
    # Gradients
    img = skiutil.img_as_float(img)
    imgy, imgx = np.gradient(img)

    imgxy = imgx * imgy
    imgxx = imgx ** 2
    imgyy = imgy ** 2

    # compute parts of harris matrix
    a11 = ndimage.gaussian_filter(imgxx, sigma=sigma, mode="constant")
    a12 = ndimage.gaussian_filter(imgxy, sigma=sigma, mode="constant")
    a21 = a12
    a22 = ndimage.gaussian_filter(imgyy, sigma=sigma, mode="constant")

    # compute score per pixel
    det_a = a11 * a22 - a12 * a21
    trace_a = a11 + a22
    score = det_a - k * trace_a ** 2

    return score
コード例 #30
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                     
コード例 #31
0
def adjust_contast_brightness(image: np.ndarray,
                              alpha=1.0,
                              betta=0.0) -> np.ndarray:
    image = img_as_float(image)
    return np.clip(alpha * image + betta, 0, 1)
コード例 #32
0
    def process_wrapper(self, **kwargs):
        """
        From scikit-image: Quick shift segments image using quickshift clustering in Color-(x,y) space.

        Real time : No

        Keyword Arguments (in parentheses, argument name):
            * Color space (color_space): Selected color space, default: RGB
            * Width of Gaussian kernel (kernel_size): Width of Gaussian kernel used in smoothing the sample density. Higher means fewer clusters.
            * Max distance (max_dist): Cut-off point for data distances. Higher means fewer clusters
            * Ratio (ratio): Balances color-space proximity and image-space proximity. Higher values give more weight to color-space.
        """
        wrapper = self.init_wrapper(**kwargs)
        if wrapper is None:
            return False

        color_space = self.get_value_of("color_space")
        kernel_size = self.get_value_of("kernel_size")
        max_dist = self.get_value_of("max_dist")
        ratio = self.get_value_of("ratio") / 100

        res = False
        try:
            img = self.wrapper.current_image

            if color_space.upper() == "HSV":
                img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
            elif color_space.upper() == "LAB":
                img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)

            if kernel_size == 1:
                kernel_size = 0
            elif kernel_size % 2 == 0:
                kernel_size += 1

            img = img_as_float(img)
            labels = quickshift(img,
                                kernel_size=kernel_size,
                                max_dist=max_dist,
                                ratio=ratio)
            self.result = labels.copy()

            labels[labels == -1] = 0
            labels = ((labels - labels.min()) / (labels.max() - labels.min()) *
                      255).astype(np.uint8)
            water_img = cv2.applyColorMap(255 - labels, DEFAULT_COLOR_MAP)

            wrapper.store_image(water_img, "quick_shift_vis")

            self.demo_image = self.print_segmentation_labels(
                watershed_image=water_img.copy(),
                labels=labels,
                dbg_suffix="quick_shift",
            )

        except Exception as e:
            res = False
            wrapper.error_holder.add_error(
                new_error_text=f'Failed to process {self. name}: "{repr(e)}"',
                new_error_level=35,
                target_logger=logger,
            )
        else:
            res = True
        finally:
            return res
コード例 #33
0
def detector(frame,
             average,
             roi,
             output,
             cluster_pars=["dbscan", 10, 10],
             threshold_pars=["otsu"],
             total_frames=False,
             fit_kind="circle",
             block_shape=(256, 256),
             nthreads=2,
             regex="[0-9]{6,}",
             debug=False):
    """
    Detect whitecapping using a local thresholding approach.

    There is no output, this function will write to file.

    Parameters:
    ----------
    frame : str
        Full frame path
    averages : str
        Full average path.
        Use compute_average_image.py to obtain the averages.
    roi : list or pd.DataFrame
        Region of interest for processing.
        Use minmun_bounding_geometry.py to obtain a valid file.
    output : str
        Output path for the processed data.
    cluster_pars : list
        List of parameters for clustering.
    threshold_pars : list
        List of parameters for thresholding.
    total_frames: int
        Total number of frames. for plotting only
    fit_kind : str
        What geometry to fit to a cluster. Can be either circle or ellipse.
    block_shape : tupple
        Block size for view_as_blocks. must be a power of 2
    regex : string
        Regex to get the sequential image number "[0-9]{6,}".
    debug : bool
        Run in debug mode. will run in serial and plot outputs.


    Returns:
    -------
        Nothing. Will write to file instead.
    """
    # try to figure out frame number and process ID
    PID = os.getpid()
    frmid = int(re.search(regex, os.path.basename(frame)).group())
    print("  -- started processing frame", frmid, "of", total_frames,
          "with PID", PID)

    # ---- try the detection pipeline ----
    try:

        # read
        img = img_as_float(rgb2gray(imread(frame)))

        # ---- deal with roi ----
        try:
            roi_coords, roi_rect, mask = compute_roi(roi, frame, regex=regex)
        except Exception:
            print("   -- died because of Region of Interest processing frame",
                  frmid)
            return 0

        # try to read average image
        try:
            avg = img_as_float(rgb2gray(imread(average)))
        except Exception:
            avg = np.zeros(img.shape)

        # remove average and mask where the intensity decreases
        dif = img - avg
        dif[dif < 0] = 0
        mask = rgb2gray(mask)

        # TODO: Verify effect of scalling instead of subtraction

        dif = img_as_ubyte(dif)
        img = img_as_ubyte(img)
        avg = img_as_ubyte(avg)

        # threshold
        if threshold_pars[0] == "otsu":
            trx = otsu_threshold(dif)
            bin_img = apply_threshold(dif * mask, trx)
            bin_img = np.invert(bin_img)

        elif threshold_pars[0] == "entropy":
            kptrx = kapur_threshold(dif)
            bin_img = apply_threshold(dif * mask, kptrx)
            bin_img = np.invert(bin_img)

        elif threshold_pars[0] == "adaptative":
            local_thresh = threshold_local(dif * mask,
                                           threshold_pars[1],
                                           offset=threshold_pars[2])
            bin_img = dif > local_thresh

        elif threshold_pars[0] == "constant":
            bin_img = apply_threshold(dif * mask, int(threshold_pars[1]))
            bin_img = np.invert(bin_img)

        elif threshold_pars[0] == "file":
            trxdf = pd.read_csv(threshold_pars[1])
            nearest = trxdf["frame"].values[np.argmin(
                np.abs(frmid - trxdf["frame"].values))]
            bin_img = apply_threshold(dif * mask,
                                      trxdf.iloc[nearest]["threshold"])
            bin_img = np.invert(bin_img)

        else:
            raise ValueError("Fatal: could not deal with thresholding method.")

        # ensure the shape is right for processing as blocks
        bin_img, block_shape = ensure_shape(bin_img, block_shape)

        view = view_as_blocks(bin_img, tuple(block_shape.tolist()))

        # outputs
        dfs = []  # store dbscan results

        # loop over image blocks
        for i in range(view.shape[0]):
            for j in range(view.shape[1]):

                # target block
                blk = view[i, j, :, :]

                # update indexes
                i1 = block_shape[0] * i
                i2 = i1 + block_shape[0]
                j1 = block_shape[1] * j
                j2 = j1 + block_shape[1]

                # try to group bright pixels
                try:
                    df = cluster(np.invert(blk),
                                 cluster_pars[1],
                                 cluster_pars[2],
                                 backend=cluster_pars[0],
                                 nthreads=nthreads,
                                 fit_kind=fit_kind)
                    if not df.empty:

                        # fix offsets
                        # i, j need to swaped here, not sure why
                        df["ic"] = df["ic"] + j1
                        df["jc"] = df["jc"] + i1

                        # add info about the processing blocks
                        df["block_i"] = j
                        df["block_j"] = i
                        df["block_i_left"] = j1
                        df["block_i_right"] = j2
                        df["block_j_top"] = i1
                        df["block_j_bottom"] = i2

                        # append
                        dfs.append(df)

                except Exception:
                    raise
                    pass  # do nothing here
                    # it means that a block search failled

        # concatenate
        if dfs:
            df_dbscan = pd.concat(dfs)

            # add some extra information
            # df_dbscan["step"] = int(os.path.basename(frame).split(".")[0])
            df_dbscan["frame"] = frmid

            # write to file
            fname = str(frmid).zfill(8) + ".csv"
            df_dbscan.to_csv(os.path.join(output, fname), index=False)
        else:
            print("   -- no clusters were found processing frame", frmid)
            df_dbscan = pd.DataFrame()
            return 0

        # debug plot
        if debug:
            fig, ax = plot(frmid, img, bin_img, block_shape, roi_rect,
                           df_dbscan, total_frames, fit_kind)

            # save to file
            fname = str(frmid).zfill(8) + ".png"
            plt.savefig(os.path.join(output, fname),
                        dpi=150,
                        bbox_inches="tight",
                        pad_inches=0.1)
            # plt.show()
            plt.close()

    except Exception:
        raise
        print("   -- died for some unknown reason processing frame", frmid)

    print("   -- finished processing frame", frmid)

    return 1
コード例 #34
0
ファイル: main.py プロジェクト: canbax/PyWristIdentification
def vein_gabor_enhancement(img, scale):
    I = img_as_float(img)
    No_of_Orientation = 16  # in wrist paper 16 oreientations
    No_of_Scale = len(scale)
    a = scale
    Gr, _ = Gabor(0, 1.5, 1, 200, a[No_of_Scale-1])  # 1.5
    p = Energy_check(Gr)

    Gabor_Matrix_Gr = np.zeros(
        (2*p + 1, 2*p + 1, No_of_Orientation, No_of_Scale))

    ER = [0] * No_of_Scale

    for s in range(No_of_Scale):
        ang_count = 0
        for ang in range(0, 179, int(np.ceil(180 / No_of_Orientation))):
            Gr, _ = Gabor(ang, 1.5, 1, p, a[s])  # 1.5
            Gabor_Matrix_Gr[:, :, ang_count, s] = Gr
            ang_count = ang_count + 1
            if ang_count == No_of_Orientation:
                break
        R = Energy_check(np.squeeze(Gabor_Matrix_Gr[:, :, 1, s]))
        ER[s] = R

    Energy_Map = np.zeros(I.shape)
    Scale_Map = np.ones(I.shape)
    Orient_Map = np.ones(I.shape)

    E = np.zeros((I.shape[0], I.shape[1], No_of_Orientation, No_of_Scale))

    Gabor_fft_Matrix_Gr = np.zeros(
        (I.shape[0], I.shape[1], No_of_Orientation, No_of_Scale))

    I_fft = np.fft.fft2(I)
    for s in range(No_of_Scale):
        for ang in range(No_of_Orientation):
            pad1 = math.floor((I.shape[0] - (2*p+1)) / 2)
            pad2 = math.floor((I.shape[1] - (2*p+1)) / 2)
            pad = [(pad1, pad2), (pad1, pad2)]
            Gabor_fft_Matrix_Gr[:, :, ang, s] = np.fft.fft2(
                np.pad(Gabor_Matrix_Gr[:, :, ang, s], pad, mode='constant'), I.shape)

    I2 = np.square(I)
    Image_Power = np.zeros((I.shape[0], I.shape[1], No_of_Scale))
    for s in range(No_of_Scale):
        Image_Power[:, :, s] = np.sqrt(
            signal.convolve2d(I2, np.ones((ER[s], ER[s])), 'same')) + 0.1
        for ang in range(No_of_Orientation):
            E[:, :, ang, s] = np.fft.fftshift(
                np.fft.ifft2(-Gabor_fft_Matrix_Gr[:, :, ang, s] * I_fft)) / Image_Power[:, :, s]

    EngAng = np.zeros((I.shape[0], I.shape[1], No_of_Scale))
    AngIdx = np.zeros((I.shape[0], I.shape[1], No_of_Scale))
    for s in range(No_of_Scale):
        EngAng[:, :, s] = np.amax(E[:, :, :, s], axis=2)
        AngIdx[:, :, s] = np.argmax(E[:, :, :, s], axis=2)
    Energy_Map = np.amax(EngAng, axis=2)
    Scale_Map = np.argmax(EngAng, axis=2)
    for x in range(Energy_Map.shape[0]):
        for y in range(Energy_Map.shape[1]):
            Orient_Map[x, y] = AngIdx[x, y, Scale_Map[x, y]]

    return Orient_Map
コード例 #35
0
def test_li_coins_image_as_float():
    coins = util.img_as_float(data.coins())
    assert 94 / 255 < threshold_li(coins) < 95 / 255
コード例 #36
0
def test_equalize_float():
    img = util.img_as_float(test_img)
    img_eq = exposure.equalize_hist(img)

    cdf, bin_edges = exposure.cumulative_distribution(img_eq)
    check_cdf_slope(cdf)
コード例 #37
0
    def paint_segment_skimage(self,
                              image,
                              color,
                              px=0,
                              py=0,
                              idx_segment=[],
                              border=True,
                              clear=False):
        """Paint a list of segments using a index or position in image.
        
        Parameters
        ----------
        image : opencv 3-channel color image.
            Segmented image.
        color : string
            X11Color name.
        px : integer, optional, default = 0
            Segment point inside the image in x-axis.
        py : integer, optional, default = 0
            Segment point inside the image in y-axis.
        idx_segment : list, optional, default = []
            List of segments.
        border : boolean, optional, default = True
            If true paint the border of segments with default color.
        clear : boolean, optional, default = False
            If true clear previous painting in the image.

        Returns
        -------
        new_image : opencv 3-channel color image.
            Painted image.
        run_time : integer
            Running time spent in milliseconds.
        """
        # Check if segmentation was already performed
        if self._segments is None:
            return image, 0

        start_time = TimeUtils.get_time()

        # If idx_segment not passed, get the segment index using the points (px, py)
        if len(idx_segment) == 0:
            idx_segment = [self._segments[py, px]]
        height, width, channels = self._original_image.shape

        # Create a mask, painting black all pixels outside of segments and white the pixels inside
        mask_segment = np.zeros(self._original_image.shape[:2], dtype="uint8")
        for idx in idx_segment:
            mask_segment[self._segments == idx] = 255
        mask_inv = cv2.bitwise_not(mask_segment)

        # Paint all pixels in original image with choosed color
        class_color = np.zeros((height, width, 3), np.uint8)
        class_color[:, :] = X11Colors.get_color(color)
        if SkimageSegmenter.FORCE_OPT == False:
            colored_image = cv2.addWeighted(self._original_image, 0.7,
                                            class_color, 0.3, 0)
        else:
            colored_image = cv2.addWeighted(image, 0.7, class_color, 0.3, 0)
        colored_image = cv2.bitwise_and(colored_image,
                                        colored_image,
                                        mask=mask_segment)

        # Create a new image keeping the painting only in pixels inside of segments
        new_image = image if clear == False else self._original_image
        new_image = cv2.bitwise_and(new_image, new_image, mask=mask_inv)
        mask_segment[:] = 255
        new_image = cv2.bitwise_or(new_image, colored_image, mask=mask_segment)

        # If border is true, paint the segments borders
        if border == True and SkimageSegmenter.FORCE_OPT == False:
            color = X11Colors.get_color_zero_one(
                self.border_color.get_cast_val())
            outline_color = color if self.border_outline.value == 'Yes' else None

            new_image = img_as_ubyte(
                mark_boundaries(img_as_float(new_image),
                                self._segments.astype(np.int8),
                                color=color,
                                outline_color=outline_color))

        end_time = TimeUtils.get_time()

        # Return painted image
        return new_image, (end_time - start_time)
コード例 #38
0
Created on Tue Jun 20 09:23:45 2017

@author: viper
"""

matplotlib.use('pgf')  # Force Matplotlib back-end

# Modules....
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from scipy import ndimage
from skimage import segmentation
from skimage.util import img_as_float
from skimage import color

plt.close('all')  # Close all remaining figures

filename = 'DoP.tiff'
im = io.imread(filename)  # Open the image
im = img_as_float(im)
im = color.grey2rgb(im)
plt.figure(1)
plt.imshow(im, cmap='gray')

labels = segmentation.felzenszwalb(im, scale=800, sigma=0.5, min_size=250)

plt.imshow(segmentation.mark_boundaries(color.label2rgb(labels, im), labels))
plt.savefig('Processed/Felzenszwalb/' + filename)
コード例 #39
0
of Quickshift, while ``n_segments`` chooses the number of centers for kmeans.

.. [3] Radhakrishna Achanta, Appu Shaji, Kevin Smith, Aurelien Lucchi,
    Pascal Fua, and Sabine Suesstrunk, SLIC Superpixels Compared to
    State-of-the-art Superpixel Methods, TPAMI, May 2012.
"""

import matplotlib.pyplot as plt
import numpy as np

from skimage.data import lena
from skimage.segmentation import felzenszwalb, slic, quickshift
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float

img = img_as_float(lena()[::2, ::2])
segments_fz = felzenszwalb(img, scale=100, sigma=0.5, min_size=50)
segments_slic = slic(img, ratio=10, n_segments=250, sigma=1)
segments_quick = quickshift(img, 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)
fig.set_size_inches(8, 3, forward=True)
plt.subplots_adjust(0.05, 0.05, 0.95, 0.95, 0.05, 0.05)

ax[0].imshow(mark_boundaries(img, segments_fz))
ax[0].set_title("Felzenszwalbs's method")
ax[1].imshow(mark_boundaries(img, segments_slic))
コード例 #40
0
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())

# load the image and apply SLIC and extract (approximately)
# the supplied number of segments
image = cv2.imread(args["image"])
image = cv2.resize(image, (800, 600))

segments = slic(image, n_segments=40)

# 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()

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

    # show the masked region
    cv2.imshow("Applied", cv2.bitwise_and(image, image, mask=mask))
    cv2.waitKey(0)
コード例 #41
0
def adaptiveEqualization(img: np.ndarray,
                         clip_limit: float = 0.03) -> np.ndarray:
    img_f = img_as_float(img)
    logging.info(img_f[0, :4])
    return img_as_ubyte(exposure.equalize_adapthist(img_f, clip_limit))
コード例 #42
0
# In[ ]:

# In[50]:

# 3 segmentation algorithms: felzenszwalb, slic, quickshift
from __future__ import print_function

import matplotlib.pyplot as plt
import numpy as np

from skimage.segmentation import felzenszwalb, slic, quickshift
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float

img = img_as_float(resize(image, (100, 100)))
# img = img_as_float(resize(image, (50, 50)))

segments_fz = felzenszwalb(img, scale=100, sigma=0.5, min_size=50)
segments_slic = slic(img, n_segments=250, compactness=10, sigma=1)
segments_quick = quickshift(img, 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)
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(img, segments_fz))
コード例 #43
0
    return min_i


# prepare filter bank kernels
kernels = []
for theta in range(4):
    theta = theta / 4. * np.pi
    for sigma in (1, 3):
        for frequency in (0.05, 0.25):
            kernel = np.real(gabor_kernel(frequency, theta=theta,
                                          sigma_x=sigma, sigma_y=sigma))
            kernels.append(kernel)


shrink = (slice(0, None, 3), slice(0, None, 3))
brick = img_as_float(data.load('brick.png'))[shrink]
grass = img_as_float(data.load('grass.png'))[shrink]
wall = img_as_float(data.load('rough-wall.png'))[shrink]
image_names = ('brick', 'grass', 'wall')
images = (brick, grass, wall)

# prepare reference features
ref_feats = np.zeros((3, len(kernels), 2), dtype=np.double)
ref_feats[0, :, :] = compute_feats(brick, kernels)
ref_feats[1, :, :] = compute_feats(grass, kernels)
ref_feats[2, :, :] = compute_feats(wall, kernels)

print('Rotated images matched against references using Gabor filter banks:')

print('original: brick, rotated: 30deg, match result: ', end='')
feats = compute_feats(ndi.rotate(brick, angle=190, reshape=False), kernels)
コード例 #44
0
def carregaImg(arg):
    return data.imread("../imagens/" + arg)


#carrega imagem
img_1 = carregaImg(sys.argv[1])
nome_img_saida = sys.argv[2]
mask_size = int(sys.argv[3])

# Se necessário, converte para imagem em níveis de cinza.
if img_1.ndim > 2:
    img_1 = color.rgb2gray(img_1)

# Converte imagem para float.
img_1 = util.img_as_float(img_1)

#processa a imagem
masc_random = np.ones([mask_size, mask_size], dtype=float)
masc_random = masc_random / float(mask_size ^ 2)

img = img_1
img_1 = filters.convolve(img_1, masc_random, mode='constant', cval=0)

# Operadores de Sobel
sob_h = np.array([[-1., -2., -1.], [0., 0., 0.], [1., 2., 1.]], dtype=float)
sob_v = np.array([[-1., 0., 1.], [-2., 0., 2.], [-1., 0., 1.]], dtype=float)

# Gradiente de Sobel.
im_sob_h = filters.convolve(img_1, sob_h)
im_sob_v = filters.convolve(img_1, sob_v)
コード例 #45
0
def random_walker(data,
                  labels,
                  mode='bf',
                  tol=1.e-3,
                  copy=True,
                  return_full_prob=False,
                  spacing=None,
                  alpha=0.3,
                  beta=0.3,
                  gamma=0.4,
                  a=130.0,
                  b=10.0,
                  c=800.0):
    """Random walker algorithm for segmentation from markers.
    Random walker algorithm is implemented for gray-level or multichannel
    images.
    Parameters
    ----------
    data : array_like
        Image to be segmented in phases. Gray-level `data` can be two- or
        three-dimensional; multichannel data can be three- or four-
        dimensional (multichannel=True) with the highest dimension denoting
        channels. Data spacing is assumed isotropic unless the `spacing`
        keyword argument is used.
    labels : array of ints, of same shape as `data` without channels dimension
        Array of seed markers labeled with different positive integers
        for different phases. Zero-labeled pixels are unlabeled pixels.
        Negative labels correspond to inactive pixels that are not taken
        into account (they are removed from the graph). If labels are not
        consecutive integers, the labels array will be transformed so that
        labels are consecutive. In the multichannel case, `labels` should have
        the same shape as a single channel of `data`, i.e. without the final
        dimension denoting channels.
    beta : float
        Penalization coefficient for the random walker motion
        (the greater `beta`, the more difficult the diffusion).
    mode : string, available options {'cg_mg', 'cg', 'bf'}
        Mode for solving the linear system in the random walker algorithm.
        If no preference given, automatically attempt to use the fastest
        option available ('cg_mg' from pyamg >> 'cg' with UMFPACK > 'bf').
        - 'bf' (brute force): an LU factorization of the Laplacian is
          computed. This is fast for small images (<1024x1024), but very slow
          and memory-intensive for large images (e.g., 3-D volumes).
        - 'cg' (conjugate gradient): the linear system is solved iteratively
          using the Conjugate Gradient method from scipy.sparse.linalg. This is
          less memory-consuming than the brute force method for large images,
          but it is quite slow.
        - 'cg_mg' (conjugate gradient with multigrid preconditioner): a
          preconditioner is computed using a multigrid solver, then the
          solution is computed with the Conjugate Gradient method.  This mode
          requires that the pyamg module (http://pyamg.org/) is
          installed. For images of size > 512x512, this is the recommended
          (fastest) mode.
    tol : float
        tolerance to achieve when solving the linear system, in
        cg' and 'cg_mg' modes.
    copy : bool
        If copy is False, the `labels` array will be overwritten with
        the result of the segmentation. Use copy=False if you want to
        save on memory.
    multichannel : bool, default False
        If True, input data is parsed as multichannel data (see 'data' above
        for proper input format in this case)
    return_full_prob : bool, default False
        If True, the probability that a pixel belongs to each of the labels
        will be returned, instead of only the most likely label.
    spacing : iterable of floats
        Spacing between voxels in each spatial dimension. If `None`, then
        the spacing between pixels/voxels in each dimension is assumed 1.
    Returns
    -------
    output : ndarray
        * If `return_full_prob` is False, array of ints of same shape as
          `data`, in which each pixel has been labeled according to the marker
          that reached the pixel first by anisotropic diffusion.
        * If `return_full_prob` is True, array of floats of shape
          `(nlabels, data.shape)`. `output[label_nb, i, j]` is the probability
          that label `label_nb` reaches the pixel `(i, j)` first.
    See also
    --------
    skimage.morphology.watershed: watershed segmentation
        A segmentation algorithm based on mathematical morphology
        and "flooding" of regions from markers.
    Notes
    -----
    Multichannel inputs are scaled with all channel data combined. Ensure all
    channels are separately normalized prior to running this algorithm.
    The `spacing` argument is specifically for anisotropic datasets, where
    data points are spaced differently in one or more spatial dimensions.
    Anisotropic data is commonly encountered in medical imaging.
    The algorithm was first proposed in *Random walks for image
    segmentation*, Leo Grady, IEEE Trans Pattern Anal Mach Intell.
    2006 Nov;28(11):1768-83.
    The algorithm solves the diffusion equation at infinite times for
    sources placed on markers of each phase in turn. A pixel is labeled with
    the phase that has the greatest probability to diffuse first to the pixel.
    The diffusion equation is solved by minimizing x.T L x for each phase,
    where L is the Laplacian of the weighted graph of the image, and x is
    the probability that a marker of the given phase arrives first at a pixel
    by diffusion (x=1 on markers of the phase, x=0 on the other markers, and
    the other coefficients are looked for). Each pixel is attributed the label
    for which it has a maximal value of x. The Laplacian L of the image
    is defined as:
       - L_ii = d_i, the number of neighbors of pixel i (the degree of i)
       - L_ij = -w_ij if i and j are adjacent pixels
    The weight w_ij is a decreasing function of the norm of the local gradient.
    This ensures that diffusion is easier between pixels of similar values.
    When the Laplacian is decomposed into blocks of marked and unmarked
    pixels::
        L = M B.T
            B A
    with first indices corresponding to marked pixels, and then to unmarked
    pixels, minimizing x.T L x for one phase amount to solving::
        A x = - B x_m
    where x_m = 1 on markers of the given phase, and 0 on other markers.
    This linear system is solved in the algorithm using a direct method for
    small images, and an iterative method for larger images.
    Examples
    --------
    >>> np.random.seed(0)
    >>> a = np.zeros((10, 10)) + 0.2 * np.random.rand(10, 10)
    >>> a[5:8, 5:8] += 1
    >>> b = np.zeros_like(a)
    >>> b[3, 3] = 1  # Marker for first phase
    >>> b[6, 6] = 2  # Marker for second phase
    >>> random_walker(a, b)
    array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 2, 2, 2, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)
    """
    # Parse input data
    if mode is None:
        if amg_loaded:
            mode = 'cg_mg'
        elif UmfpackContext is not None:
            mode = 'cg'
        else:
            mode = 'bf'

    if (labels != 0).all():
        warn('Random walker only segments unlabeled areas, where '
             'labels == 0. No zero valued areas in labels were '
             'found. Returning provided labels.')

        if return_full_prob:
            # Find and iterate over valid labels
            unique_labels = np.unique(labels)
            unique_labels = unique_labels[unique_labels > 0]

            out_labels = np.empty(labels.shape + (len(unique_labels), ),
                                  dtype=np.bool)
            for n, i in enumerate(unique_labels):
                out_labels[..., n] = (labels == i)

        else:
            out_labels = labels
        return out_labels

    if data.ndim < 4:
        raise ValueError('Dta must have 4 ' 'dimensions.')
    dims = data[..., 0].shape  # To reshape final labeled result
    data = img_as_float(data)

    # Spacing kwarg checks
    if spacing is None:
        spacing = np.asarray((1., ) * 4)
    elif len(spacing) == len(dims):
        if len(spacing) == 2:  # Need a dummy spacing for singleton 3rd dim
            spacing = np.r_[spacing, 1.]
        else:  # Convert to array
            spacing = np.asarray(spacing)
    else:
        raise ValueError('Input argument `spacing` incorrect, should be an '
                         'iterable with one number per spatial dimension.')

    if copy:
        labels = np.copy(labels)
    label_values = np.unique(labels)

    # Reorder label values to have consecutive integers (no gaps)
    if np.any(np.diff(label_values) != 1):
        mask = labels >= 0
        labels[mask] = rank_order(labels[mask])[0].astype(labels.dtype)
    labels = labels.astype(np.int32)

    # If the array has pruned zones, be sure that no isolated pixels
    # exist between pruned zones (they could not be determined)
    if np.any(labels < 0):
        filled = ndi.binary_propagation(labels > 0, mask=labels >= 0)
        labels[np.logical_and(np.logical_not(filled), labels == 0)] = -1
        del filled
    labels = np.atleast_3d(labels)
    if np.any(labels < 0):
        lap_sparse = _build_laplacian(data,
                                      spacing,
                                      mask=labels >= 0,
                                      alpha=alpha,
                                      beta=beta,
                                      gamma=gamma,
                                      a=a,
                                      b=b,
                                      c=c)
    else:
        lap_sparse = _build_laplacian(data,
                                      spacing,
                                      alpha=alpha,
                                      beta=beta,
                                      gamma=gamma,
                                      a=a,
                                      b=b,
                                      c=c)
    lap_sparse, B = _buildAB(lap_sparse, labels)

    # We solve the linear system
    # lap_sparse X = B
    # where X[i, j] is the probability that a marker of label i arrives
    # first at pixel j by anisotropic diffusion.
    if mode == 'cg':
        X = _solve_cg(lap_sparse,
                      B,
                      tol=tol,
                      return_full_prob=return_full_prob)
    if mode == 'cg_mg':
        if not amg_loaded:
            warn("""pyamg (http://pyamg.org/)) is needed to use
                this mode, but is not installed. The 'cg' mode will be used
                instead.""")
            X = _solve_cg(lap_sparse,
                          B,
                          tol=tol,
                          return_full_prob=return_full_prob)
        else:
            X = _solve_cg_mg(lap_sparse,
                             B,
                             tol=tol,
                             return_full_prob=return_full_prob)
    if mode == 'bf':
        X = _solve_bf(lap_sparse, B, return_full_prob=return_full_prob)

    # Clean up results
    if return_full_prob:
        labels = labels.astype(np.float)
        X = np.array([
            _clean_labels_ar(Xline, labels, copy=True).reshape(dims)
            for Xline in X
        ])

        for i in range(1, int(labels.max()) + 1):
            mask_i = np.squeeze(labels == i)
            X[:, mask_i] = 0
            X[i - 1, mask_i] = 1

    else:
        X = _clean_labels_ar(X + 1, labels).reshape(dims)
    return X
コード例 #46
0
    # construct the argument parser and parse the arguments
    #ap = argparse.ArgumentParser()
    #ap.add_argument("-i", "--image", required = True, help = "Path to the image")
    #args = vars(ap.parse_args())
    train = []

    # load the image and apply SLIC and extract (approximately)
    # the supplied number of segments
    #image = cv2.imread(args["image"])

    for i in range(3, 20):
        image = cv2.imread('images/image' + str(i) + '.jpg')
        #img = image.load_img('image2.jpg')
        ground_truth = cv2.imread('ground_truth/ground_truth' + str(i) +
                                  '.jpg')
        segments = slic(img_as_float(image), n_segments=100, sigma=5)
        res = np.zeros(image.shape[:2], dtype="uint8")
        selected = np.zeros(image.shape[:2], dtype="uint8")
        res = ground_truth[:, :, 0]

        # 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()
        label = np.zeros(1)
コード例 #47
0
    if np.issubdtype(dtype, np.integer):
        assert_array_equal(bin_centers, np.arange(imin, imax + 1))
    assert frequencies[0][0] == channel_size
    assert frequencies[0][-1] == 0
    assert frequencies[1][0] == 0
    assert frequencies[1][-1] == channel_size


# Test histogram equalization
# ===========================

np.random.seed(0)

test_img_int = data.camera()
# squeeze image intensities to lower image contrast
test_img = util.img_as_float(test_img_int)
test_img = exposure.rescale_intensity(test_img / 5. + 100)


def test_equalize_uint8_approx():
    """Check integer bins used for uint8 images."""
    img_eq0 = exposure.equalize_hist(test_img_int)
    img_eq1 = exposure.equalize_hist(test_img_int, nbins=3)
    np.testing.assert_allclose(img_eq0, img_eq1)


def test_equalize_ubyte():
    img = util.img_as_ubyte(test_img)
    img_eq = exposure.equalize_hist(img)

    cdf, bin_edges = exposure.cumulative_distribution(img_eq)
コード例 #48
0
def validate_mask(val_loader, model, criterion, val_img_index):

    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()
    count = 0
    for i, (input, target) in enumerate(val_loader):
        count += 1

        if count > val_img_index:
            break

        target = target.cuda(async=True)
        input_var = torch.autograd.Variable(input, volatile=True).cuda()
        target_var = torch.autograd.Variable(target, volatile=True).cuda()

        img_show = input[0].numpy().copy()
        img_show = img_show.transpose(1, 2, 0)
        img_show -= img_show.min()
        img_show /= img_show.max()
        img_show *= 255
        img_show = img_show.astype(np.uint8)

        if count == val_img_index:
            #cv2.imwrite("orginal_img.png", img_show)

            segments = felzenszwalb(img_as_float(img_show),
                                    scale=100,
                                    sigma=0.5,
                                    min_size=50)

            print("Felzenszwalb number of segments: {}".format(
                len(np.unique(segments))))

            # cv2.imshow('superpixels', mark_boundaries(img_as_float(img_show), segments))
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()

            # compute output
            output = model(input_var)
            loss = criterion(output, target_var)

            pred = output.data.max(1, keepdim=True)[1]
            label = target[0]

            if pred[0].cpu().numpy()[0] == label:
                print("correct prediction, index ", count)

            correct_pred_count = 0
            wrong_pred_count = 0

            dict_pixel = get_pixel_sorted_mask_label()
            plot_summed_heatmap(val_img_index, img_show, label, dict_pixel)
            sorted_dict_values_set = sorted(set(dict_pixel.values()))

            # Binary search the pixel label threshold
            first = 0
            last = len(sorted_dict_values_set) - 1

            while first <= last:

                midpoint = int((first + last) / 2)

                mask_threshold1 = sorted_dict_values_set[midpoint]

                mask_threshold2 = sorted_dict_values_set[midpoint + 1]

                mask1 = generate_new_mask(dict_pixel, mask_threshold1)

                mask2 = generate_new_mask(dict_pixel, mask_threshold2)

                print("sorted_dict_values_set")
                print(sorted_dict_values_set)

                print("len(sorted_dict_values_set)")
                print(len(sorted_dict_values_set))

                masked_img1 = input[0].numpy().copy() * mask1
                masked_img2 = input[0].numpy().copy() * mask2

                masked_img_batch1 = masked_img1[None, :, :, :]
                masked_img_batch2 = masked_img2[None, :, :, :]

                masked_img_tensor1 = torch.autograd.Variable(
                    torch.from_numpy(masked_img_batch1)).cuda()
                masked_img_tensor2 = torch.autograd.Variable(
                    torch.from_numpy(masked_img_batch2)).cuda()
                # Evaluate the NN model
                mask_output1 = model(masked_img_tensor1)
                mask_output2 = model(masked_img_tensor2)

                pred_mask1 = mask_output1.data.max(1, keepdim=True)[1]
                pred_mask2 = mask_output2.data.max(1, keepdim=True)[1]

                masked_img_show1 = masked_img1.copy()
                masked_img_show1 = masked_img_show1.transpose(1, 2, 0)
                masked_img_show1 -= masked_img_show1.min()
                masked_img_show1 /= masked_img_show1.max()
                masked_img_show1 *= 255
                masked_img_show1 = masked_img_show1.astype(np.uint8)

                masked_img_show2 = masked_img2.copy()
                masked_img_show2 = masked_img_show2.transpose(1, 2, 0)
                masked_img_show2 -= masked_img_show2.min()
                masked_img_show2 /= masked_img_show2.max()
                masked_img_show2 *= 255
                masked_img_show2 = masked_img_show2.astype(np.uint8)

                if pred_mask1[0].cpu().numpy()[0] == target[0]:

                    correct_pred_count += 1
                    print("correct_pred_count: ", correct_pred_count)

                    if pred_mask2[0].cpu().numpy()[0] != target[0]:

                        plt.subplot(141), plt.imshow(
                            cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB),
                            'gray'), plt.title('original_img_label_{}'.format(
                                classes_dict[target[0]]),
                                               fontsize=60)
                        plt.subplot(142), plt.imshow(
                            mark_boundaries(img_as_float(img_show[:, :, ::-1]),
                                            segments),
                            'gray'), plt.title('Superpixel', fontsize=60)

                        plt.subplot(143), plt.imshow(
                            mask1 * 255, 'gray'), plt.title(
                                "Mask threshold_{}".format(mask_threshold1),
                                fontsize=60)
                        plt.subplot(144), plt.imshow(
                            cv2.cvtColor(masked_img_show1, cv2.COLOR_BGR2RGB),
                            'gray'), plt.title(
                                'Org_img_with_mask pred_{}'.format(
                                    classes_dict[pred_mask1[0].cpu().numpy()
                                                 [0]]),
                                fontsize=60)

                        #cv2.imwrite("frog.png", masked_img_show1)
                        figure = plt.gcf()  # get current figure
                        figure.set_size_inches(90, 30)

                        plt.savefig(
                            'result_imgs/index_{}_threshold_{}.png'.format(
                                val_img_index, mask_threshold1))

                        plt.subplot(141), plt.imshow(
                            cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB),
                            'gray'), plt.title('original_img_label_{}'.format(
                                classes_dict[target[0]]),
                                               fontsize=60)
                        plt.subplot(142), plt.imshow(
                            mark_boundaries(img_as_float(img_show[:, :, ::-1]),
                                            segments),
                            'gray'), plt.title('Superpixel', fontsize=60)

                        plt.subplot(143), plt.imshow(
                            mask2 * 255, 'gray'), plt.title(
                                "Mask threshold_{}".format(mask_threshold2),
                                fontsize=60)
                        plt.subplot(144), plt.imshow(
                            cv2.cvtColor(masked_img_show2, cv2.COLOR_BGR2RGB),
                            'gray'), plt.title('pred_{}'.format(
                                classes_dict[pred_mask2[0].cpu().numpy()[0]]),
                                               fontsize=60)

                        figure = plt.gcf()  # get current figure
                        figure.set_size_inches(90, 30)
                        plt.savefig(
                            'result_imgs/index_{}_threshold_{}.png'.format(
                                val_img_index, mask_threshold2))
                        # plt.show()
                        # plt.close()

                        return mask_threshold1
                    else:
                        first = midpoint + 1
                else:
                    wrong_pred_count += 1
                    print("wrong_pred_count: ", wrong_pred_count)
                    last = midpoint - 1
                    print("masked label threshold")
                    print(mask_threshold1)
コード例 #49
0
def imgs2lf(input_dir,
            lf_file,
            lf_dataset='lightfield',
            img_extension='.png',
            dtype=np.uint8,
            RGB=True):
    """
    Convert several images to a lightfield.

    Parameters
    ----------
    input_dir : string
        The directory where the ligthfield images are located.
    lf_file: string
        The filename  (including the directory), of the output file.
    lf_dataset : string, optional
        The new container name of the hdf5 file.
    img_extension : string, optional
        The file extension of the images to look for.
    dtype : numpy.dtype, optional
        The new data type for the downscaled lightfield. Must be either
        np.float64, np.uint8 or np.uint16.
    RGB : bool, optional
        If True, the output lightfield will be converted to RGB (default).
        Otherwise gray type images are stored.
    """

    # look for images
    files = multiLoading(identifier="*.{e}".format(e=img_extension),
                         path=input_dir)

    # prepare saving
    lf_file = prepareSaving(lf_file, extension=".hdf5")

    # Which dtype should be used?
    if dtype == np.float64:
        img_0 = img_as_float(imread(files[0]))
    elif dtype == np.uint8:
        img_0 = img_as_ubyte(imread(files[0]))
    elif dtype == np.uint16:
        img_0 = img_as_uint(imread(files[0]))
    else:
        raise TypeError('The given data type is not supported!')

    # Do we shall take RGB or gray images?
    if (len(img_0.shape) == 3 and img_0.shape[2] == 3):
        rows, cols, orig_channels = img_0.shape  # automatically determine the images'shapes from the first image.
    elif len(img_0.shape) == 2:
        orig_channels = 1
        rows, cols = img_0.shape  # automatically determine the images'shapes from the first image.
    else:
        raise TypeError('The given images are neither gray nor RGB images!')

    f_out = h5py.File(lf_file, 'w')
    if RGB:
        dataset = f_out.create_dataset(lf_dataset,
                                       shape=(len(files), rows, cols, 3),
                                       dtype=dtype)
    else:
        dataset = f_out.create_dataset(lf_dataset,
                                       shape=(len(files), rows, cols),
                                       dtype=dtype)
    for k in range(len(files)):

        if dtype == np.float64:
            if orig_channels == 1 and RGB:
                dataset[k, ...] = img_as_float(gray2rgb(imread(files[k])))
            elif orig_channels == 3 and not RGB:
                dataset[k, ...] = img_as_float(rgb2gray(imread(files[k])))
            else:
                dataset[k, ...] = img_as_float(imread(files[k]))
        elif dtype == np.uint8:
            if orig_channels == 1 and RGB:
                dataset[k, ...] = img_as_ubyte(gray2rgb(imread(files[k])))
            elif orig_channels == 3 and not RGB:
                dataset[k, ...] = img_as_ubyte(rgb2gray(imread(files[k])))
            else:
                dataset[k, ...] = img_as_ubyte(imread(files[k]))
        elif dtype == np.uint16:
            if orig_channels == 1 and RGB:
                dataset[k, ...] = img_as_uint(gray2rgb(imread(files[k])))
            elif orig_channels == 3 and not RGB:
                dataset[k, ...] = img_as_uint(rgb2gray(imread(files[k])))
            else:
                dataset[k, ...] = img_as_uint(imread(files[k]))
        else:
            raise TypeError('Given dtype not supported.')
    f_out.close()
コード例 #50
0
def test_otsu_coins_image_as_float():
    coins = util.img_as_float(data.coins())
    assert 0.41 < threshold_otsu(coins) < 0.42
コード例 #51
0
#####################################################################
# Calibrating a wavelet denoiser

import numpy as np
from matplotlib import pyplot as plt

from skimage.data import chelsea
from skimage.restoration import calibrate_denoiser, denoise_wavelet

from skimage.util import img_as_float, random_noise
from functools import partial

# rescale_sigma=True required to silence deprecation warnings
_denoise_wavelet = partial(denoise_wavelet, rescale_sigma=True)

image = img_as_float(chelsea())
sigma = 0.3
noisy = random_noise(image, var=sigma**2)

# Parameters to test when calibrating the denoising algorithm
parameter_ranges = {
    'sigma': np.arange(0.1, 0.3, 0.02),
    'wavelet': ['db1', 'db2'],
    'convert2ycbcr': [True, False],
    'multichannel': [True]
}

# Denoised image using default parameters of `denoise_wavelet`
default_output = denoise_wavelet(noisy, multichannel=True, rescale_sigma=True)

# Calibrate denoiser
コード例 #52
0
def validate(val_loader, model, criterion, eval_img_index):
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    count = 0
    for i, (input, target) in enumerate(val_loader):
        count += 1

        if count > eval_img_index:
            break

        input_var = torch.autograd.Variable(input, volatile=True).cuda()
        target_var = torch.autograd.Variable(target, volatile=True).cuda()

        img_show = input[0].numpy().copy()
        img_show = img_show.transpose(1, 2, 0)

        img_show -= img_show.min()
        img_show /= img_show.max()
        img_show *= 255
        img_show = img_show.astype(np.uint8)
        # cv2.imshow('index_{}_label_{}'.format(i, classes_dict[target[0]]), img_show)
        #     # cv2.waitKey(0)
        if count == eval_img_index:

            segments = felzenszwalb(img_as_float(img_show),
                                    scale=100,
                                    sigma=0.5,
                                    min_size=50)

            print("Felzenszwalb number of segments: {}".format(
                len(np.unique(segments))))

            #cv2.imshow('superpixels', mark_boundaries(img_as_float(img_show), segments))
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()

            # compute output
            output = model(input_var)
            loss = criterion(output, target_var)
            print("output")
            print(output)
            print("target_var")
            print(target_var)
            print("loss")
            print(loss)

            pred = output.data.max(1, keepdim=True)[1]
            label = target[0]
            print("label ", label)
            print("pred[0].cpu().numpy() ", pred[0].cpu().numpy()[0])

            mask_dir = './masks'
            if not os.path.exists(mask_dir):
                os.makedirs(mask_dir)
            else:
                shutil.rmtree(mask_dir)  #removes all the subdirectories!
                os.makedirs(mask_dir)

            if pred[0].cpu().numpy()[0] == label:
                print("correct prediction, index_{} , label_{}".format(
                    count, classes_dict[label]))

                correct_pred_count = 0
                wrong_pred_count = 0

                for i in range(args.num_mask_samples):

                    total_num_segments = len(np.unique(segments))
                    num_conse_superpixels = int(0.4 * total_num_segments)
                    print("total_num_segments: ", total_num_segments)
                    print("num_conse_superpixels: ", num_conse_superpixels)
                    firstIndex = randint(
                        1, total_num_segments - num_conse_superpixels)

                    random_sampled_list = np.unique(segments)[firstIndex:(
                        firstIndex + num_conse_superpixels)]
                    #random_sampled_list= sample(range(np.unique(segments)[0], np.unique(segments)[-1]), num_conse_superpixels)

                    #print("random_sampled_list: ", random_sampled_list)
                    mask = np.zeros(img_show.shape[:2], dtype="uint8")
                    #mask.fill(1)
                    for (j, segVal) in enumerate(random_sampled_list):
                        mask[segments == segVal] = 1

                    masked_img = input[0].numpy().copy() * mask

                    masked_img_batch = masked_img[None, :, :, :]

                    masked_img_tensor = torch.autograd.Variable(
                        torch.from_numpy(masked_img_batch)).cuda()
                    mask_output = model(masked_img_tensor)

                    pred_mask = mask_output.data.max(1, keepdim=True)[1]

                    masked_img_show = masked_img.copy()
                    masked_img_show = masked_img_show.transpose(1, 2, 0)
                    masked_img_show -= masked_img_show.min()
                    masked_img_show /= masked_img_show.max()
                    masked_img_show *= 255
                    masked_img_show = masked_img_show.astype(np.uint8)

                    if pred_mask[0].cpu().numpy()[0] == target[0]:
                        correct_pred_count += 1
                        print("correct_pred_count: ", correct_pred_count)
                        cv2.imwrite('./masks/mask_{}_{}.png'.format(i, 1),
                                    mask * 255)
                    # cv2.imwrite('./mask_on_img/masked_imgs_{}.png'.format(i), masked_img_show)
                    else:
                        wrong_pred_count += 1
                        print("wrong_pred_count: ", wrong_pred_count)
                        cv2.imwrite('./masks/mask_{}_{}.png'.format(i, 0),
                                    mask * 255)
                        #cv2.imwrite('./mask_on_img/masked_imgs_{}.png'.format(i), masked_img_show)

                return correct_pred_count
            else:
                print("wrong prediction")
                #print("%d samples, the corrrect prediction number: %d "%(len(mask_filenames), correct_pred_count))

    return 0
コード例 #53
0
#total_int = dapi.sum( axis = 0 )
global_thresh = filters.threshold_otsu(nuclei) * 5
mask_nuclei = nuclei > global_thresh
#mask_nuclei = filters.threshold_local(nuclei, 21)

mask_roi = np.zeros(nuclei.shape, dtype=bool)
#mask_roi[nuclei>filters.threshold_otsu(nuclei)] = True
mask_roi[nuclei > 1000] = True
roi_ilot = morphology.remove_small_objects(mask_roi, 500000)
bbox = ndi.find_objects(roi_ilot)

dapi_roi = dapi[bbox[0]]
#rb_roi = rb[bbox[0]]

# Denoise using chambolle
dapi_float = util.img_as_float(dapi_roi)
dapi_float_tvc = restoration.denoise_tv_chambolle(dapi_float, weight=0.05)

# Find z-stack w/highest variance
variance = [np.var(image) for _, image in enumerate(mask_dapi)]
idx = np.argmax(variance)
dapi_roi_z = dapi_float_tvc[idx - 3:idx + 3]
distance_a = ndi.distance_transform_edt(mask_dapi[3])

#Local peak markers
smooth_distance = filters.gaussian(distance_a, sigma=4)
local_maxi = feature.peak_local_max(smooth_distance,
                                    indices=True,
                                    exclude_border=False,
                                    footprint=np.ones((15, 15)))
markers_lm = np.zeros(distance_a.shape, dtype=np.int)
コード例 #54
0
    for i, h in enumerate(norm_hists):
        if i in fgbg_superpixels[0]:
            g.add_tedge(nodes[i], 0, 1000)  # FG - set high cost to BG
        elif i in fgbg_superpixels[1]:
            g.add_tedge(nodes[i], 1000, 0)  # BG - set high cost to FG
        else:
            g.add_tedge(nodes[i],
                        cv2.compareHist(fgbg_hists[0], h, hist_comp_alg),
                        cv2.compareHist(fgbg_hists[1], h, hist_comp_alg))

    g.maxflow()
    return g.get_grid_segments(nodes)


if __name__ == '__main__':
    img = img_as_float(astronaut()[::2, ::2])
    img_marking = cv2.imread("astronaut_marking.png")

    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)
コード例 #55
0
def load_pred(output_dir, imname):
    pred_path = os.path.join(output_dir, '{}.png'.format(imname))
    return img_as_float(imread(pred_path))
コード例 #56
0
# prepare filter bank kernels
kernels = []
for theta in range(4):
    theta = theta / 4. * np.pi
    for sigma in (1, 3):
        for frequency in (0.05, 0.25):
            kernel = np.real(
                gabor_kernel(frequency,
                             theta=theta,
                             sigma_x=sigma,
                             sigma_y=sigma))
            kernels.append(kernel)

shrink = (slice(0, None, 3), slice(0, None, 3))
brick = img_as_float(data.brick())[shrink]
grass = img_as_float(data.grass())[shrink]
gravel = img_as_float(data.gravel())[shrink]
image_names = ('brick', 'grass', 'gravel')
images = (brick, grass, gravel)

# prepare reference features
ref_feats = np.zeros((3, len(kernels), 2), dtype=np.double)
ref_feats[0, :, :] = compute_feats(brick, kernels)
ref_feats[1, :, :] = compute_feats(grass, kernels)
ref_feats[2, :, :] = compute_feats(gravel, kernels)

print('Rotated images matched against references using Gabor filter banks:')

print('original: brick, rotated: 30deg, match result: ', end='')
feats = compute_feats(ndi.rotate(brick, angle=190, reshape=False), kernels)
コード例 #57
0
from __future__ import print_function

import numpy as np
from matplotlib import pyplot as plt

from skimage import data
from skimage.util import img_as_float
from skimage.feature import (corner_harris, corner_subpix, corner_peaks,
                             plot_matches)
from skimage.transform import warp, AffineTransform
from skimage.exposure import rescale_intensity
from skimage.color import rgb2gray
from skimage.measure import ransac

# generate synthetic checkerboard image and add gradient for the later matching
checkerboard = img_as_float(data.checkerboard())
img_orig = np.zeros(list(checkerboard.shape) + [3])
img_orig[..., 0] = checkerboard
gradient_r, gradient_c = np.mgrid[0:img_orig.shape[0],
                                  0:img_orig.shape[1]] / float(
                                      img_orig.shape[0])
img_orig[..., 1] = gradient_r
img_orig[..., 2] = gradient_c
img_orig = rescale_intensity(img_orig)
img_orig_gray = rgb2gray(img_orig)

# warp synthetic image
tform = AffineTransform(scale=(0.9, 0.9), rotation=0.2, translation=(20, -10))
img_warped = warp(img_orig, tform.inverse, output_shape=(200, 200))
img_warped_gray = rgb2gray(img_warped)
コード例 #58
0
def paste(img, canvas, i, j, method='replace', export_dtype='float'):
    """ paste `img` on `canvas` with its left-top corner at (i, j) """
    # check dtypes
    img = su.img_as_float(img)
    canvas = su.img_as_float(canvas)
    # check shapes
    if len(img.shape) != 2 or len(img.shape) != 3:
        if len(canvas.shape) != 2 or len(canvas.shape) != 3:
            assert AttributeError(
                'dimensions of input images not all equal to 2 or 3!')
    # check channels
    # all grayscale image
    if len(img.shape) == 2 and len(canvas.shape) == 2:
        pass
    # `img` color image, possible with alpha channel; `canvas` grayscale image
    elif len(img.shape) == 3 and len(canvas.shape) == 2:
        c = img.shape[-1]
        if c == 3:
            canvas = np.stack([canvas] * c, axis=-1)
        if c == 4:
            canvas = np.stack([canvas] * (c - 1) +
                              [np.ones((canvas.shape[0], canvas.shape[1]))],
                              axis=-1)
    # `canvas` color image, possible with alpha channel; `img` grayscale image
    elif len(img.shape) == 2 and len(canvas.shape) == 3:
        c = canvas.shape[-1]
        if c == 3:
            img = np.stack([img] * c, axis=-1)
        if c == 4:
            img = np.stack([img] * (c - 1) +
                           [np.ones((img.shape[0], img.shape[1]))],
                           axis=-1)
    # all color image
    elif len(img.shape) == 3 and len(canvas.shape) == 3:
        if img.shape[-1] == 3 and canvas.shape[-1] == 4:
            img = np.concatenate(
                [img, np.ones((img.shape[0], img.shape[1], 1))], -1)
        elif img.shape[-1] == 4 and canvas.shape[-1] == 3:
            canvas = np.concatenate(
                [canvas,
                 np.ones((canvas.shape[0], canvas.shape[1], 1))], -1)
        elif img.shape[-1] == canvas.shape[-1]:
            pass
        else:
            assert ValueError('channel number should equal to 3 or 4!')
    # get shapes
    h_i, w_i = img.shape[:2]
    h_c, w_c = canvas.shape[:2]
    # find extent of `img` on `canvas`
    i_min = np.max([0, i])
    i_max = np.min([h_c, i + h_i])
    j_min = np.max([0, j])
    j_max = np.min([w_c, j + w_i])
    # paste `img` on `canvas`
    if method == 'replace':
        canvas[i_min:i_max, j_min:j_max] = img[i_min - i:i_max - i,
                                               j_min - j:j_max - j]
    elif method == 'add':
        canvas[i_min:i_max, j_min:j_max] += img[i_min - i:i_max - i,
                                                j_min - j:j_max - j]
    else:
        raise ValueError('no such method!')
    # return `canvas`
    if export_dtype == 'float':
        return canvas
    elif export_dtype == 'ubyte':
        return su.img_as_ubyte(canvas)
    else:
        raise ValueError('no such data type for exporting!')
コード例 #59
0
def test_yen_coins_image_as_float():
    coins = util.img_as_float(data.coins())
    assert 0.43 < threshold_yen(coins) < 0.44
コード例 #60
0
    bck = bck / i
    #kp.append(list(bck.astype(np.uint8)))
    return list(bck.astype(np.uint8))


# def read_raw(filename,r,c):
# 	fd = open(os.path.join(mask_src,filename.split('.')[0] + '.raw' ), 'rb')

# 	f = np.fromfile(fd, dtype=np.uint8,count=r*c)
# 	im = f.reshape((r,c)) #notice row, column format
# 	fd.close()
# 	return im
g = gm.GenerateMask()
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(img_src, filename)))
    img = cv2.imread(os.path.join(img_src, filename))
    #image =  cv2.resize(image,None,fx=2.5,fy=2.5,interpolation=cv2.INTER_CUBIC)
    #image =  cv2.resize(image,None,fx=2.5,fy=2.5,interpolation=cv2.INTER_CUBIC)
    #img =  cv2.resize(img,None,fx=2.5,fy=2.5,interpolation=cv2.INTER_CUBIC).astype(np.uint8)
    temp = image.copy().astype('uint8')
    h, w, d = image.shape

    kp = read_csv(filename)
    bg, sure_fg = g.mask_generate(kp, h, w)

    mid_hip = ret_mid_pt([kp[8], kp[11]])
    sure_fg = add_limb(kp[1], mid_hip, sure_fg)
    # sure_fg2 = add_limb(kp[1],kp[11],sure_fg)
    # sure_fg2 = add_limb(kp[1],kp[8],sure_fg2)
    sure_fg = add_limb(kp[5], kp[8], sure_fg)