Exemplo n.º 1
0
    def showPyr(self, pRange=None, gap=1, scale=None, disp='qt'):
        if (len(self.band(0).shape) == 1 or self.band(0).shape[0] == 1
                or self.band(0).shape[1] == 1):
            oned = 1
        else:
            oned = 0

        if pRange is None and oned == 1:
            pRange = 'auto1'
        elif pRange is None and oned == 0:
            pRange = 'auto2'

        if scale is None and oned == 1:
            scale = math.sqrt(2)
        elif scale is None and oned == 0:
            scale = 2

        nind = self.height

        # auto range calculations
        if pRange == 'auto1':
            pRange = numpy.zeros((nind, 1))
            mn = 0.0
            mx = 0.0
            for bnum in range(nind):
                band = self.band(bnum)
                band /= numpy.power(scale, bnum - 1)
                pRange[bnum] = numpy.power(scale, bnum - 1)
                bmn = numpy.amin(band)
                bmx = numpy.amax(band)
                mn = numpy.amin([mn, bmn])
                mx = numpy.amax([mx, bmx])
            if oned == 1:
                pad = (mx - mn) / 12  # magic number
                mn -= pad
                mx += pad
            pRange = numpy.outer(pRange, numpy.array([mn, mx]))
            band = self.pyrLow()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            if oned == 1:
                pad = (mx - mn) / 12
                mn -= pad
                mx += pad
            pRange[nind - 1, :] = [mn, mx]
        elif pRange == 'indep1':
            pRange = numpy.zeros((nind, 1))
            for bnum in range(nind):
                band = self.band(bnum)
                mn = numpy.amin(band)
                mx = numpy.amax(band)
                if oned == 1:
                    pad = (mx - mn) / 12
                    mn -= pad
                    mx += pad
                pRange[bnum, :] = numpy.array([mn, mx])
        elif pRange == 'auto2':
            pRange = numpy.zeros((nind, 1))
            sqsum = 0
            numpixels = 0
            for bnum in range(0, nind - 1):
                band = self.band(bnum)
                band /= numpy.power(scale, bnum)
                sqsum += numpy.sum(numpy.power(band, 2))
                numpixels += numpy.prod(band.shape)
                pRange[bnum, :] = numpy.power(scale, bnum)
            stdev = math.sqrt(sqsum / (numpixels - 1))
            pRange = numpy.outer(pRange, numpy.array([-3 * stdev, 3 * stdev]))
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.std(band)
            pRange[nind - 1, :] = numpy.array([av - 2 * stdev, av + 2 * stdev])
        elif pRange == 'indep2':
            pRange = numpy.zeros(nind, 2)
            for bnum in range(0, nind - 1):
                band = self.band(bnum)
                stdev = numpy.std(band)
                pRange[bnum, :] = numpy.array([-3 * stdev, 3 * stdev])
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.std(band)
            pRange[nind, :] = numpy.array([av - 2 * stdev, av + 2 * stdev])
        elif isinstance(pRange, basestring):
            print "Error: band range argument: %s" % (pRange)
            return
        elif pRange.shape[0] == 1 and pRange.shape[1] == 2:
            scales = numpy.power(numpy.array(range(0, nind)), scale)
            pRange = numpy.outer(scales, pRange)
            band = self.pyrLow()
            pRange[nind, :] = (pRange[nind, :] + numpy.mean(band) -
                               numpy.mean(pRange[nind, :]))

        # draw
        if oned == 1:
            # FIX: something here broke with Anaconda update!!
            #fig = matplotlib.pyplot.figure()
            pyplot.figure()
            #pyplot.subplot()...
            #ax0 = fig.add_subplot(nind, 1, 0)
            #ax0.set_frame_on(False)
            #ax0.get_xaxis().tick_bottom()
            #ax0.get_xaxis().tick_top()
            #ax0.get_yaxis().tick_right()
            #ax0.get_yaxis().tick_left()
            #ax0.get_yaxis().set_visible(False)
            #for bnum in range(0,nind):
            #    pylab.subplot(nind, 1, bnum+1)
            #    pylab.plot(numpy.array(range(numpy.amax(self.band(bnum).shape))).T,
            #               self.band(bnum).T)
            #    ylim(pRange[bnum,:])
            #    xlim((0,self.band(bnum).shape[1]-1))
            #matplotlib.pyplot.show()
        else:
            colormap = matplotlib.cm.Greys_r
            # skipping background calculation. needed?

            # compute positions of subbands:
            llpos = numpy.ones((nind, 2)).astype(float)
            dirr = numpy.array([-1.0, -1.0])
            ctr = numpy.array([self.band(0).shape[0] + 1 + gap,
                               1]).astype(float)
            sz = numpy.array([0.0, 0.0])
            for bnum in range(nind):
                prevsz = sz
                sz = self.band(bnum).shape

                # determine center position of new band:
                ctr = (ctr + gap * dirr / 2.0 + dirr * numpy.floor(
                    (prevsz + (dirr < 0).astype(int)) / 2.0))
                dirr = numpy.dot(dirr, numpy.array([[0, -1],
                                                    [1, 0]]))  # ccw rotation
                ctr = (ctr + gap * dirr / 2 + dirr * numpy.floor(
                    (sz + (dirr < 0).astype(int)) / 2.0))
                llpos[bnum, :] = ctr - numpy.floor(numpy.array(sz)) / 2.0
            # make position list positive, and allocate appropriate image
            llpos = llpos - numpy.ones((nind, 1)) * numpy.min(llpos)
            pind = range(self.height)
            for i in pind:
                pind[i] = self.band(i).shape
            urpos = llpos + pind
            d_im = numpy.ones((numpy.max(urpos), numpy.max(urpos))) * 255

            # paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
            nshades = 256
            for bnum in range(nind):
                mult = (nshades - 1) / (pRange[bnum, 1] - pRange[bnum, 0])
                d_im[llpos[bnum, 0]:urpos[bnum, 0],
                     llpos[bnum,
                           1]:urpos[bnum,
                                    1]] = (mult * self.band(bnum) +
                                           (1.5 - mult * pRange[bnum, 0]))

            # FIX: need a mode to switch between above and below display
            if disp == 'nb':
                JBhelpers.showIm(d_im[:self.band(0).shape[0]][:])
            elif disp == 'qt':
                showIm(d_im[:self.band(0).shape[0]][:])
