Python numpy.bitwise_and() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def draw_flow(img, flow, step=16):
    h, w = img.shape[:2]
    y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1)
    fx, fy = flow[y,x].T
    m = np.bitwise_and(np.isfinite(fx), np.isfinite(fy))
    lines = np.vstack([x[m], y[m], x[m]+fx[m], y[m]+fy[m]]).T.reshape(-1, 2, 2)
    lines = np.int32(lines + 0.5)
    vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    cv2.polylines(vis, lines, 0, (0, 255, 0))
    for (x1, y1), (x2, y2) in lines:
        cv2.circle(vis, (x1, y1), 1, (0, 255, 0), -1)
    return vis 

Example 2

def logpdf(self, samples):
        '''
        Calculates the log of the probability density function.

        Parameters
        ----------
        samples : array_like
            n-by-2 matrix of samples where n is the number of samples.

        Returns
        -------
        vals : ndarray
            Log of the probability density function evaluated at `samples`.
        '''
        samples = np.copy(np.asarray(samples))
        samples = self.__rotate_input(samples)
        inner = np.all(np.bitwise_and(samples > 0.0, samples < 1.0), axis=1)
        outer = np.invert(inner)
        vals = np.zeros(samples.shape[0])
        vals[inner] = self._logpdf(samples[inner, :])
        # Assign zero mass to border
        vals[outer] = -np.inf
        return vals 

Example 3

def get_rgb_mask(img, debug=False):
    assert isinstance(img, numpy.ndarray), 'image must be a numpy array'
    assert img.ndim == 3, 'skin detection can only work on color images'
    logger.debug('getting rgb mask')

    lower_thresh = numpy.array([45, 52, 108], dtype=numpy.uint8)
    upper_thresh = numpy.array([255, 255, 255], dtype=numpy.uint8)

    mask_a = cv2.inRange(img, lower_thresh, upper_thresh)
    mask_b = 255 * ((img[:, :, 2] - img[:, :, 1]) / 20)
    mask_c = 255 * ((numpy.max(img, axis=2) - numpy.min(img, axis=2)) / 20)
    mask_d = numpy.bitwise_and(numpy.uint64(mask_a), numpy.uint64(mask_b))
    # mask = numpy.zeros_like(mask_d, dtype=numpy.uint8)
    msk_rgb = numpy.bitwise_and(numpy.uint64(mask_c), numpy.uint64(mask_d))
    # msk_rgb = cv2.fromarray(mask_rgb)
    msk_rgb[msk_rgb < 128] = 0
    msk_rgb[msk_rgb >= 128] = 1

    if debug:
        scripts.display('input', img)
        scripts.display('mask_rgb', msk_rgb)

    return msk_rgb.astype(float) 

Example 4

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 5

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 6

def agent_start(self, observation):

        # Preprocess
        tmp = np.bitwise_and(np.asarray(observation.intArray[128:]).reshape([210, 160]), 0b0001111)  # Get Intensity from the observation
        obs_array = (spm.imresize(tmp, (110, 84)))[110-84-8:110-8, :]  # Scaling

        # Initialize State
        self.state = np.zeros((4, 84, 84), dtype=np.uint8)
        self.state[0] = obs_array
        state_ = cuda.to_gpu(np.asanyarray(self.state.reshape(1, 4, 84, 84), dtype=np.float32))

        # Generate an Action e-greedy
        returnAction = Action()
        action, Q_now = self.DDQN.e_greedy(state_, self.epsilon)
        returnAction.intArray = [action]

        # Update for next step
        self.lastAction = copy.deepcopy(returnAction)
        self.last_state = self.state.copy()
        self.last_observation = obs_array

        return returnAction 

Example 7

