Python skimage 模块,img_as_float() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用skimage.img_as_float()

项目:iterative_inference_segm    作者:adri-romsor    | 项目源码 | 文件源码
def my_label2rgboverlay(labels, colors, image, alpha=0.2):
    """
    Generates image with segmentation labels on top

    Parameters
    ----------
    labels:  labels of one image (0, 1)
    colors:  colormap
    image:   image (0, 1, c), where c=3 (rgb)
    alpha: transparency
    """
    image_float = gray2rgb(img_as_float(rgb2gray(image) if
                                        image.shape[2] == 3 else
                                        np.squeeze(image)))
    label_image = my_label2rgb(labels, colors)
    output = image_float * alpha + label_image * (1 - alpha)
    return output
项目:S2VT    作者:chenxinpeng    | 项目源码 | 文件源码
def preprocess_frame(image, target_height=224, target_width=224):

    if len(image.shape) == 2:
        image = np.tile(image[:,:,None], 3)
    elif len(image.shape) == 4:
        image = image[:,:,:,0]

    image = skimage.img_as_float(image).astype(np.float32)
    height, width, rgb = image.shape
    if width == height:
        resized_image = cv2.resize(image, (target_height,target_width))

    elif height < width:
        resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
        cropping_length = int((resized_image.shape[1] - target_height) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_width) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return cv2.resize(resized_image, (target_height, target_width))
项目:DeepNet    作者:hok205    | 项目源码 | 文件源码
def process_patches(images, net, transformer):
    """ Process a patch through the neural network and extract the predictions

    Args:
        images (array list): list of images to process (length = batch_size)
        net (obj): the Caffe Net
        transformer (obj): the Caffe Transformer for preprocessing
    """
    # caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
    # but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
    data = np.zeros(net.blobs['data'].data.shape)
    for i in range(len(images)):
        data[i] = transformer.preprocess('data', img_as_float(images[i]))
    net.forward(data=data)
    output = net.blobs['conv1_1_D'].data[:len(images)]
    output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
    return output
项目:DeepNet    作者:hok205    | 项目源码 | 文件源码
def process_patches(images, net, transformer):
    """ Process a patch through the neural network and extract the predictions

    Args:
        images (array list): list of images to process (length = batch_size)
        net (obj): the Caffe Net
        transformer (obj): the Caffe Transformer for preprocessing
    """
    # caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
    # but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
    data = np.zeros(net.blobs['data'].data.shape)
    for i in range(len(images)):
        data[i] = transformer.preprocess('data', img_as_float(images[i]))
    net.forward(data=data)
    output = net.blobs['conv1_1_D'].data[:len(images)]
    output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
    return output
项目:TF-Examples    作者:CharlesShang    | 项目源码 | 文件源码
def load_img(self, img_filename):
        """
        Summary:
            Load image from the filename. Default is to load in color if
            possible.

        Args:
            img_name (string): string of the image name, relative to
                the image directory.

        Returns:
            np array of float32: an image as a numpy array of float32
        """
        if not img_filename.endswith('.jpg'):
            img_filename = os.path.join(self.img_dir, img_filename + '.jpg')
        else:
            img_filename = os.path.join(self.img_dir, img_filename)
        img = skimage.img_as_float(io.imread(
            img_filename)).astype(np.float32)
        if img.ndim == 2:
            img = img[:, :, np.newaxis]
        elif img.shape[2] == 4:
            img = img[:, :, :3]
        return img
