示例#1
0
    def applyDistortionMap(self, map, inplace=False, order=0):
        # for some reason, isinstance(map, fisheye.DistortionMap)
        # or isinstance(map, DistortionMap) does not work?!
        # this solves it...
        if not map.__class__.__name__ == "DistortionMap":
            raise TypeError("map has to be a DistortionMap")

        if not np.shape(self.data)[:2] == map.src_shape:
            logger.warning("Map source shape is not defined or does not match!")

        if inplace: image = self # operate on this image
        else:       image = Image( self ) # copy over this image

        # apply map
        logger.debug("applying distortion map...")

        # This is NOT very elegant...
        # I don't know a better possibility to loop over the last index
        # than this. The problem is, that a grayscale image has no third index.
        # ...
        if len(np.shape(image.data)) == 3:
            for layer in range(np.shape(image.data)[2]):
                layerout = scipy.ndimage.interpolation.map_coordinates(
                    input = self.data[:,:,layer],
                    coordinates = map.T, # map has to be transposed somehow
                    order = order
                    )
                try:    out = np.dstack( (out, layerout) ) # add to stack
                except: out = layerout # first addition
            image.data = out # set image data
            #image.data = scipy.ndimage.filters.median_filter(image.data,(5,5,1))
        else: # only 2 dim...
            image.data = scipy.ndimage.interpolation.map_coordinates(
                input = image.data,
                coordinates = map.T, # map has to be transposed somehow
                order = order
                )
            #image.data = scipy.ndimage.filters.median_filter(image.data,(5,5))


        # set coordinates from DistortionMap
        image.coordinates = map.out_coord
            
        logger.debug("done applying distortion map.")

        if not inplace: # return new image if not inplace
            return image