def create_mask_from_bitmask(geoimg, filename=''):
    """ Mask geoimg with a series of provided bitmasks """
    # medium and high confidence clouds
    nodata = int('0000000000000001', 2)
    clouds = int('1000000000000000', 2)
    cirrus = int('0011000000000000', 2)

    # calculate mask
    arr = geoimg.read().astype('int16')
    # it is a good data mask
    mask = (np.bitwise_and(arr, nodata) != nodata) & \
           (np.bitwise_and(arr, clouds) < clouds) & \
           (np.bitwise_and(arr, cirrus) < cirrus)

    # create mask file
    logger.info('Saving to file %s' % filename, action='Save file', actee=filename, actor=__name__)
    maskimg = GeoImage.create_from(geoimg, filename=filename, dtype='uint8')
    maskimg.set_nodata(0)
    maskimg[0].write(mask.astype('uint8'))

    return maskimg 

Example 8

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 9

def test_values(self):
        for dt in self.bitwise_types:
            zeros = np.array([0], dtype=dt)
            ones = np.array([-1], dtype=dt)
            msg = "dt = '%s'" % dt.char

            assert_equal(np.bitwise_not(zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_not(ones), zeros, err_msg=msg)

            assert_equal(np.bitwise_or(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_or(zeros, ones), ones, err_msg=msg)
            assert_equal(np.bitwise_or(ones, zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_or(ones, ones), ones, err_msg=msg)

            assert_equal(np.bitwise_xor(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_xor(zeros, ones), ones, err_msg=msg)
            assert_equal(np.bitwise_xor(ones, zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_xor(ones, ones), zeros, err_msg=msg)

            assert_equal(np.bitwise_and(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(zeros, ones), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(ones, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(ones, ones), ones, err_msg=msg) 

Example 10

def set_ufunc(self, scalar_op):
        # This is probably a speed up of the implementation
        if isinstance(scalar_op, theano.scalar.basic.Add):
            self.ufunc = numpy.add
        elif isinstance(scalar_op, theano.scalar.basic.Mul):
            self.ufunc = numpy.multiply
        elif isinstance(scalar_op, theano.scalar.basic.Maximum):
            self.ufunc = numpy.maximum
        elif isinstance(scalar_op, theano.scalar.basic.Minimum):
            self.ufunc = numpy.minimum
        elif isinstance(scalar_op, theano.scalar.basic.AND):
            self.ufunc = numpy.bitwise_and
        elif isinstance(scalar_op, theano.scalar.basic.OR):
            self.ufunc = numpy.bitwise_or
        elif isinstance(scalar_op, theano.scalar.basic.XOR):
            self.ufunc = numpy.bitwise_xor
        else:
            self.ufunc = numpy.frompyfunc(scalar_op.impl, 2, 1) 

Example 11

def parseNpf(self, buffer, imageWidth, imageHeight):
        # Read the header
        sectionLengths = self._readUgarHeader(buffer)
        # Read the palette data (section number 1)
        paletteData = np.frombuffer(buffer.read(roundToPower(sectionLengths[0])), dtype=np.uint16)
        # Read the image data (section number 2)
        imageData = np.frombuffer(buffer.read(sectionLengths[1]), dtype=np.uint8)
        # NPF image data uses 1 byte per 2 pixels, so we need to split that byte into two
        imageData = np.stack((np.bitwise_and(imageData, 0x0f), np.bitwise_and(imageData >> 4, 0x0f)), axis=-1).flatten()
        # Unpack palette colors
        palette = unpackColors(paletteData, useAlpha=False)
        # Convert each pixel from a palette index to full color
        pixels = np.fromiter((palette[i] if i > 0 else 0 for i in imageData), dtype=">u4")
        # Clip the image data and create a Pillow image from it
        return Image.fromarray(self._clipImageData(pixels, (imageWidth, imageHeight)), mode="RGBA")

    # Write the image as an npf to buffer 

Example 12

def __init__(self, imageBuffer, size):
        width, height = self.get_size(size)
        self.size = size
        self.surface = Surface((width, height), depth=8)
        # Read the header
        paletteLength, imageDataLength = self.read_ugar_header(imageBuffer)
        # Read the image palette and unpack
        palette = self.unpack_palette(np.fromstring(imageBuffer.read(self.round_to_power(paletteLength)), dtype=np.uint16))
        self.surface.set_palette(palette)
        # All pixels with the index of 0 are transparent
        self.surface.set_colorkey(0)
        # Read the pixel data bytes
        pixelData = np.fromstring(imageBuffer.read(imageDataLength), dtype=np.uint8)
        # Split each byte into 2 pixels
        pixels = np.stack((np.bitwise_and(pixelData, 0x0f), np.bitwise_and(pixelData >> 4, 0x0f)), axis=-1).flatten()
        pixels = np.swapaxes(np.reshape(pixels, (-1, width)), 0, 1)
        pixelcopy.array_to_surface(self.surface, pixels) 

Example 13

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 14

def test_uw_rgbd_object(): 
    from pybot.vision.image_utils import to_color
    from pybot.vision.imshow_utils import imshow_cv

    object_directory = '~/data/rgbd_datasets/udub/rgbd-object-crop/rgbd-dataset'
    rgbd_data_uw = UWRGBDObjectDataset(directory=object_directory)

    for f in rgbd_data_uw.iteritems(every_k_frames=5): 
        bbox = f.bbox
        imshow_cv('frame', 
                  np.hstack([f.img, np.bitwise_and(f.img, to_color(f.mask))]), 
                  text='Image + Mask [Category: [%i] %s, Instance: %i]' % 
                  (bbox['category'], rgbd_data_uw.get_category_name(bbox['category']), bbox['instance']))
        imshow_cv('depth', (f.depth / 16).astype(np.uint8), text='Depth') 

Example 15

def project(self, X, check_bounds=False, check_depth=False, return_depth=False, min_depth=0.1):
        """
        Project [Nx3] points onto 2-D image plane [Nx2]
        """
        R, t = self.to_Rt()
	rvec,_ = cv2.Rodrigues(R)
	proj,_ = cv2.projectPoints(X, rvec, t, self.K, self.D)
        x = proj.reshape(-1,2)

        if check_depth: 
            # Only return positive depths
            depths = self.depth_from_projection(X)
            valid = depths >= min_depth
        else:
            valid = np.ones(len(x), dtype=np.bool)
        
        if check_bounds: 
            if self.shape is None: 
                raise ValueError('check_bounds cannot proceed. Camera.shape is not set')
        
            # Only return points within-image bounds
            valid = np.bitwise_and(
                valid, np.bitwise_and(
                    np.bitwise_and(x[:,0] >= 0, x[:,0] < self.shape[1]), \
                    np.bitwise_and(x[:,1] >= 0, x[:,1] < self.shape[0]))
            )

        if return_depth: 
            return x[valid], depths[valid]

        return x[valid] 

Example 16

def get_bounded_projection(camera, pts, subsample=10): 
    """ Project points and only return points that are within image bounds """

    # Project points
    pts2d = camera.project(pts[::subsample].astype(np.float32))

    # Only return points within-image bounds
    valid = np.bitwise_and(np.bitwise_and(pts2d[:,0] >= 0, pts2d[:,0] < camera.shape[1]), \
                           np.bitwise_and(pts2d[:,1] >= 0, pts2d[:,1] < camera.shape[0]))
    return pts2d[valid], valid 

Example 17

def bbox_inbounds(bb, shape): 
    assert(shape[1] > shape[0] and bb.shape[1] == 4)
    return np.all(np.bitwise_and(np.bitwise_and(bb[:,0] >= 0, bb[:,2] < shape[1]), \
                                 np.bitwise_and(bb[:,1] >= 0, bb[:,3] < shape[0]))) 

Example 18

def match_targets(bboxes_truth, bboxes_test, intersection_th=0.5): 
    A = brute_force_match_coords(bboxes_truth, bboxes_test)
    B = brute_force_match_target(bboxes_truth, bboxes_test)
    pos = np.bitwise_and(A > intersection_th, B)
    return pos 

Example 19

def finite_and_within_bounds(xys, shape): 
    H, W = shape[:2]
    if not len(xys): 
        return np.array([])
    return np.bitwise_and(np.isfinite(xys).all(axis=1), 
                          reduce(lambda x,y: np.bitwise_and(x,y), [xys[:,0] >= 0, xys[:,0] < W, 
                                                                   xys[:,1] >= 0, xys[:,1] < H])) 

Example 20

def detect(self, im, mask=None): 
        edges = self.detect_edges_(im)
        # edges1 = edges.copy()
        if mask is not None: 
            edges = np.bitwise_and(edges, mask)
        # cv2.imshow('mask', np.hstack([edges1, edges]))
        kpts = self.detector_.detect(im, mask=edges)
        return kpts 

Example 21

def getCrop(self, dpt, xstart, xend, ystart, yend, zstart, zend, thresh_z=True):
        """
        Crop patch from image
        :param dpt: depth image to crop from
        :param xstart: start x
        :param xend: end x
        :param ystart: start y
        :param yend: end y
        :param zstart: start z
        :param zend: end z
        :param thresh_z: threshold z values
        :return: cropped image
        """
        if len(dpt.shape) == 2:
            cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1])].copy()
            # add pixels that are out of the image in order to keep aspect ratio
            cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
                                           abs(yend)-min(yend, dpt.shape[0])),
                                          (abs(xstart)-max(xstart, 0),
                                           abs(xend)-min(xend, dpt.shape[1]))), mode='constant', constant_values=0)
        elif len(dpt.shape) == 3:
            cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1]), :].copy()
            # add pixels that are out of the image in order to keep aspect ratio
            cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
                                           abs(yend)-min(yend, dpt.shape[0])),
                                          (abs(xstart)-max(xstart, 0),
                                           abs(xend)-min(xend, dpt.shape[1])),
                                          (0, 0)), mode='constant', constant_values=0)
        else:
            raise NotImplementedError()

        if thresh_z is True:
            msk1 = numpy.bitwise_and(cropped < zstart, cropped != 0)
            msk2 = numpy.bitwise_and(cropped > zend, cropped != 0)
            cropped[msk1] = zstart
            cropped[msk2] = 0.  # backface is at 0, it is set later
        return cropped 

