Ejemplo n.º 1
0
    def createImages(self):
        #the fortran code used to read in short, convert to float and convert back to short.
        #let's use the image api and the casters to do that. The image in input can be of any
        # comptible type
        inImage = self._dem.clone()
        #reads short and convert to float
        inImage.initImage(self.inputFilename,'read',self.width,self.dataType)
        #create a suitable caster from self.dataType to self._dataTypeBindings
        inImage.setCaster('read',self._dataTypeBindings)
        inImage.createImage()
        self._numberLines = inImage.getLength()
        outImage = Image()
        #if name not provided assume overwrite of input
        import random  
        if(not self.outputFilename):
            self.outputFilename = os.path.basename(self.inputFilename) + str(int(random.random()*100000)) #add 6 digit random number to input filename 
            self.overwriteInputFileFlag = True
        #manages float and writes out short
        outImage.initImage(self.outputFilename,'write',self.width,self.dataType)
        outImage.metadatalocation = self.outputFilename

        #create a suitable caster from self._dataTypeBindings to self.dataType
        outImage.setCaster('write',self._dataTypeBindings)
        outImage.createImage()
        return inImage,outImage
Ejemplo n.º 2
0
class BandExtractor(Filter):
#Use kwargs so each subclass can add parameters to the init function.  
#If nameOut is a string then create the image using the input image info,
#otherwise check if it is an image object and raise an exceptio if not.

    def init(self,imgIn,nameOut,**kwargs):
        if isinstance(nameOut,str):
            #create generic image
            self._imgOut = Image()
            width = imgIn.getWidth()
            accessmode = 'write'
            bands = imgIn.getBands()
            scheme = imgIn.getInterleavedScheme()
            typec = imgIn.getDataType()
            #For now extract one band at the time. Might extend to do
            #multiple bands
            band = 1
            #create output image of the same type as input
            self._imgOut.initImage(nameOut,accessmode,width,typec,band,scheme)
            self._imgOut.createImage()
            #if created here then need to finalize at the end
            self._outCreatedHere = True
        elif(nameOut,Image):
            self._imgOut = nameOut

        else:
            print("Error. The second argument of BandExtractor.init() must be a string or an Image object")
            raise TypeError


        imgIn.createImage() # just in case has not been run before. if it was run then it does not have any effect 
        accessorIn = imgIn.getImagePointer()
        accessorOut = self._imgOut.getImagePointer()
        FL.init(self._filter,accessorIn,accessorOut)
    
    def finalize(self):#extend base one
        if self._outCreatedHere: 
            self._imgOut.finalizeImage()
        Filter.finalize(self)
    def __getstate__(self):
        d = dict(self.__dict__)
        del d['logger']
        return d

    def __setstate__(self,d):
        self.__dict__.update(d)
        self.logger = logging.getLogger('isce.isceobj.ImgeFilter.BandExtractor')
        return
    
    def __init__(self,typeExtractor,band):
        Filter.__init__(self)
        self.logger = logging.getLogger('isce.isceobj.ImageFilter.BandExtractor')
        #get the filter C++ object pointer
        self._filter = FL.createFilter(typeExtractor,band)
        self._outCreatedHere = False
        self._imgOut = None 
