def computeSuperPixelLabels( gtImage, superPixelObj ):
  n = superPixelObj.getNumSuperPixels()
  res = pomio.getVoidIdx() * np.ones( (n,), dtype=int )
  c = 0
  for sp in range( n ):
    try:
      res[sp] = assignClassLabelToSuperPixel( superPixelObj.getLabelImage()==sp, gtImage )
    except:
      # Keep a count of the undecided pixels.  Default label is void
      c += 1

  if c > 0:
    print 'WARNING: %d of %d superpixels have conflicting GT class labels. Setting to void.' % (c,len(res))
    if showDodgySPs:
      # Display for debug purposes.  set to false to turn it off.
      superPixels.displayImage( superPixels.generateImageWithSuperPixelBoundaries( pomio.msrc_convertLabelsToRGB(gtImage),\
                                                                                     superPixelObj.m_labels ), \
                                  imgTitle="Bad image graph", orientation="lower" )
      pdb.set_trace()
      plt.waitforbuttonpress()
                                                                                     
  return res
    plt.figure()
    pomio.showClassColours()
    plt.figure()

print 'Classifying file ', args.infile
image = skimage.io.imread(args.infile)
[spClassPreds, spGraph
 ] = SuperPixelClassifier.predictSuperPixelLabels(clfr, image,
                                                  numberSuperPixels,
                                                  superPixelCompactness)
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)
    print '   done.'

if args.verbose:
    plt.interactive(0)
    plt.show()
if args.verbose:
    plt.figure()

segResult = uflow.inferenceSuperPixel( \
    spix,\
    -np.log( np.maximum(1E-10, np.ascontiguousarray(classProbs) ) ), \
    adjProbs, \
    'abswap',\
    args.nbrPotentialMethod,\
    K )#, np.ascontiguousarray(nbrPotentialParams) )

# turn from label classes to msrc labels
segResult += 1
print '   done.'

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

# Show the result.
if args.verbose:
    pomio.showLabels(segResult)
    plt.title( 'Segmentation CRF result with K=%f' % K )
    plt.draw()
    print "labelling result, K = ", K 
    if dointeract:
        plt.waitforbuttonpress()

if dooverlay:
  plt.imshow(imgRGB)
  alphaValue = 0.5
else:
  alphaValue = 1.0

pomio.showLabels(segResult, colourMap, alphaVal=alphaValue)
if args.verbose:
  plt.title( 'Segmentation with K=%f, K0=%f' % ( args.K, args.K0 ) )
  plt.draw()
print "labelling result, K = ", args.K 


if args.outfile and len(args.outfile)>0:
    print 'Writing output label file %s' % args.outfile
    outimg = pomio.msrc_convertLabelsToRGB( segResult, colourMap )
    print 'size of outimg = ', outimg.shape
    #plt.imsave(args.outfile, outimg)
    y=Image.fromarray( outimg )
    y.save( args.outfile )
    print '   done.'


if args.verbose:
  if args.interactive:
      plt.waitforbuttonpress()

  if args.interactive:
    plt.interactive(False)
    plt.show()
print 'Performing CRF inference...'
if args.verbose:
    plt.figure()

segResult = uflow.inferenceSuperPixel( \
    spix,\
    -np.log( np.maximum(1E-10, np.ascontiguousarray(classProbs) ) ), \
    adjProbs, \
    'abswap',\
    args.nbrPotentialMethod,\
    K )#, np.ascontiguousarray(nbrPotentialParams) )

# turn from label classes to msrc labels
segResult += 1
print '   done.'

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

# Show the result.
if args.verbose:
    pomio.showLabels(segResult)
    plt.title('Segmentation CRF result with K=%f' % K)
    plt.draw()
    print "labelling result, K = ", K
    if dointeract:
        plt.waitforbuttonpress()
superPixelCompactness = args.superPixelCompactness

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

print 'Classifying file ', args.infile
image = skimage.io.imread(args.infile)
[spClassPreds, spGraph] = SuperPixelClassifier.predictSuperPixelLabels(clfr, image,numberSuperPixels, superPixelCompactness)
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)
    print '   done.'

if args.verbose:
    plt.interactive(0)
    plt.show()