Example 22

def _ccdf(self, samples):
        vals = np.zeros(samples.shape[0])
        # Avoid subtraction of infinities
        neqz = np.bitwise_and(np.any(samples > 0.0, axis=1),
                              np.any(samples < 1.0, axis=1))
        nrvs = norm.ppf(samples[neqz, :])
        vals[neqz] = norm.cdf((nrvs[:, 0] - self.theta * nrvs[:, 1])
                              / np.sqrt(1 - self.theta**2))
        vals[np.invert(neqz)] = norm.cdf(0.0)
        return vals 

Example 23

def roi_from_bbox(
        input_file,
        bbox,
        output_file):
    """ Create a ROI image from a bounding box.

    Parameters
    ----------
    input_file: str
        the reference image where the bbox is defined.
    bbox: 6-uplet
        the corner of the bbox in voxel coordinates: xmin, xmax, ymin, ymax,
        zmin, zmax.
    output_file: str
        the desired ROI image file.
    """
    # Load the reference image and generate a grid
    im = nibabel.load(input_file)
    xv, yv, zv = numpy.meshgrid(
        numpy.linspace(0, im.shape[0] - 1, im.shape[0]),
        numpy.linspace(0, im.shape[1] - 1, im.shape[1]),
        numpy.linspace(0, im.shape[2] - 1, im.shape[2]))
    xv = xv.astype(int)
    yv = yv.astype(int)
    zv = zv.astype(int)

    # Intersect the grid with the bbox
    xa = numpy.bitwise_and(xv >= bbox[0], xv <= bbox[1])
    ya = numpy.bitwise_and(yv >= bbox[2], yv <= bbox[3])
    za = numpy.bitwise_and(zv >= bbox[4], zv <= bbox[5])

    # Generate bbox indices
    indices = numpy.bitwise_and(numpy.bitwise_and(xa, ya), za)

    # Generate/save ROI
    roi = numpy.zeros(im.shape, dtype=int)
    roi[xv[indices].tolist(), yv[indices].tolist(), zv[indices].tolist()] = 1
    roi_im = nibabel.Nifti1Image(roi, affine=im.get_affine())
    nibabel.save(roi_im, output_file) 

