def predictSuperPixelLabels(classifier, image,numberSuperPixels,  \
                                superPixelCompactness, makeProbabilities ):
    print "\n**Computing super pixel labelling for input image"
    outProbs = None

    # Get superpixels
    spgraph = superPixels.computeSuperPixelGraph(image,'slic',[numberSuperPixels, superPixelCompactness])
    imgSuperPixelsMask = spgraph.m_labels
    imgSuperPixels = spgraph.m_nodes
    numberImgSuperPixels = len(imgSuperPixels)
    print "**Image contains", numberImgSuperPixels, "superpixels"
    
    # Get superpixel features
    # todo: replace with features.computeSuperPixelFeatures JRS
    superPixelFeatures = FeatureGenerator.generateSuperPixelFeatures(image, imgSuperPixelsMask, None)
    assert np.shape(superPixelFeatures)[0] == numberImgSuperPixels, "Number of superpixels in feature array != number super pixels in image!:: " + str(np.shape(superPixelFeatures)[0]) + " vs. " + str(numberImgSuperPixels)

    superPixelLabels = classifier.predict( superPixelFeatures )
    
    if makeProbabilities:
        outProbs = classification.classProbsOfFeatures(
            superPixelFeatures, classifier, requireAllClasses=False
        )
    return (superPixelLabels, spgraph, outProbs)
def getSuperPixelData(msrcImages,numberSuperPixels, superPixelCompactness):
    
    # Should probably make this a call to pomio in case the ordering changes in the future...
    voidClassLabel = pomio.getVoidIdx()
    
    numberImages = len(msrcImages)    
    
    # for each image:
    #   determine superpixel label (discard if void)
    #   compute superpixel features of valid superpixels
    #   append features to cumulative array of all super pixel features
    #   append label to array of all labels
    
    superPixelFeatures = None
    superPixelLabels = np.array([], int) # used for superpixel labels
    numberVoidSuperPixels = 0   # keep track of void superpixels

    nbClasses = pomio.getNumClasses()
    classAdjCounts = np.zeros( (nbClasses, nbClasses) )
    adjCountsTotal = 0
    adjVoidCountsTotal = 0

    for imgIdx in range(0, numberImages):
    
        superPixelIgnoreList = np.array([], int) # this is used to skip over the superpixel in feature processing
    
        print "\n**Processing Image#" , (imgIdx + 1) , " of" , numberImages
    
        # get raw image and ground truth labels
        img = msrcImages[imgIdx].m_img
        imgPixelLabels = msrcImages[imgIdx].m_gt
        
        # create superpixel map and graph for image
        spgraph = superPixels.computeSuperPixelGraph( img, 'slic', [numberSuperPixels, superPixelCompactness] )
        imgSuperPixelMask = spgraph.m_labels
        imgSuperPixels = spgraph.m_nodes
        numberImgSuperPixels = spgraph.getNumSuperPixels()
    
        # create superpixel exclude list & superpixel label array
        allSPClassLabels = []
        for spIdx in range(0, numberImgSuperPixels):
            
            superPixelValue = imgSuperPixels[spIdx]
            #print "\tINFO: Processing superpixel =", superPixelValue , " of" , numberImgSuperPixels, " in image"
            
            
            # Assume superpixel labels are sequence of integers
            superPixelValueMask = (imgSuperPixelMask == superPixelValue ) # Boolean array for indexing superpixel-pixels
            superPixelLabel = assignClassLabelToSuperPixel(superPixelValueMask, imgPixelLabels)
            allSPClassLabels.append( superPixelLabel)

            if(superPixelLabel == voidClassLabel):
            
                # add to ignore list, increment void count & do not add to superpixel label array
                superPixelIgnoreList = np.append(superPixelIgnoreList, superPixelValue)
                numberVoidSuperPixels = numberVoidSuperPixels + 1
                
            else:
                superPixelLabels = np.append(superPixelLabels, superPixelLabel)
        
        assert len(allSPClassLabels) == numberImgSuperPixels
        (theseClassAdjCounts,adjVoidCount,adjCount) = spgraph.countClassAdjacencies( nbClasses, allSPClassLabels )
        classAdjCounts     += theseClassAdjCounts
        adjCountsTotal     += adjCount
        adjVoidCountsTotal += adjVoidCount

        # Now we have the superpixel labels, and an ignore list of void superpixels - time to get the features!
        # todo: replace with features.computeSuperPixelFeatures JRS
        imgSuperPixelFeatures = FeatureGenerator.generateSuperPixelFeatures(img, imgSuperPixelMask, excludeSuperPixelList=superPixelIgnoreList)
        
        if superPixelFeatures == None:        
            superPixelFeatures = imgSuperPixelFeatures;
        else:
            # stack the superpixel features into a single list
            superPixelFeatures = np.vstack( [ superPixelFeatures, imgSuperPixelFeatures ] )
    
    
    assert np.shape(superPixelFeatures)[0] == np.shape(superPixelFeatures)[0] , "Number of samples != number labels"
    print "\n**Processed total of" , numberImages, "images"
    print "  %d out of %d adjacencies were ignored due to void (%.2f %%)" % \
        (adjVoidCountsTotal, adjCountsTotal, \
             100.0*adjVoidCountsTotal/adjCountsTotal)

    # Now return the results
    return [ superPixelFeatures, superPixelLabels, classAdjCounts ]
        colourMap = pomio.msrc_classToRGB
    else:
        spix, classProbs = isprs.loadISPRSResultFromMatlab(args.infile)
        colourMap = isprs.colourMap