Exemplo n.º 2
0
    def showPyr(self, prange='auto2', gap=1, scale=2, disp='qt'):
        ht = self.spyrHt()
        nind = len(self.pyr)
        nbands = self.numBands()

        ## Auto range calculations:
        if prange == 'auto1':
            prange = numpy.ones((nind, 1))
            band = self.pyrHigh()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            for lnum in range(1, ht + 1):
                for bnum in range(nbands):
                    idx = pyPyrUtils.LB2idx(lnum, bnum, ht + 2, nbands)
                    band = self.band(idx) / (numpy.power(scale, lnum))
                    prange[(lnum - 1) * nbands + bnum + 1] = numpy.power(
                        scale, lnum - 1)
                    bmn = numpy.amin(band)
                    bmx = numpy.amax(band)
                    mn = min([mn, bmn])
                    mx = max([mx, bmx])
            prange = numpy.outer(prange, numpy.array([mn, mx]))
            band = self.pyrLow()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            prange[nind - 1, :] = numpy.array([mn, mx])
        elif prange == 'indep1':
            prange = numpy.zeros((nind, 2))
            for bnum in range(nind):
                band = self.band(bnum)
                mn = band.min()
                mx = band.max()
                prange[bnum, :] = numpy.array([mn, mx])
        elif prange == 'auto2':
            prange = numpy.ones(nind)
            band = self.pyrHigh()
            sqsum = numpy.sum(numpy.power(band, 2))
            numpixels = band.shape[0] * band.shape[1]
            for lnum in range(1, ht + 1):
                for bnum in range(nbands):
                    band = self.band(LB2idx(lnum, bnum, ht + 2, nbands))
                    band = band / numpy.power(scale, lnum - 1)
                    sqsum += numpy.sum(numpy.power(band, 2))
                    numpixels += band.shape[0] * band.shape[1]
                    prange[(lnum - 1) * nbands + bnum + 1] = numpy.power(
                        scale, lnum - 1)
            stdev = numpy.sqrt(sqsum / (numpixels - 1))
            prange = numpy.outer(prange, numpy.array([-3 * stdev, 3 * stdev]))
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.sqrt(numpy.var(band))
            prange[nind - 1, :] = numpy.array([av - 2 * stdev, av + 2 * stdev])
        elif prange == 'indep2':
            prange = numpy.zeros((nind, 2))
            for bnum in range(nind - 1):
                band = self.band(bnum)
                stdev = numpy.sqrt(numpy.var(band))
                prange[bnum, :] = numpy.array([-3 * stdev, 3 * stdev])
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.sqrt(numpy.var(band))
            prange[nind - 1, :] = numpy.array([av - 2 * stdev, av + 2 * stdev])
        elif isinstance(prange, basestring):
            raise Exception("Bad RANGE argument: %s'" % (prange))
        elif prange.shape[0] == 1 and prange.shape[1] == 2:
            scales = numpy.power(scale, range(ht))
            scales = numpy.outer(numpy.ones((nbands, 1)), scales)
            scales = numpy.array([1, scales, numpy.power(scale, ht)])
            prange = numpy.outer(scales, prange)
            band = self.pyrLow()
            prange[nind, :] += numpy.mean(band) - numpy.mean(prange[nind, :])

        colormap = matplotlib.cm.Greys_r

        # compute positions of subbands
        llpos = numpy.ones((nind, 2))

        ncols = int(numpy.ceil((nbands + 1) / 2))
        nrows = int(numpy.ceil(nbands / 2))

        a = numpy.array(range(1 - nrows, 1))
        b = numpy.zeros((1, ncols))[0]
        ab = numpy.concatenate((a, b))
        c = numpy.zeros((1, nrows))[0]
        d = range(-1, -ncols - 1, -1)
        cd = numpy.concatenate((c, d))
        relpos = numpy.vstack((ab, cd)).T

        if nbands > 1:
            mvpos = numpy.array([-1, -1]).reshape(1, 2)
        else:
            mvpos = numpy.array([0, -1]).reshape(1, 2)
        basepos = numpy.array([0, 0]).reshape(1, 2)

        for lnum in range(1, ht + 1):
            ind1 = (lnum - 1) * nbands + 1
            sz = numpy.array(self.pyrSize[ind1]) + gap
            basepos = basepos + mvpos * sz
            if nbands < 5:  # to align edges
                sz += gap * (ht - lnum)
            llpos[ind1:ind1 +
                  nbands, :] = numpy.dot(relpos, numpy.diag(sz)) + (numpy.ones(
                      (nbands, 1)) * basepos)

        # lowpass band
        sz = numpy.array(self.pyrSize[nind - 1]) + gap
        basepos += mvpos * sz
        llpos[nind - 1, :] = basepos

        # make position list positive, and allocate appropriate image:
        llpos = llpos - ((numpy.ones(
            (nind, 2)) * numpy.amin(llpos, axis=0)) + 1) + 1
        llpos[0, :] = numpy.array([1, 1])
        # we want to cast it as ints, since we'll be using these as indices
        llpos = llpos.astype(int)
        urpos = llpos + self.pyrSize
        d_im = numpy.zeros((numpy.amax(urpos), numpy.amax(urpos)))

        # paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
        nshades = 64

        for bnum in range(1, nind):
            mult = (nshades - 1) / (prange[bnum, 1] - prange[bnum, 0])
            d_im[llpos[bnum, 0]:urpos[bnum, 0], llpos[bnum, 1]:urpos[
                bnum,
                1]] = mult * self.band(bnum) + (1.5 - mult * prange[bnum, 0])

        if disp == 'qt':
            showIm(d_im[:self.pyrSize[0][0] * 2, :])
        elif disp == 'nb':
            JBhelpers.showIm(d_im[:self.pyrSize[0][0] * 2, :])