Example 24

def _rgb_integers_to_components(self, rgb_integers):
        red_mask = 0x00FF0000
        green_mask = 0x0000FF00
        blue_mask =  0x000000FF
        masks = np.asarray([[red_mask, green_mask, blue_mask]])
        masked_rgb_components = np.bitwise_and(rgb_integers, masks)

        red_shifted = np.right_shift(masked_rgb_components[:,0], 16)
        green_shifted = np.right_shift(masked_rgb_components[:,1], 8)
        blue_shifted =  np.right_shift(masked_rgb_components[:,2], 0)
        return np.array([red_shifted, green_shifted, blue_shifted]).transpose() 

Example 25

def load_feature_run_model(bundle_hdf_path, prediction_csv_path):
    with h5py.File(bundle_hdf_path, 'r') as bundle_f:
        is_valid = bundle_f['info']['is_valid'].value
        is_test = bundle_f['info']['is_test'].value
        passenger_id = bundle_f['id'].value
        label = bundle_f['label'].value

        # concated
        feature = bundle_f['features'].value

    train_filter = (np.bitwise_and(~is_valid, ~is_test))
    valid_filter = (np.bitwise_and(is_valid, ~is_test))
    test_filter = is_test

    ##############
    # validation #
    ##############
    clf = RandomForestClassifier()
    clf.fit(feature[train_filter], label[train_filter])
    print('validation score: (Accuracy)',
          clf.score(feature[valid_filter], label[valid_filter]))

    ##############
    # prediction #
    ##############
    clf.fit(feature[~test_filter], label[~test_filter])
    prediction = clf.predict(feature[test_filter])

    df = pd.DataFrame(prediction, columns=['Survived'],
                      index=passenger_id[test_filter])
    df.index.rename('PassengerId')
    df.to_csv(prediction_csv_path) 

