Пример #1
0
def MappedBoundingBox(transforms):
    '''Calculate the bounding box of the warped position for a set of transforms'''
    
    if len(transforms) == 1:
        # Copy the data instead of passing the transforms object
        return nornir_imageregistration.Rectangle(transforms[0].MappedBoundingBox.ToTuple())

    mbb = None
    for t in transforms:
        if mbb is None:
            mbb = t.MappedBoundingBox.ToArray()
        else:
            mbb = np.vstack((mbb, t.MappedBoundingBox.ToArray()))

    minX = np.min(mbb[:, 1])
    minY = np.min(mbb[:, 0])
    maxX = np.max(mbb[:, 3])
    maxY = np.max(mbb[:, 2])

    return  nornir_imageregistration.Rectangle((float(minY), float(minX), float(maxY), float(maxX)))
Пример #2
0
    def test_tile_overlaps_diagonal(self):
        A_fixed_center = (10, 40)
        B_fixed_center = (-10, -40)
        A_shape = (100, 100)
        B_shape = (100, 100)

        tile_A = self.create_tile(A_fixed_center, A_shape)
        tile_B = self.create_tile(B_fixed_center, B_shape)

        overlap = nornir_imageregistration.tile_overlap.TileOverlap(
            tile_A, tile_B, 1, 1)
        #       plot_tile_overlap(overlap)
        expected_overlap_A = nornir_imageregistration.Rectangle(
            (-50, -50, 30, -30))
        numpy.testing.assert_array_equal(
            overlap.scaled_overlapping_source_rect_A.BoundingBox,
            expected_overlap_A.BoundingBox)
        expected_overlap_B = nornir_imageregistration.Rectangle(
            (-30, 30, 50, 50))
        numpy.testing.assert_array_equal(
            overlap.scaled_overlapping_source_rect_B.BoundingBox,
            expected_overlap_B.BoundingBox)
        return
Пример #3
0
def CropImage(imageparam, Xo, Yo, Width, Height, cval=None):
    '''
       Crop the image at the passed bounds and returns the cropped ndarray.
       If the requested area is outside the bounds of the array then the correct region is returned
       with a background color set
       
       :param ndarray imageparam: An ndarray image to crop.  A string containing a path to an image is also acceptable.e
       :param int Xo: X origin for crop
       :param int Yo: Y origin for crop
       :param int Width: New width of image
       :param int Height: New height of image
       :param int cval: default value for regions outside the original image boundaries.  Defaults to 0.  Use 'random' to fill with random noise matching images statistical profile
       
       :return: Cropped image
       :rtype: ndarray
       '''

    image = ImageParamToImageArray(imageparam)

    if image is None:
        return None
    
#     if not isinstance(Width, int):
#         Width = int(Width)
#     
#     if not isinstance(Height, int):
#         Height = int(Height)
        
    assert(isinstance(Width, int))
    assert(isinstance(Height, int))
    
    if Width < 0:
        raise ValueError("Negative dimensions are not allowed")
    
    if Height < 0:
        raise ValueError("Negative dimensions are not allowed")
    
    image_rectangle = nornir_imageregistration.Rectangle([0, 0, image.shape[0], image.shape[1]])
    crop_rectangle = nornir_imageregistration.Rectangle.CreateFromPointAndArea([Yo, Xo], [Height, Width])
    
    overlap_rectangle = nornir_imageregistration.Rectangle.overlap_rect(image_rectangle, crop_rectangle)
    
    in_startY = Yo
    in_startX = Xo
    in_endX = Xo + Width
    in_endY = Yo + Height

    out_startY = 0
    out_startX = 0
    out_endX = Width
    out_endY = Height
    
    if overlap_rectangle is None:
        out_startY = 0
        out_startX = 0
        out_endX = 0
        out_endY = 0
        
        in_startY = Yo
        in_startX = Xo
        in_endX = Xo
        in_endY = Yo
    else:
        (in_startY, in_startX) = overlap_rectangle.BottomLeft
        (in_endY, in_endX) = overlap_rectangle.TopRight
        
        (out_startY, out_startX) = overlap_rectangle.BottomLeft - crop_rectangle.BottomLeft 
        (out_endY, out_endX) = np.array([out_startY, out_startX]) + overlap_rectangle.Size
        
    #To correct a numpy warning, convert values to int
    in_startX = int(in_startX)
    in_startY = int(in_startY)
    in_endX = int(in_endX)
    in_endY = int(in_endY)
    
    out_startX = int(out_startX)
    out_startY = int(out_startY)
    out_endX = int(out_endX)
    out_endY = int(out_endY)
    
    # Create mask
    rMask = None
    if cval == 'random':
        rMask = np.zeros((Height, Width), dtype=np.bool)
        rMask[out_startY:out_endY, out_startX:out_endX] = True
        
    # Create output image
    cropped = None
    if cval is None:
        cropped = np.zeros((Height, Width), dtype=image.dtype)
    elif cval == 'random':    
        cropped = np.ones((Height, Width), dtype=image.dtype)
    else:
        cropped = np.ones((Height, Width), dtype=image.dtype) * cval

    cropped[out_startY:out_endY, out_startX:out_endX] = image[in_startY:in_endY, in_startX:in_endX]
    
    if not rMask is None:
        return RandomNoiseMask(cropped, rMask, Copy=False)

    return cropped