Exemplo n.º 3
0
    def showPyr(self, prange = 'auto2', gap = 1, scale = 2, disp = 'qt'):
        ht = self.spyrHt()
        nind = len(self.pyr)
        nbands = self.numBands()

        ## Auto range calculations:
        if prange == 'auto1':
            prange = numpy.ones((nind,1))
            band = self.pyrHigh()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            for lnum in range(1,ht+1):
                for bnum in range(nbands):
                    idx = pyPyrUtils.LB2idx(lnum, bnum, ht+2, nbands)
                    band = self.band(idx)/(numpy.power(scale,lnum))
                    prange[(lnum-1)*nbands+bnum+1] = numpy.power(scale,lnum-1)
                    bmn = numpy.amin(band)
                    bmx = numpy.amax(band)
                    mn = min([mn, bmn])
                    mx = max([mx, bmx])
            prange = numpy.outer(prange, numpy.array([mn, mx]))
            band = self.pyrLow()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            prange[nind-1,:] = numpy.array([mn, mx])
        elif prange == 'indep1':
            prange = numpy.zeros((nind,2))
            for bnum in range(nind):
                band = self.band(bnum)
                mn = band.min()
                mx = band.max()
                prange[bnum,:] = numpy.array([mn, mx])
        elif prange == 'auto2':
            prange = numpy.ones(nind)
            band = self.pyrHigh()
            sqsum = numpy.sum( numpy.power(band, 2) )
            numpixels = band.shape[0] * band.shape[1]
            for lnum in range(1,ht+1):
                for bnum in range(nbands):
                    band = self.band(LB2idx(lnum, bnum, ht+2, nbands))
                    band = band / numpy.power(scale,lnum-1)
                    sqsum += numpy.sum( numpy.power(band, 2) )
                    numpixels += band.shape[0] * band.shape[1]
                    prange[(lnum-1)*nbands+bnum+1] = numpy.power(scale, lnum-1)
            stdev = numpy.sqrt( sqsum / (numpixels-1) )
            prange = numpy.outer(prange, numpy.array([-3*stdev, 3*stdev]))
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.sqrt( numpy.var(band) )
            prange[nind-1,:] = numpy.array([av-2*stdev, av+2*stdev])
        elif prange == 'indep2':
            prange = numpy.zeros((nind,2))
            for bnum in range(nind-1):
                band = self.band(bnum)
                stdev = numpy.sqrt( numpy.var(band) )
                prange[bnum,:] = numpy.array([-3*stdev, 3*stdev])
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.sqrt( numpy.var(band) )
            prange[nind-1,:] = numpy.array([av-2*stdev, av+2*stdev])
        elif isinstance(prange, basestring):
            print "Error:Bad RANGE argument: %s'" % (prange)
        elif prange.shape[0] == 1 and prange.shape[1] == 2:
            scales = numpy.power(scale, range(ht))
            scales = numpy.outer( numpy.ones((nbands,1)), scales )
            scales = numpy.array([1, scales, numpy.power(scale, ht)])
            prange = numpy.outer(scales, prange)
            band = self.pyrLow()
            prange[nind,:] += numpy.mean(band) - numpy.mean(prange[nind,:])

        colormap = matplotlib.cm.Greys_r

        # compute positions of subbands
        llpos = numpy.ones((nind,2));

        if nbands == 2:
            ncols = 1
            nrows = 2
        else:
            ncols = int(numpy.ceil((nbands+1)/2))
            nrows = int(numpy.ceil(nbands/2))

        a = numpy.array(range(1-nrows, 1))
        b = numpy.zeros((1,ncols))[0]
        ab = numpy.concatenate((a,b))
        c = numpy.zeros((1,nrows))[0]
        d = range(-1, -ncols-1, -1)
        cd = numpy.concatenate((c,d))
        relpos = numpy.vstack((ab,cd)).T
        
        if nbands > 1:
            mvpos = numpy.array([-1, -1]).reshape(1,2)
        else:
            mvpos = numpy.array([0, -1]).reshape(1,2)
        basepos = numpy.array([0, 0]).reshape(1,2)

        for lnum in range(1,ht+1):
            ind1 = (lnum-1)*nbands + 1
            sz = numpy.array(self.pyrSize[ind1]) + gap
            basepos = basepos + mvpos * sz
            if nbands < 5:         # to align edges
                sz += gap * (ht-lnum)
            llpos[ind1:ind1+nbands, :] = numpy.dot(relpos, numpy.diag(sz)) + ( numpy.ones((nbands,1)) * basepos )
    
        # lowpass band
        sz = numpy.array(self.pyrSize[nind-1]) + gap
        basepos += mvpos * sz
        llpos[nind-1,:] = basepos

        # make position list positive, and allocate appropriate image:
        llpos = llpos - ((numpy.ones((nind,2)) * numpy.amin(llpos, axis=0)) + 1) + 1
        llpos[0,:] = numpy.array([1, 1])
        urpos = llpos + self.pyrSize
        d_im = numpy.zeros((numpy.amax(urpos), numpy.amax(urpos)))
        
        # paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
        nshades = 64;

        for bnum in range(1,nind):
            mult = (nshades-1) / (prange[bnum,1]-prange[bnum,0])
            d_im[llpos[bnum,0]:urpos[bnum,0], 
                 llpos[bnum,1]:urpos[bnum,1]] = mult * self.band(bnum) + (1.5-mult*prange[bnum,0])

        if disp == 'qt':
            showIm(d_im[:self.pyrSize[0][0]*2,:])
        elif disp == 'nb':
            JBhelpers.showIm(d_im[:self.pyrSize[0][0]*2,:])