Example 26

def test_truth_table_bitwise(self):
        arg1 = [False, False, True, True]
        arg2 = [False, True, False, True]

        out = [False, True, True, True]
        assert_equal(np.bitwise_or(arg1, arg2), out)

        out = [False, False, False, True]
        assert_equal(np.bitwise_and(arg1, arg2), out)

        out = [False, True, True, False]
        assert_equal(np.bitwise_xor(arg1, arg2), out) 

Example 27

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 28

def __and__(self, other):
        return bitwise_and(self, other) 

Example 29

def __iand__(self, other):
        return bitwise_and(self, other, self) 

Example 30

def __rand__(self, other):
        return bitwise_and(other, self) 

Example 31

def main():
    """Main demo."""

    # Load survey data
    llh, data = get_flightlines()

    decimate = 5
    llh = llh[::decimate]
    data = data[::decimate]


    # Select by height intervals  (57% of the data)
    hrange = [95., 105.]
    keep = np.bitwise_and(llh[:, 2] > hrange[0], llh[:,2] < hrange[1])
    llh = llh[keep]
    data = data[keep]

    # Write out the reduced llh, data
    sf = shapefile.Writer(shapefile.POINT)
    outname = data_root + 'new_flightlines'
    log.info('Writing shapefile')
    sf.field("K")
    sf.field("Th")
    sf.field("U")
    for ll, dat in tqdm(zip(llh, data)):
        sf.point(ll[0], ll[1], ll[2])
        sf.record(K=dat[0], Th=dat[1], U=dat[2])
    sf.save(outname)
    log.info('Done!') 

Example 32