项目:neuralmonkey    作者:ufal    | 项目源码 | 文件源码
def crop_image(x, target_height=227, target_width=227):
    image = skimage.img_as_float(skimage.io.imread(x)).astype(np.float32)

    if len(image.shape) == 2:
        image = np.tile(image[:,:,None], 3)
    elif len(image.shape) == 4:
        image = image[:,:,:,0]

    height, width, rgb = image.shape
    if width == height:
        resized_image = skimage.transform.resize(image, (target_height,target_width))

    elif height < width:
        resized_image = skimage.transform.resize(image, (int(width * float(target_height)/height), target_width))
        cropping_length = int((resized_image.shape[1] - target_height) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = skimage.transform.resize(image, (target_height, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_width) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return skimage.transform.resize(resized_image, (target_height, target_width))
项目:ml-traffic    作者:Zepheus    | 项目源码 | 文件源码
def postprocess(imgs, size, grayscale=False):
    print("Postprocessing images and resize (at %d)" % size)
    keyname = ('gray_%d' if grayscale else 'color_%d') % size
    for img in imgs:

        # Continue if already calculated
        if img.isSetByName(keyname):
            continue

        floatimg = img_as_float(img.image)
        floatimg = resize(floatimg, (size, size))
        if grayscale:
            floatimg = rgb2gray(floatimg)
        img.setByName(keyname, floatimg)  # expect to return floats


# Augment images
项目:dcn.tf    作者:beopst    | 项目源码 | 文件源码
def load_data(src,shuffle=True):
    """ Load data from directories.
    """

    imgs = [img for img in glob.glob(os.path.join(src,'*.png'))]

    x = np.zeros((len(imgs),100,100), dtype=np.float32)
    y = np.zeros(len(imgs), dtype=np.int64)

    for idx, img in enumerate(imgs):
        im = io.imread(img,1)
        im = img_as_float(im) # rescale from [0,255] to [0,1]

        label = int(img.split('/')[-1].split('.')[0].split('_')[-1])

        x[idx] = im
        y[idx] = label

    x = np.expand_dims(x,3)
    data = zip(x,y)

    if shuffle: random.shuffle(data)

    return data
项目:nn-segmentation-for-lar    作者:cvdlab    | 项目源码 | 文件源码
def predict_image(self, test_img):
        """
        predicts classes of input image
        :param test_img: filepath to image to predict on
        :param show: displays segmentation results
        :return: segmented result
        """
        img = np.array( rgb2gray( imread( test_img ).astype( 'float' ) ).reshape( 5, 216, 160 )[-2] ) / 256

        plist = []

        # create patches from an entire slice
        img_1 = adjust_sigmoid( img ).astype( float )
        edges_1 = adjust_sigmoid( img, inv=True ).astype( float )
        edges_2 = img_1
        edges_5_n = normalize( laplace( img_1 ) )
        edges_5_n = img_as_float( img_as_ubyte( edges_5_n ) )

        plist.append( extract_patches_2d( edges_1, (23, 23) ) )
        plist.append( extract_patches_2d( edges_2, (23, 23) ) )
        plist.append( extract_patches_2d( edges_5_n, (23, 23) ) )
        patches = np.array( zip( np.array( plist[0] ), np.array( plist[1] ), np.array( plist[2] ) ) )

        # predict classes of each pixel based on model
        full_pred = self.model.predict_classes( patches )
        fp1 = full_pred.reshape( 194, 138 )
        return fp1
项目:sca-cnn    作者:zjuchenlong    | 项目源码 | 文件源码
def crop_image(x, target_height=227, target_width=227):
    print x
    # skimage.img_as_float convert image np.ndarray into float type, with range (0, 1)
    image = skimage.img_as_float(skimage.io.imread(x)).astype(np.float32)

    if image.ndim == 2:
        image = image[:,:,np.newaxis][:,:,[0,0,0]]  # convert the gray image to rgb image

    height, width, rgb = image.shape
    if width == height:
        resized_image = cv2.resize(image, (target_width,target_height))

    elif height < width:
        resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_height))
        cropping_length = int((resized_image.shape[1] - target_width) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = cv2.resize(image, (target_width, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_height) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return cv2.resize(resized_image, (target_width, target_height))
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def test():
    test_filename = sys.argv[2]
    test_image = scipy.misc.imread(test_filename, flatten=True)
    test_image = scipy.misc.imresize(test_image, [background_height, background_width])
    test_image = skimage.img_as_float(test_image).astype(np.float32)

    model.load("find_kanji.tflearn")
    Y = model.predict(test_image.reshape([-1, background_height, background_width]))[0]

    print(Y)

    masked = np.square(Y)
    masked = scipy.misc.imresize(masked, [background_height, background_width])
    masked = test_image * masked
    scipy.misc.imsave("y.png", masked)

    #Y_indices = np.argsort(Y)[::-1]
项目:lipnet    作者:grishasergei    | 项目源码 | 文件源码
def _read_image(self, image_name):
        """
        Read image from self._path_to_img and perform any necessary preparation
        :param image_name: string, image name, which is added to self._path_to_img
        :return: numpy 2d array
        """
        filename = image_name
        try:
            img = io.imread(filename)
        except IOError:
            return None
        img = img_as_float(img)
        if len(img.shape) > 2:
            img = img[:, :, 0]
        img = resize(img, (self._image_width, self._image_height))
        img = img.reshape((self._image_width, self._image_height, 1))
        return img
项目:lipnet    作者:grishasergei    | 项目源码 | 文件源码
def read_image(image_name, img_size=None):
        """
        Read image from file
        :param image_name: string, image full name including path
        :param img_size: tuple of two ints, optional, specify if you want to resize image
        :return: numpy 2d array
        """
        filename = image_name
        try:
            img = io.imread(filename)
        except IOError:
            return None
        img = img_as_float(img)
        if len(img.shape) > 2:
            img = img[:, :, 0]

        if img_size is not None:
            img = resize(img, (img_size[0], img_size[1]))
            img = img.reshape((1, img_size[0], img_size[1]))

        return img
项目:VQG-tensorflow    作者:JamesChuanggg    | 项目源码 | 文件源码
def crop_image(x, target_height=227, target_width=227, as_float=True):
    image = skimage.io.imread(x)
    if as_float:
        image = skimage.img_as_float(image).astype(np.float32)

    if len(image.shape) == 2:
        image = np.tile(image[:,:,None], 3)
    elif len(image.shape) == 4:
        image = image[:,:,:,0]

    height, width, rgb = image.shape
    if width == height:
        resized_image = cv2.resize(image, (target_height,target_width))

    elif height < width:
        resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
        cropping_length = int((resized_image.shape[1] - target_height) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_width) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return cv2.resize(resized_image, (target_height, target_width))
项目:tanda    作者:HazyResearch    | 项目源码 | 文件源码
def load_cifar10_batch(fpath, one_hot=True, as_float=True):
    with open(fpath, 'rb') as f:
        # https://stackoverflow.com/questions/11305790
        data = cPickle.load(f, encoding='latin1')
        X = np.copy(data['data']).reshape(-1, 32*32, 3, order='F')
        X = X.reshape(-1, 32, 32, 3)
        Y = np.array(data['labels'])

        # Convert labels to one hot
        if one_hot:
            Y = to_one_hot(Y)

        # CONVERT TO FLOAT [0,1] TYPE HERE to be consistent with skimage TFs!!!
        # See: http://scikit-image.org/docs/dev/user_guide/data_types.html
        if as_float:
            X = img_as_float(X)
    return X, Y
项目:livespin    作者:biocompibens    | 项目源码 | 文件源码
def findMaximaOnFG(self, param):
        self.defineFG(param)
        #self.smooth_corr()
        self.coorExtract = [0, 0]
        xmin, ymin = self.coorExtract


        img=self.candi
        img [self.FG ==0] =0

        im = img_as_float(img)
        image_max = ndimage.maximum_filter(im, size=10, mode='constant')
        coordinates = peak_local_max(im, min_distance=10)

        tep=np.zeros(self.candi.shape)
        for i,ide in enumerate(coordinates):
            tep[ide[0],ide[1]] = self.candi[ide[0],ide[1]]
        lbl = ndimage.label(tep)[0]
        centerc = np.round(ndimage.measurements.center_of_mass(tep, lbl, range(1,np.max(lbl)+1)))
        if centerc.size > 0:
            self.centersX = centerc[:,0].astype(int)
            self.centersY = centerc[:,1].astype(int)
        self.nComponents = len(self.centersX)
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_rgb2hsv_conversion(self):
        rgb = img_as_float(self.img_rgb)[::16, ::16]
        hsv = rgb2hsv(rgb).reshape(-1, 3)
        # ground truth from colorsys
        gt = np.array([colorsys.rgb_to_hsv(pt[0], pt[1], pt[2])
                       for pt in rgb.reshape(-1, 3)]
                      )
        assert_almost_equal(hsv, gt)
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_xyz_rgb_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(xyz2rgb(rgb2xyz(img_rgb)), img_rgb)

    # RGB<->HED roundtrip with ubyte image
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_hed_rgb_float_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(hed2rgb(rgb2hed(img_rgb)), img_rgb)

    # RGB<->HDX roundtrip with ubyte image
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_hdx_rgb_roundtrip(self):
        from skimage.color.colorconv import hdx_from_rgb, rgb_from_hdx
        img_rgb = img_as_float(self.img_rgb)
        conv = combine_stains(separate_stains(img_rgb, hdx_from_rgb),
                              rgb_from_hdx)
        assert_array_almost_equal(conv, img_rgb)

    # RGB to RGB CIE
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_lab_rgb_roundtrip(self):
        img_rgb = img_as_float(self.img_rgb)
        assert_array_almost_equal(lab2rgb(rgb2lab(img_rgb)), img_rgb)

    # test matrices for xyz2luv and luv2xyz generated using
    # http://www.easyrgb.com/index.php?X=CALC
    # Note: easyrgb website displays xyz*100
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_lab_lch_roundtrip(self):
        rgb = img_as_float(self.img_rgb)
        lab = rgb2lab(rgb)
        lab2 = lch2lab(lab2lch(lab))
        assert_array_almost_equal(lab2, lab)
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_rgb_lch_roundtrip(self):
        rgb = img_as_float(self.img_rgb)
        lab = rgb2lab(rgb)
        lch = lab2lch(lab)
        lab2 = lch2lab(lch)
        rgb2 = lab2rgb(lab2)
        assert_array_almost_equal(rgb, rgb2)
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def _get_lab0(self):
        rgb = img_as_float(self.img_rgb[:1, :1, :])
        return rgb2lab(rgb)[0, 0, :]
项目:FCN_train    作者:315386775    | 项目源码 | 文件源码
def test_each_channel():
    filtered = edges_each(COLOR_IMAGE)
    for i, channel in enumerate(np.rollaxis(filtered, axis=-1)):
        expected = img_as_float(filters.sobel(COLOR_IMAGE[:, :, i]))
        assert_allclose(channel, expected)
项目:KagglePlanetPytorch    作者:Mctigger    | 项目源码 | 文件源码
def to_float(x):
    return img_as_float(x)
项目:skan    作者:jni    | 项目源码 | 文件源码
def _normalise_image(image, *, image_cmap=None):
    image = img_as_float(image)
    if image.ndim == 2:
        if image_cmap is None:
            image = gray2rgb(image)
        else:
            image = plt.get_cmap(image_cmap)(image)[..., :3]
    return image
项目:DeepNet    作者:hok205    | 项目源码 | 文件源码
def create_image_lmdb(target, samples, bgr=BGR, normalize=False):
    """Create an image LMDB

    Args:
        target (str): path of the LMDB to be created
        samples (array list): list of images to be included in the LMDB
        bgr (bool): True if we want to reverse the channel order (RGB->BGR)
        normalize (bool): True if we want to normalize data in [0,1]
    """

    # Open the LMDB
    if os.path.isdir(target):
        raise Exception("LMDB already exists in {}, aborted.".format(target))
    db = lmdb.open(target, map_size=int(1e12))
    with db.begin(write=True) as txn:
        for idx, sample in tqdm(enumerate(samples), total=len(samples)):
            sample = io.imread(sample)
            # load image:
            if normalize:
                # - in [0,1.]float range
                sample = img_as_float(sample)
            if bgr:
                # - in BGR (reverse from RGB)
                sample = sample[:,:,::-1]
            # - in Channel x Height x Width order (switch from H x W x C)
            sample = sample.transpose((2,0,1))
            datum = caffe.io.array_to_datum(sample)
            # Write the data into the db
            txn.put('{:0>10d}'.format(idx), datum.SerializeToString())

    db.close()
项目:image_captioning    作者:bityangke    | 项目源码 | 文件源码
def check_files(image_dir):
    print("Checking image files in %s" %(image_dir))
    files = os.listdir(image_dir)
    images = [os.path.join(image_dir, f) for f in files if f.lower().endswith('.jpg')]
    good_imgs = []
    for img in images:
        try:
           x = skimage.img_as_float(skimage.io.imread(img)).astype(np.float32)
           good_imgs.append(img)
        except:
           print("Image %s is corrupted and will be removed." %(img))
           os.remove(img)
    good_files = [img.split(os.sep)[-1] for img in good_imgs]
    return good_files
项目:image_captioning    作者:bityangke    | 项目源码 | 文件源码
def load_image(self, image_dir):
        image = skimage.img_as_float(skimage.io.imread(image_dir)).astype(np.float32)
        assert image.ndim == 2 or image.ndim == 3
        if image.ndim == 2:
            image = image[:, :, np.newaxis]
            image = np.tile(image, (1, 1, 3))
        elif image.shape[2] > 3:
            image = image[:, :, :3]
        return image
项目:CancerImageAnalyzer2    作者:byeungchun    | 项目源码 | 文件源码
def loadRawImage(_rawImageFile="null"):
    if _rawImageFile == "null":
        #rawImageFile = "BF_position020200_time0001.tif"
        _rawImageFile = "./test1.tif"
    global rawImageFile
    rawImageFile =  _rawImageFile
    rawImg = imread(_rawImageFile)
    rawImgFloat = skimage.img_as_float(rawImg)
    return rawImgFloat
项目:CancerImageAnalyzer2    作者:byeungchun    | 项目源码 | 文件源码
def loadPreprocessingImage(preprocessingFile):
    preprocessingImg = imread(preprocessingFile)
    preprocessingImg = skimage.img_as_float(preprocessingImg)
    return preprocessingImg
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def main():
    if Display:
        cap = cv2.VideoCapture(0)
        out = cv2.VideoWriter('output.avi', -1, 2.0, (600, 440))
        count = 0

        while(count < 20):
            ret, frame1 = cap.read()
            ret, frame2 = cap.read()

            frame1 = skimage.img_as_float(frame1)
            frame2 = skimage.img_as_float(frame2)

            output_img = optical_flow_ssd(frame1, frame2)
            out.write(output_img)

            cv2.imshow('frame', output_img)
            count = count + 1

            if cv2.waitKey(1) & 0xFF == ord('q'):
                cap.release()
                out.release()
                cv2.destroyAllWindows()
                break
    else:
        test()
项目:vizgen    作者:uva-graphics    | 项目源码 | 文件源码
def read_img(in_filename, grayscale=False, extra_info={}):
    """Returns the image saved at in_filename as a numpy array.

    If grayscale is True, converts from 3D RGB image to 2D grayscale image.
    """
    if is_pypy:
        ans = Image.open(in_filename)
        height = ans.height
        width = ans.width
        channels = len(ans.getbands())
        if ans.mode == 'I':
            numpy_mode = 'uint32'
            maxval = 65535.0
        elif ans.mode in ['L', 'RGB', 'RGBA']:
            numpy_mode = 'uint8'
            maxval = 255.0
        else:
            raise ValueError('unknown mode')
        ans = numpy.fromstring(ans.tobytes(), numpy_mode).reshape((height, width, channels))
        ans = ans/maxval
        if grayscale and (len(ans.shape) == 3 and ans.shape[2] == 3):
            ans = ans[:,:,0]*0.2125 + ans[:,:,1]*0.7154 + ans[:,:,2]*0.0721
        if len(ans.shape) == 3 and ans.shape[2] == 1:
            ans = ans[:,:,0]
        return ans
    else:
        ans = skimage.io.imread(in_filename)
        if ans.dtype == numpy.int32:    # Work around scikit-image bug #1680
            ans = numpy.asarray(ans, numpy.uint16)
        ans = skimage.img_as_float(ans)
        if grayscale:
            ans = skimage.color.rgb2gray(ans)
#        print('here', use_4channel, len(ans.shape) == 3, ans.shape[2] == 3)
        if use_4channel and len(ans.shape) == 3 and ans.shape[2] == 3:
            ans = numpy.dstack((ans,) + (numpy.ones((ans.shape[0], ans.shape[1], 1)),))
            extra_info['originally_3channel'] = True
    return ans
项目:Seam-Carving-using-DP    作者:arpitmathur89    | 项目源码 | 文件源码
def remove_seam(img, seam):
    img = img_as_float(img)
    img = img.tolist()
    seam = seam.tolist()
    for i in range(0, len(img)):
        del img[i][seam[i]]
    plt.imshow(img)
    print "Width of the image is: ", len(img[0])
    return img
项目:Seam-Carving-using-DP    作者:arpitmathur89    | 项目源码 | 文件源码
def remove_multiple_pixels(img, count):
    for i in range(0, count):
        img = remove_seam(img, find_seam(img))
        img = img_as_float(img)
    plt.imshow(img)
项目:mcv-m5    作者:david-vazquez    | 项目源码 | 文件源码
def my_label2rgboverlay(labels, colors, image, bglabel=None,
                        bg_color=(0., 0., 0.), alpha=0.2):
    image_float = gray2rgb(img_as_float(rgb2gray(image)))
    label_image = my_label2rgb(labels, colors, bglabel=bglabel,
                               bg_color=bg_color)
    output = image_float * alpha + label_image * (1 - alpha)
    return output


# Save 3 images (Image, mask and result)
项目:clitorisvulgaris    作者:fhoehl    | 项目源码 | 文件源码
def find_dominant_colors(image):
    """Cluster the colors of the image in CLUSTER_NUMBER of clusters. Returns
    an array of dominant colors reverse sorted by cluster size.
    """

    array = img_as_float(fromimage(image))

    # Reshape from MxNx4 to Mx4 array
    array = array.reshape(scipy.product(array.shape[:2]), array.shape[2])

    # Remove transparent pixels if any (channel 4 is alpha)
    if array.shape[-1] > 3:
        array = array[array[:, 3] == 1]

    # Finding centroids (centroids are colors)
    centroids, _ = kmeans(array, CLUSTER_NUMBER)

    # Allocate pixel to a centroid cluster
    observations, _ = vq(array, centroids)

    # Calculate the number of pixels in a cluster
    histogram, _ = scipy.histogram(observations, len(centroids))

    # Sort centroids by number of pixels in their cluster
    sorted_centroids = sorted(zip(centroids, histogram),
                              key=lambda x: x[1],
                              reverse=True)

    sorted_colors = tuple((couple[0] for couple in sorted_centroids))

    return sorted_colors
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def fix_image(image_filename):
    image = scipy.misc.imread(test_filenames[i], flatten=False)
    image = scipy.misc.imresize(image, [image_size, image_size])
    image = skimage.img_as_float(image)
    image = np.swapaxes(image, 0, 2)
    image = np.swapaxes(image, 1, 2)    
    return image
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def augment_kanji(kanji, augmentation_idx):
    angle = np.random.randint(0,360) * (np.pi / 180.0)
    dist = np.random.randint(0,aug_translation_max_dist)
    x = int(math.cos(angle) * dist)
    y = int(math.sin(angle) * dist)


    augmented = np.roll(kanji, [y, x], axis=[0, 1])

    #angle_step = (np.pi * 2.0) / float(num_augmentations+1)
    #angle = angle_step + (angle_step * float(augmentation_idx))
    #angle *= (180.0 / np.pi) # degrees
    rot_angle = np.random.randint(-2, 2)
    augmented = scipy.misc.imrotate(augmented, rot_angle, interp="bilinear")

    pad_max = 12
    pad_w = np.random.randint(0, pad_max)
    pad_h = pad_w
    augmented = np.pad(augmented, ((pad_h, pad_h), (pad_w, pad_w)), mode="constant")
    augmented = scipy.misc.imresize(augmented, [kanji_height, kanji_width])

    augmented = skimage.img_as_float(augmented).astype(np.float32)

    noise = np.random.uniform(low=0.1, high=0.5)
    augmented += np.random.uniform(low=-noise, high=noise, size=augmented.shape)
    augmented = np.maximum(0.0, np.minimum(augmented, 1.0))

    return augmented
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def rasterize_kanji(kanji, weight, output_file):
    kanji = kanji[0:4] # strip extra stuff like footnotes off kanji

    prop = fm.FontProperties(fname="ipam.ttc", size=70)

    plt.figure(figsize=(1, 1))
    plt.text(0, 0, kanji, ha='center', va='center', fontproperties=prop)
    plt.xlim(-0.4, 0.1)
    plt.ylim(-0.1, 0.1)
    plt.axis("off")

    #plt.savefig(output_file)
    #image = scipy.misc.imread(output_file, flatten=True)

    buf = io.BytesIO()
    plt.savefig(buf, format="png")
    buf.seek(0)
    image = PIL.Image.open(buf).convert(mode="L")
    buf.close()
    image = np.asarray(image, dtype=np.uint8)
    plt.close()

    image = scipy.misc.imresize(image, [kanji_height, kanji_width])
    image = skimage.img_as_float(image).astype(np.float32)

    image = 1.0 - image # make the background black and the text white

    if (weight == "bold"):
        erosion_size = 5
    elif (weight == "normal"):
        erosion_size = 3
    else:
        erosion_size = 0

    if (erosion_size > 0):
        kernel = np.ones((erosion_size, erosion_size), np.float32)
        image = cv2.dilate(image, kernel, iterations=1)
        scipy.misc.imsave(output_file, (1.0 - image))

    return image
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def dump_heatmaps(filename, images, heatmaps, antialias=True, multiply=True):
    images = images.numpy()
    heatmaps = heatmaps.numpy()
    all_masked = np.zeros(images.shape)
    num_images = images.shape[0]
    for i in range(num_images):
        image = images[i] / 255.0
        heatmap = heatmaps[i]

        # resize the heatmap to be the same size as the image
        if (antialias):
            interp = "bilinear"
        else:
            interp = "nearest"
        heatmap = img_as_float(scipy.misc.imresize(heatmap, [image.shape[1], image.shape[2]], interp=interp))

        # tile the heatmap in each component so it's HxWx3 like the image
        heatmap = heatmap.reshape(1, heatmap.shape[0], heatmap.shape[1])
        heatmap = np.tile(heatmap, (3,1,1))

        # mask the image by the heatmap
        if multiply:
            masked = image * heatmap
        else:
            masked = image + heatmap

        all_masked[i] = masked

    vutils.save_image(torch.FloatTensor(all_masked), filename, normalize=True)
项目:MachineLearning    作者:timomernick    | 项目源码 | 文件源码
def augment_image(image, augmentation_idx):
    image_height = image.shape[0]
    image_width = image.shape[1]

    if (image_width > hr_size):
        cropX = np.random.randint(0, image_width - hr_size)
    else:
        cropX = 0

    if (image_height > hr_size):
        cropY = np.random.randint(0, image_height - hr_size)
    else:
        cropY = 0

    hr_image = image[cropY:cropY+hr_size,cropX:cropX+hr_size,...]

    lr_image = scipy.misc.imresize(hr_image, [lr_size, lr_size], interp="bilinear")

    # scale low res back to high res so we can learn something other than scaling up the input
    lr_scaled = scipy.misc.imresize(lr_image, [hr_size, hr_size], interp="bilinear")

    # resizing changes to int, go back to float
    lr_image = skimage.img_as_float(lr_image)
    lr_scaled = skimage.img_as_float(lr_scaled)

    #scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + ".png", hr_image)
    #scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + "_small_.png", lr_image)
    #scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + "_scaled_.png", lr_scaled)    

    lr_image = np.swapaxes(lr_image, 0, 2)
    lr_image = np.swapaxes(lr_image, 1, 2)

    hr_image = np.swapaxes(hr_image, 0, 2)
    hr_image = np.swapaxes(hr_image, 1, 2)

    lr_scaled = np.swapaxes(lr_scaled, 0, 2)
    lr_scaled = np.swapaxes(lr_scaled, 1, 2)

    return lr_image, lr_scaled, hr_image
项目:SceneUnderstanding_CIARP_2017    作者:verlab    | 项目源码 | 文件源码
def read_caffe_img(path):
    return skimage.img_as_float(skimage.io.imread(path)).astype(np.float32).copy()
项目:keras_zoo    作者:david-vazquez    | 项目源码 | 文件源码
def my_label2rgboverlay(labels, colors, image, bglabel=None,
                        bg_color=(0., 0., 0.), alpha=0.2):
    image_float = gray2rgb(img_as_float(rgb2gray(image)))
    label_image = my_label2rgb(labels, colors, bglabel=bglabel,
                               bg_color=bg_color)
    output = image_float * alpha + label_image * (1 - alpha)
    return output


# Save 3 images (Image, mask and result)
项目:Handwritten_recognition_tensorflow    作者:sanjanaramprasad    | 项目源码 | 文件源码
def crop_image(x, target_height=227, target_width=227, as_float=True):
    image = skimage.io.imread(x)
    if as_float:
        image = skimage.img_as_float(image).astype(np.float32)

    print(len(image.shape))
    if len(image.shape) == 2:
        image = np.tile(image[:, :, None], 3)
    elif len(image.shape) == 4:
        image = image[:, :, :, 0]

    height, width, rgb = image.shape
    if width == height:
        resized_image = np.resize(image, (target_height,target_width))

    elif height < width:
        resized_image = np.resize(image, (int(width * float(target_height)/height), target_width))
        cropping_length = int((resized_image.shape[1] - target_height) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = np.resize(image, (target_height, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_width) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return np.resize(resized_image, (target_height, target_width))
项目:dataset_loaders    作者:fvisin    | 项目源码 | 文件源码
def my_label2rgboverlay(labels, cmap, image, bglabel=None,
                        bg_color=(0., 0., 0.), alpha=0.2):
    '''Superimpose a mask over an image

    Convert a label mask to RGB applying a color map and superimposing it
    over an image as a transparent overlay'''
    image_float = gray2rgb(img_as_float(rgb2gray(image)))
    label_image = my_label2rgb(labels, cmap, bglabel=bglabel,
                               bg_color=bg_color)
    output = image_float * alpha + label_image * (1 - alpha)
    return output
项目:tanda    作者:HazyResearch    | 项目源码 | 文件源码
def pil_to_np(x):
    """Converts from PIL.Image to np float format image"""
    x = np.asarray(x)
    if len(x.shape) == 2: 
        x = x[:,:,np.newaxis]
    return img_as_float(np.asarray(x))
项目:CVtools    作者:Tyler-D    | 项目源码 | 文件源码
def oversample(img, crop_ratio, horizon_flip=False, vertical_flip=False):
    """
    4 corner crops , horizonly flips and vertically flips
    Parameter:
    -------------
    img: h*w*c image; ndarray, dtype= np.float32
    crop_ratio: (crop_height_ratio, crop_width_ratio); tuple
    horizon_flip: flip the image horizonly;bool
    vertical_flip:

    Return:
    ------------
    crops: N*h*w*c images; ndarray
    """
    img_height = img.shape[0]
    img_width = img.shape[1]
    img_channel = img.shape[-1]
    crop_dims = (int(img_height * crop_ratio[0]), int(img_width * crop_ratio[1]))
    h_indices = (0, img_height - crop_dims[0])
    w_indices = (0, img_width - crop_dims[1])
    crop_idx = np.empty((4, 4), dtype=int)
    idx = 0
    for i in h_indices:
        for j in w_indices:
            #crop_idx : (ymin, xmin, ymax, xmax)
            crop_idx[idx] = (i, j, i + crop_dims[0], j + crop_dims[1])
            idx+=1
    num = 4
    if horizon_flip:
        num += 4
    if vertical_flip:
        num += 4
    crops = np.empty((num, crop_dims[0], crop_dims[1], img_channel), dtype=np.float32)
    idx = 0
    for crop in crop_idx:
        crops[idx] = img[crop[0]:crop[2], crop[1]:crop[3], :]
        img_crop = crops[idx]
        idx += 1
        if horizon_flip:
            crops[idx] = np.flipud(img_crop)
            idx += 1
        if vertical_flip:
            crops[idx] = np.fliplr(img_crop)
            idx += 1
    return crops


#test
# img = skimage.img_as_float(skimage.io.imread("./test.jpg")).astype(np.float32)
# # img = skimage.io.imread("./test.jpg")
# print img.shape
# print type(img)
# crops = oversample(img,(0.8,0.8),True,True)
# print crops.shape
# for idx in xrange(crops.shape[0]):
    # skimage.io.imsave(os.path.join("./", str(idx) + ".jpg"), crops[idx, :, :, :])
项目:heart-segm    作者:grihabor    | 项目源码 | 文件源码
def get_output(i):
    image = load_image('../../data/sbdd/dataset', 'img_{}'.format(i))
    res = net.forward()

    score = res['score_output2'].transpose((2, 3, 1, 0))
    label = res['label'].transpose((2, 3, 1, 0))
    print('score shape:', score.shape)
    print('label shape:', label.shape)
    score = score[:, :, :, 0]
    label = label[:, :, 0, 0]
    print(score.shape)
    print(label.shape, label.dtype)

    width = 3
    height = 2

    image = img_as_float(np.reshape(image[:, :, 0], image.shape[:2]))

    plt.subplot(height, width, 1)
    plt.title('target image')
    plt.imshow(image, cmap='gray')
    plt.subplot(height, width, 2)
    plt.title('network output\nlabel 0')
    plt.imshow(score[:, :, 0], cmap='gray')
    plt.subplot(height, width, 3)
    plt.title('network output\nlabel 1')
    plt.imshow(score[:, :, 1], cmap='gray')

    prob_threshold = 0.5
    binary_score = (score[:, :, 1] > prob_threshold).astype(np.float)

    plt.subplot(height, width, 4)
    plt.title('prob > {}'.format(prob_threshold))
    plt.imshow(binary_score, cmap='gray')

    print(image, image.shape)

    plt.subplot(height, width, 5)
    plt.title('image + output')
    plt.imshow(image + binary_score, cmap='gray')

    plt.subplot(height, width, 6)
    plt.title('image + grand truth')
    plt.imshow(image + label, cmap='gray')
    plt.savefig('output/img_{}.png'.format(i), bbox_inches='tight')