else:
    # Assume input image needs processing and classifying
    print "Superpixel generation mode"

    numberSuperPixels = args.nbSuperPixels
    superPixelCompactness = args.superPixelCompactness

    imgRGB = amntools.readImage(args.infile)

    # Turn image into superpixels.
    spix = superPixels.computeSuperPixelGraph(
        imgRGB, 'slic', [numberSuperPixels, superPixelCompactness])

    print 'Loading classifier...'
    assert args.clfrFn != None, 'No classifier filename specified!'

    clfr = pomio.unpickleObject(args.clfrFn)

    print 'Computing superpixel features...'
    ftrs = features.computeSuperPixelFeatures(imgRGB,
                                              spix,
                                              ftype='classic',
                                              aggtype='classic')

    print 'Computing class probabilities...'
    classProbs = classification.classProbsOfFeatures(ftrs,clfr,\
                                                     requireAllClasses=False)
예제 #4
0
from matplotlib import pyplot as plt
from matplotlib import cm
import pomio
import slic
import superPixels
import skimage
import isprs
import amntools

imgRGB = amntools.readImage(args.imagefile)

if len(args.spfile) == 0:
    numberSuperPixels = args.nbSuperPixels
    superPixelCompactness = args.superPixelCompactness
    # Turn image into superpixels.
    spix = superPixels.computeSuperPixelGraph(imgRGB, "slic", [numberSuperPixels, superPixelCompactness])
elif args.spfile.endswith(".pkl"):
    superPixelInput = pomio.unpickleObject(args.spfile)
    spix = superPixelInput[0]
    classProbs = superPixelInput[1]
    colourMap = pomio.msrc_classToRGB
elif args.spfile.endswith(".mat"):
    spix, classProbs = isprs.loadISPRSResultFromMatlab(args.spfile)
    colourMap = isprs.colourMap
else:
    assert False


# Display superpixel boundaries on image.
plt.imshow(superPixels.generateImageWithSuperPixelBoundaries(imgRGB, spix.m_labels))
plt.show()
예제 #5
0
#infile = args.infile
#outfile = args.outfile
#outprobsfile = args.outprobsfile
numberSuperPixels = args.nbSuperPixels
superPixelCompactness = args.superPixelCompactness

if args.verbose:
    plt.interactive(1)
    plt.figure()
    pomio.showClassColours()
    plt.figure()

print 'Classifying file ', args.infile
image = amntools.readImage(args.infile)
spGraph = superPixels.computeSuperPixelGraph(
    image, 'slic', [numberSuperPixels, superPixelCompactness])
[spClassPreds, spClassProbs
 ] = classification.classifyImageSuperPixels(image, clfr, spGraph, ftype,
                                             aggtype, makeProbs)
spClassPredsImage = spGraph.imageFromSuperPixelData(
    spClassPreds.reshape((len(spClassPreds), 1)))

if args.verbose:
    plt.subplot(1, 2, 1)
    plt.imshow(image)
    plt.title(args.infile)
    plt.subplot(1, 2, 2)
    pomio.showLabels(spClassPredsImage)
    plt.waitforbuttonpress()

if args.outfile and len(args.outfile) > 0:
#infile = args.infile
#outfile = args.outfile
#outprobsfile = args.outprobsfile
numberSuperPixels = args.nbSuperPixels
superPixelCompactness = args.superPixelCompactness

if args.verbose:
    plt.interactive(1)
    plt.figure()
    pomio.showClassColours()
    plt.figure()

print 'Classifying file ', args.infile
image = amntools.readImage(args.infile)
spGraph = superPixels.computeSuperPixelGraph(image,'slic',[numberSuperPixels, superPixelCompactness])
[spClassPreds, spClassProbs] = classification.classifyImageSuperPixels( image, clfr, spGraph, ftype, aggtype, makeProbs)
spClassPredsImage = spGraph.imageFromSuperPixelData( spClassPreds.reshape( (len(spClassPreds),1) ) )

if args.verbose:
    plt.subplot(1,2,1)
    plt.imshow(image)
    plt.title(args.infile)
    plt.subplot(1,2,2)
    pomio.showLabels(spClassPredsImage)
    plt.waitforbuttonpress()

if args.outfile and len(args.outfile)>0:
    print 'Writing output label file %s' % args.outfile
    outimg = pomio.msrc_convertLabelsToRGB( spClassPredsImage )
    skimage.io.imsave(args.outfile, outimg)