def create_binary_wf_data(wf, sync_mkr=0, samp_mkr=0, vertical_resolution=12):
        """Given numpy arrays of waveform and marker data convert to binary format.
        Assumes waveform data is np.float in range -1 to 1 and marker data can be cast to bool
        Binary format is waveform in MSB and and markers in LSB
        waveform       sync_mkr samp_mkr
        15 downto 4/2     1      0
        """
        #cast the waveform to integers
        if not((vertical_resolution == 12) or (vertical_resolution == 14)):
            raise ValueError("vertical resolution must be 12 or 14 bits")

        #convert waveform to integers
        scale_factor = 2**(vertical_resolution-1)
        bin_data = np.int16((scale_factor-1)*np.array(wf))

        #clip if necessary
        if np.max(bin_data) > scale_factor-1 or np.min(bin_data) < -scale_factor:
            warnings.warn("Clipping waveform. Max value: {:d} Min value: {:d}. Scale factor: {:d}.".format(np.max(bin_data), np.min(bin_data),scale_factor))
            bin_data = np.clip(bin_data, -scale_factor, scale_factor-1)

        # bin_data = bin_data.byteswap()
        #shift up to the MSB
        bin_data = np.left_shift(bin_data, 4 if vertical_resolution == 12 else 2)

        #add in the marker bits
        bin_data = np.bitwise_or(bin_data, np.bitwise_or(np.left_shift(np.bitwise_and(sync_mkr, 0x1), 1), np.bitwise_and(samp_mkr, 0x1)))

        return bin_data 

Example 33

def get_mask_overlap(mask1, mask2):
    intersect = np.bitwise_and(mask1, mask2).sum()
    union = np.bitwise_or(mask1, mask2).sum()
    return 1.0 * intersect / union 

Example 34

def training_stress_pma_grappe_score(X, pma):
    """Compute the training stress score using the MAP.

    Parameters
    ----------
    X : array-like, shape (n_samples, )
        Array containing the power intensities for a ride.

    pma : float
        Maximum Anaerobic Power.

    Returns
    -------
    tss_score: float
        Return the training stress score.

    """
    # Check the consistency of X and pma
    if len(X.shape) != 1:
        raise ValueError('X should have 1 dimension. Got {}, instead'.format(
            len(X.shape)))

    # Compute the stress for each item of the ESIE
    tss_grappe = 0.
    for key_sc in TS_SCALE_GRAPPE.keys():

        # Count the number of elements which corresponds to as sec
        # We need to convert it to minutes
        curr_stress = np.count_nonzero(
            np.bitwise_and(X >= ESIE_SCALE_GRAPPE[key_sc][0] * pma,
                           X < ESIE_SCALE_GRAPPE[key_sc][1] * pma)) / 60

        # Compute the cumulative stress
        tss_grappe += curr_stress * TS_SCALE_GRAPPE[key_sc]

    return tss_grappe 

Example 35

def getCrop(self, dpt, xstart, xend, ystart, yend, zstart, zend, thresh_z=True):
        """
        Crop patch from image
        :param dpt: depth image to crop from
        :param xstart: start x
        :param xend: end x
        :param ystart: start y
        :param yend: end y
        :param zstart: start z
        :param zend: end z
        :param thresh_z: threshold z values
        :return: cropped image
        """
        if len(dpt.shape) == 2:
            cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1])].copy()
            # add pixels that are out of the image in order to keep aspect ratio
            cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
                                           abs(yend)-min(yend, dpt.shape[0])),
                                          (abs(xstart)-max(xstart, 0),
                                           abs(xend)-min(xend, dpt.shape[1]))), mode='constant', constant_values=0)
        elif len(dpt.shape) == 3:
            cropped = dpt[max(ystart, 0):min(yend, dpt.shape[0]), max(xstart, 0):min(xend, dpt.shape[1]), :].copy()
            # add pixels that are out of the image in order to keep aspect ratio
            cropped = numpy.pad(cropped, ((abs(ystart)-max(ystart, 0),
                                           abs(yend)-min(yend, dpt.shape[0])),
                                          (abs(xstart)-max(xstart, 0),
                                           abs(xend)-min(xend, dpt.shape[1])),
                                          (0, 0)), mode='constant', constant_values=0)
        else:
            raise NotImplementedError()

        if thresh_z is True:
            msk1 = numpy.bitwise_and(cropped < zstart, cropped != 0)
            msk2 = numpy.bitwise_and(cropped > zend, cropped != 0)
            cropped[msk1] = zstart
            cropped[msk2] = 0.  # backface is at 0, it is set later
        return cropped 