Ejemplo n.º 3
0
def main():
    
    opt = sys.argv[1]
    if opt == '1':
        #extract phase from complex image in polar coordinates
        filename = 'complexPolarBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('PhaseExtractor','polar')
        outfile = 'phasePolarBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '2':
        #extract magnitude from complex image in polar coordinates
        filename = 'complexPolarBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('MagnitudeExtractor','polar')
        outfile = 'magnitudePolarBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '3':
        #extract phase from complex image in cartesian coordinates
        filename = 'complexXYBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('PhaseExtractor','cartesian')
        outfile = 'phaseBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '4':
        #extract magnitude from complex image in cartesian coordinates
        filename = 'complexXYBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('MagnitudeExtractor','cartesian')
        outfile = 'magnitudeBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '5':
        #extract real part from complex image in cartesian coordinates
        filename = 'complexXYBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('RealExtractor','cartesian')
        outfile = 'realBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '6':
        #extract imaginary part from complex image in cartesian coordinates
        filename = 'complexXYBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('ImagExtractor','cartesian')
        outfile = 'imagBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '7':
        #extract real part from complex image in polar coordinates
        filename = 'complexPolarBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('RealExtractor','polar')
        outfile = 'realPolarBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '8':
        #extract imaginary part from complex image in polar coordinates
        filename = 'complexPolarBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        filter = createFilter('ImagExtractor','polar')
        outfile = 'imagPolarBIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
    elif opt == '9':    
        #extract band from image
        filename = 'complexXYBIP'
        scheme = 'BIP'
        bands = 2
        typeF = 'CDOUBLE'
        accessmode = 'read'
        width = 3
        img = Image()
        img.initImage(filename,accessmode,width,typeF,bands,scheme)
        #bands are zero based
        bandToExtract = 0
        filter = createFilter('BandExtractor',bandToExtract)
        outfile = 'band0BIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        
        #need to rewind the image to the beginning 
        img.rewind()
        #bands are zero based
        bandToExtract = 1
        filter = createFilter('BandExtractor',bandToExtract)
        outfile = 'band1BIP'
        filter.init(img,outfile)
        filter.extract()
        filter.finalize()
        img.finalizeImage()
Ejemplo n.º 4
0
class ComplexExtractor(Filter):
    """Extracts components (real, imaginary, magnitude, phase) from a complex datatype"""

    #Use kwargs so each subclass can add parameters to the init function.
    #If nameOut is a string then create the image using the input image info,
    #otherwise check if it is an image object and raise an exception if not.

    def init(self, imgIn, nameOut, **kwargs):
        """Method to pass the input and output image to the  filter"""
        # check if the output image nameOut is provided. If it is a string create the image here using
        # the input image as template
        if isinstance(nameOut, str):
            #create generic image
            self._imgOut = Image()
            width = imgIn.getWidth()
            accessmode = 'write'
            bands = imgIn.getBands()
            scheme = imgIn.getInterleavedScheme()
            typec = imgIn.getDataType()
            #The assumption is that the imgIn is complex. The single component is the imgIn data type without the C
            # for instace CREAL becomes REAL
            typeF = typec[1:]
            #create output image of the same type as input
            self._imgOut.initImage(nameOut, accessmode, width, typeF, bands,
                                   scheme)
            self._imgOut.createImage()
            #if created here then need to finalize at the end
            self._outCreatedHere = True
        elif (nameOut, Image):
            self._imgOut = nameOut

        else:
            print(
                "Error. The second argument of ComplexExtractor.init() must be a string or an Image object"
            )
            raise TypeError

        imgIn.createImage(
        )  # just in case has not been run before. if it was run then it does not have any effect
        accessorIn = imgIn.getImagePointer()
        accessorOut = self._imgOut.getImagePointer()
        FL.init(self._filter, accessorIn, accessorOut)

    def finalize(self):  #extend base one
        """Finalize filter baseclass and output accessor if created here and not passed"""
        if self._outCreatedHere:
            self._imgOut.finalizeImage()
        Filter.finalize(self)

    def __getstate__(self):
        d = dict(self.__dict__)
        del d['logger']
        return d

    def __setstate__(self, d):
        self.__dict__.update(d)
        self.logger = logging.getLogger(
            'isce.isceobj.ImageFilter.ComplexExtractor')
        return

    def __init__(self, typeExtractor, fromWhat):
        """Initialize the filter passing what is extracted and from what type of complex image"""
        Filter.__init__(self)
        self.logger = logging.getLogger(
            'isce.isceobj.ImageFilter.ComplexExtractor')
        #possible inputs
        #(MagnitudeExctractor,'cartesian')
        #(MagnitudeExctractor,'polar')
        #(PhaseExctractor,'cartesian')
        #(PhaseExctractor,'polar')
        #(RealExctractor,'cartesian')
        #(ImagExctractor,'cartesian')
        #(RealExctractor,'polar')
        #(ImagExctractor,'polar')
        #get the filter C++ object pointer calling the Filtermodule.cpp which calls the FilterFactory.cpp
        self._filter = FL.createFilter(typeExtractor, fromWhat)
        self._outCreatedHere = False
        self._imgOut = None