Exemplo n.º 4
0
    def showPyr(self, pRange = None, gap = 1, scale = None, disp = 'qt'):
        if ( len(self.band(0).shape) == 1 or self.band(0).shape[0] == 1 or
             self.band(0).shape[1] == 1 ):
            oned = 1
        else:
            oned = 0

        if pRange is None and oned == 1:
            pRange = 'auto1'
        elif pRange is None and oned == 0:
            pRange = 'auto2'

        if scale is None and oned == 1:
            scale = math.sqrt(2)
        elif scale is None and oned == 0:
            scale = 2

        nind = self.height
            
        # auto range calculations
        if pRange == 'auto1':
            pRange = numpy.zeros((nind,1))
            mn = 0.0
            mx = 0.0
            for bnum in range(nind):
                band = self.band(bnum)
                band /= numpy.power(scale, bnum-1)
                pRange[bnum] = numpy.power(scale, bnum-1)
                bmn = numpy.amin(band)
                bmx = numpy.amax(band)
                mn = numpy.amin([mn, bmn])
                mx = numpy.amax([mx, bmx])
            if oned == 1:
                pad = (mx-mn)/12       # magic number
                mn -= pad
                mx += pad
            pRange = numpy.outer(pRange, numpy.array([mn, mx]))
            band = self.pyrLow()
            mn = numpy.amin(band)
            mx = numpy.amax(band)
            if oned == 1:
                pad = (mx-mn)/12
                mn -= pad
                mx += pad
            pRange[nind-1,:] = [mn, mx];
        elif pRange == 'indep1':
            pRange = numpy.zeros((nind,1))
            for bnum in range(nind):
                band = self.band(bnum)
                mn = numpy.amin(band)
                mx = numpy.amax(band)
                if oned == 1:
                    pad = (mx-mn)/12;
                    mn -= pad
                    mx += pad
                pRange[bnum,:] = numpy.array([mn, mx])
        elif pRange == 'auto2':
            pRange = numpy.zeros((nind,1))
            sqsum = 0
            numpixels = 0
            for bnum in range(0, nind-1):
                band = self.band(bnum)
                band /= numpy.power(scale, bnum)
                sqsum += numpy.sum( numpy.power(band, 2) )
                numpixels += numpy.prod(band.shape)
                pRange[bnum,:] = numpy.power(scale, bnum)
            stdev = math.sqrt( sqsum / (numpixels-1) )
            pRange = numpy.outer( pRange, numpy.array([-3*stdev, 3*stdev]) )
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.std(band)
            pRange[nind-1,:] = numpy.array([av-2*stdev, av+2*stdev]);
        elif pRange == 'indep2':
            pRange = numpy.zeros(nind,2)
            for bnum in range(0,nind-1):
                band = self.band(bnum)
                stdev = numpy.std(band)
                pRange[bnum,:] = numpy.array([-3*stdev, 3*stdev])
            band = self.pyrLow()
            av = numpy.mean(band)
            stdev = numpy.std(band)
            pRange[nind,:] = numpy.array([av-2*stdev, av+2*stdev])
        elif isinstance(pRange, basestring):
            print "Error: band range argument: %s" % (pRange)
            return
        elif pRange.shape[0] == 1 and pRange.shape[1] == 2:
            scales = numpy.power( numpy.array( range(0,nind) ), scale)
            pRange = numpy.outer( scales, pRange )
            band = self.pyrLow()
            pRange[nind,:] = ( pRange[nind,:] + numpy.mean(band) - 
                               numpy.mean(pRange[nind,:]) )

        # draw
        if oned == 1:
            # FIX: something here broke with Anaconda update!!
            #fig = matplotlib.pyplot.figure()
            pyplot.figure()
            #pyplot.subplot()...
            #ax0 = fig.add_subplot(nind, 1, 0)
            #ax0.set_frame_on(False)
            #ax0.get_xaxis().tick_bottom()
            #ax0.get_xaxis().tick_top()
            #ax0.get_yaxis().tick_right()
            #ax0.get_yaxis().tick_left()
            #ax0.get_yaxis().set_visible(False)
            #for bnum in range(0,nind):
            #    pylab.subplot(nind, 1, bnum+1)
            #    pylab.plot(numpy.array(range(numpy.amax(self.band(bnum).shape))).T, 
            #               self.band(bnum).T)
            #    ylim(pRange[bnum,:])
            #    xlim((0,self.band(bnum).shape[1]-1))
            #matplotlib.pyplot.show()
        else:
            colormap = matplotlib.cm.Greys_r
            # skipping background calculation. needed?

            # compute positions of subbands:
            llpos = numpy.ones((nind, 2)).astype(float)
            dirr = numpy.array([-1.0, -1.0])
            ctr = numpy.array([self.band(0).shape[0]+1+gap, 1]).astype(float)
            sz = numpy.array([0.0, 0.0])
            for bnum in range(nind):
                prevsz = sz
                sz = self.band(bnum).shape
                
                # determine center position of new band:
                ctr = ( ctr + gap*dirr/2.0 + dirr * 
                        numpy.floor( (prevsz+(dirr<0).astype(int))/2.0 ) )
                dirr = numpy.dot(dirr,numpy.array([ [0, -1], [1, 0] ])) # ccw rotation
                ctr = ( ctr + gap*dirr/2 + dirr * 
                        numpy.floor( (sz+(dirr<0).astype(int)) / 2.0) )
                llpos[bnum,:] = ctr - numpy.floor(numpy.array(sz))/2.0 
            # make position list positive, and allocate appropriate image
            llpos = llpos - numpy.ones((nind,1))*numpy.min(llpos)
            pind = range(self.height)
            for i in pind:
                pind[i] = self.band(i).shape
            urpos = llpos + pind
            d_im = numpy.ones((numpy.max(urpos), numpy.max(urpos))) * 255
            
            # paste bands into image, (im-r1)*(nshades-1)/(r2-r1) + 1.5
            nshades = 256
            for bnum in range(nind):
                mult = (nshades-1) / (pRange[bnum,1]-pRange[bnum,0])
                d_im[llpos[bnum,0]:urpos[bnum,0], llpos[bnum,1]:urpos[bnum,1]]=(
                    mult*self.band(bnum) + (1.5-mult*pRange[bnum,0]) )
            
            # FIX: need a mode to switch between above and below display
            if disp == 'nb':
                JBhelpers.showIm(d_im[:self.band(0).shape[0]][:])
            elif disp == 'qt':
                showIm(d_im[:self.band(0).shape[0]][:])