Example 36

def delta_plus(self, nodes):
        '''
        Returns the list of edges forwarding from a set of nodes
        '''
        bool_indices_head = np.array([x[0] in nodes for x in self.edges])
        bool_indices_tail = np.array([x[1] not in nodes for x in self.edges])
        bool_indices_edges = np.bitwise_and(
            bool_indices_head, bool_indices_tail)
        return self.edges[bool_indices_edges] 

Example 37

def delta_minus(self, nodes):
        '''
        Returns the list of edges backwarding from a set of nodes
        '''
        bool_indices_head = np.array([x[0] not in nodes for x in self.edges])
        bool_indices_tail = np.array([x[1] in nodes for x in self.edges])
        bool_indices_edges = np.bitwise_and(
            bool_indices_head, bool_indices_tail)
        return self.edges[bool_indices_edges] 

Example 38

def delta_plus(self, nodes):
        '''
        Returns the list of edges forwarding from a set of nodes
        '''
        bool_indices_head = np.array([x[0] in nodes for x in self.edges])
        bool_indices_tail = np.array([x[1] not in nodes for x in self.edges])
        bool_indices_edges = np.bitwise_and(
            bool_indices_head, bool_indices_tail)
        return self.edges[bool_indices_edges] 

Example 39

def delta_minus(self, nodes):
        '''
        Returns the list of edges backwarding from a set of nodes
        '''
        bool_indices_head = np.array([x[0] not in nodes for x in self.edges])
        bool_indices_tail = np.array([x[1] in nodes for x in self.edges])
        bool_indices_edges = np.bitwise_and(
            bool_indices_head, bool_indices_tail)
        return self.edges[bool_indices_edges] 

Example 40

def binary_and(a,b):
    """Compare two binary arrays of the same length and return a third one,
    the bitwise addition of the first two."""
    # return np.logical_and(a,b)  # does not work with packed arrays
    return np.bitwise_and(a, b) 

Example 41

def test_truth_table_bitwise(self):
        arg1 = [False, False, True, True]
        arg2 = [False, True, False, True]

        out = [False, True, True, True]
        assert_equal(np.bitwise_or(arg1, arg2), out)

        out = [False, False, False, True]
        assert_equal(np.bitwise_and(arg1, arg2), out)

        out = [False, True, True, False]
        assert_equal(np.bitwise_xor(arg1, arg2), out) 

Example 42

def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b) 

Example 43

def __iand__(self, other):
            np.bitwise_and(self, other, out=self)
            return self 

Example 44

def _bin_results(self, length, results):
        """
        Add hits to the bins corresponding to these results. length_hit_bins
        is flattened, so we need to figure out the offset for this hit by
        factoring the sizes of the other dimensions.
        """
        hit_bin = np.zeros(results.shape[0], dtype='int64')
        multi = 1
        good = np.ones(results.shape[0], dtype='bool')
        for dim in range(len(self.out_labels)):
            for d1 in range(dim):
                multi *= self.bin_edges[d1].size
            if dim == 0 and len(self.out_labels)==1:
                try:
                    digi = np.digitize(results, self.bin_edges[dim])
                except ValueError:
                    # The user probably did something like 
                    # return a * b rather than
                    # return a[0] * b[0], which will only happen
                    # for single field functions.
                    digi = np.digitize(results[0], self.bin_edges[dim])
            else:
                digi = np.digitize(results[:,dim], self.bin_edges[dim])
            too_low = (digi == 0)
            too_high = (digi == self.bin_edges[dim].size)
            self.too_low[dim] += (too_low).sum()
            self.too_high[dim] += (too_high).sum()
            newgood = np.bitwise_and(np.invert(too_low), np.invert(too_high))
            good = np.bitwise_and(good, newgood)
            hit_bin += np.multiply((digi - 1), multi)
        digi_bins = np.arange(self.length_bin_hits[length].size+1)
        hist, digi_bins = np.histogram(hit_bin[good], digi_bins)
        self.length_bin_hits[length] += hist 

Example 45

def spread_bitsv(ival, level):
    res = np.zeros_like(ival, dtype='int64')
    for i in range(level):
        ares = np.bitwise_and(ival, 1<<i) << (i*2)
        np.bitwise_or(res, ares, res)
    return res 

Example 46

def mask_real_image(color, depth, depth_render):
    mask = (depth_render != 0).astype(np.uint8)[:, :, np.newaxis]
    masked_rgb = color * mask

    masked_hsv = cv2.cvtColor(masked_rgb, cv2.COLOR_BGR2HSV)
    saturation_mask = (masked_hsv[:, :, 2] <= SATURATION_THRESHOLD)[:, :, np.newaxis].astype(np.uint8)
    total_mask = np.bitwise_and(mask, saturation_mask)

    masked_color = color * total_mask
    masked_depth = depth[:total_mask.shape[0], :total_mask.shape[1]] * total_mask[:, :, 0]
    return masked_color, masked_depth 

Example 47

def show_occlusion(detection, rgb, depth, camera, bb_width):
    pixels = compute_2Dboundingbox(detection, camera, bb_width)
    depth_crop = depth[pixels[0, 0]:pixels[1, 0], pixels[0, 1]:pixels[2, 1]].astype(np.float)
    mask = np.bitwise_and(depth_crop < 880, depth_crop != 0)
    mask = cv2.erode(mask.astype(np.uint8), np.ones((3, 3)))
    print("Occlusion level : {}".format(np.sum(mask) / (mask.shape[0] * mask.shape[1])))
    cv2.imshow("object crop mask", (mask * 255))
    cv2.imshow("object crop depth", ((depth_crop / np.max(depth_crop) * 255).astype(np.uint8)))
    cv2.rectangle(rgb, tuple(pixels[0][::-1]), tuple(pixels[3][::-1]), (0, 0, 255), 2) 

Example 48

def test_truth_table_bitwise(self):
        arg1 = [False, False, True, True]
        arg2 = [False, True, False, True]

        out = [False, True, True, True]
        assert_equal(np.bitwise_or(arg1, arg2), out)

        out = [False, False, False, True]
        assert_equal(np.bitwise_and(arg1, arg2), out)

        out = [False, True, True, False]
        assert_equal(np.bitwise_xor(arg1, arg2), out) 

Example 49

def test_truth_table_bitwise(self):
        arg1 = [False, False, True, True]
        arg2 = [False, True, False, True]

        out = [False, True, True, True]
        assert_equal(np.bitwise_or(arg1, arg2), out)

        out = [False, False, False, True]
        assert_equal(np.bitwise_and(arg1, arg2), out)

        out = [False, True, True, False]
        assert_equal(np.bitwise_xor(arg1, arg2), out) 

Example 50

def cut_face_ellipse(image, face_coord):
    """ Cuts the image to just show the face in an ellipse.

    This function takes the rectangle coordinates around a detected face
    or faces and cuts the original image with the face coordenates. It also
    surrounds the face with an ellipse making black all the extra
    background or faces
    """
    images_ellipse = []
    for (x, y, w, h) in face_coord:
        center = (x + w / 2, y + h / 2)
        axis_major = (h / 2)
        axis_minor = (w / 2)
        mask = np.zeros_like(image)
        # create a white filled ellipse
        mask = cv2.ellipse(mask,
                           center=center,
                           axes=(axis_major, axis_minor),
                           angle=0,
                           startAngle=0,
                           endAngle=360,
                           color=(255, 255, 255),
                           thickness=-1)
        # Bitwise AND operation to black out regions outside the mask
        image_ellipse = np.bitwise_and(image, mask)
        images_ellipse.append(image_ellipse[y: y + h, x: x + w])

    return images_ellipse 
点赞