def __init__(self, fn, gtfn, hqfn, verbose=False): # load the image (as numpy nd array, 8bit) if verbose: print 'Loading image ', fn self.m_img = amntools.readImage(fn) if verbose: print 'Loading gt image ', gtfn self.m_gt = msrc_convertRGBToLabels(amntools.readImage(gtfn), gtfn) # not necessarily hq try: #self.m_hq = msrc_convertRGBToLabels( pylab.imread( hqfn ) ) if verbose: print 'Looking for high quality gt image ', hqfn self.m_hq = msrc_convertRGBToLabels(amntools.readImage(hqfn)) if verbose: print ' - found' except IOError: if verbose: print ' - not found' self.m_hq = None self.m_imgFn = fn self.m_gtFn = gtfn self.m_hqFn = hqfn
def __init__( self, fn, gtfn, hqfn, verbose=False ): # load the image (as numpy nd array, 8bit) if verbose: print 'Loading image ', fn self.m_img = amntools.readImage( fn ) if verbose: print 'Loading gt image ', gtfn self.m_gt = msrc_convertRGBToLabels( amntools.readImage( gtfn ), gtfn ) # not necessarily hq try: #self.m_hq = msrc_convertRGBToLabels( pylab.imread( hqfn ) ) if verbose: print 'Looking for high quality gt image ', hqfn self.m_hq = msrc_convertRGBToLabels( amntools.readImage( hqfn ) ) if verbose: print ' - found' except IOError: if verbose: print ' - not found' self.m_hq = None self.m_imgFn = fn self.m_gtFn = gtfn self.m_hqFn = hqfn
assert feq(h.sum(), 1.0, 1E-5), "Histogram sums to %f" % h.sum() # now regularise by adding a uniform distribution with this much mass. regAlpha = 0.01 h = regAlpha / h.size + (1.0 - regAlpha) * h assert feq(h.sum(), 1.0, 1E-5), "Histogram sums to %f" % h.sum() return h def feq(a, b, tol): return np.abs(a - b) <= tol # # MAIN # cvimg = amntools.readImage("ship-at-sea.jpg") dimg = cvimg.copy() dohsv = True dointeract = False if dohsv: # I would love to do this on rgb, but calcBackProject has a bug for # 3d histograms. Flakey. # Convert to hsv instead cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2HSV) channels = [0, 1] ranges = [0, 180, 0, 256] else: channels = [0, 1, 2] ranges = [0, 256, 0, 256, 0, 256]
def readImageFileRGB(imageFileLocation): """This returns a (i, j, 3) RGB ndarray""" sourceImage = amntools.readImage(imageFileLocation) return sourceImage
def testQuickshift_broomBroomRGB(carImg): testSuperPixelOnImage(carImg, "Quickshift") ################################################################################ # MAIN ################################################################################ if __name__ == "__main__": # Examples on given image infile = sys.argv[1] nbSuperPixels = int(sys.argv[2]) superPixelCompactness = float(sys.argv[3]) image = amntools.readImage(infile) print "Oversegmentation examples will be displayed." #testSLIC_lenaRGB(int(sys.argv[2]),int(sys.argv[3])) testSLIC_lenaRGB(nbSuperPixels,superPixelCompactness) testGraph_lenaRGB() testQuickshift_lenaRGB() # Examples on car image (idx ) from MSRC print "\tOversegmentation with car image from MSRC dataset::\n" print "\tSLIC algo:" testSLIC_broomBroomRGB(image) print "\tGraph algo:" testGraph_broomBroomRGB(image)
import isprs # parse args clfrFn = args.clfrFn imgFn = args.infile dbgMode = 0 # # MAIN # precomputedMode = args.matFn != None imgRGB = amntools.readImage( imgFn ) if args.verbose: plt.interactive(1) plt.imshow(imgRGB) plt.title('original image') #plt.waitforbuttonpress() plt.figure() if precomputedMode == True: x = scipy.io.loadmat( args.matFn ) if x.has_key('singlepix_conf'): # these are the per-pixel probs classLabs = x['singlepix_label'] classProbs = x['singlepix_conf'].astype(float) # the labels are out of order for probabilities. Paul has: impervious, bldg, car, low veg, tree, clutter #classProbs = classProbs[:,:, np.array([1,2,5,3,4])-1]
def loadPredictionImageLabels(predictImgLabelsFile): # assume an image file, use pomio to convert predictLabels = pomio.msrc_convertRGBToLabels( amntools.readImage(predictImgLabelsFile)) return predictLabels
superPixelInput = pomio.unpickleObject(args.infile) spix = superPixelInput[0] classProbs = superPixelInput[1] 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')
# # Colour map, from-to # cmap = [\ ( ( 0,255, 0),( 0,128, 0) ), \ ( (200,100, 20),(128,128, 0) ), \ ( (255, 0, 0),(128, 0, 0) ), \ ( ( 0, 0,255),( 64,128, 0) ), \ ( (100,100,100),(128, 64,128) ), \ ] infile = sys.argv[1] outfile= sys.argv[2] image = amntools.readImage(infile) plt.interactive(1) plt.figure() pomio.showClassColours() plt.figure() plt.imshow(image) plt.title('input labels') #plt.waitforbuttonpress() # Make the output image newimg = image.copy() # for each colour make the transfer
args = parser.parse_args() import pickle as pkl import sys import numpy as np 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:
aggtype = 'classic' #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()
aggtype = 'classic' #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 )
# assuming it's float. Make it sum to 1 h = h / h.sum() assert feq( h.sum(), 1.0, 1E-5 ), "Histogram sums to %f" % h.sum() # now regularise by adding a uniform distribution with this much mass. regAlpha = 0.01 h = regAlpha/h.size + (1.0-regAlpha)*h assert feq( h.sum(), 1.0, 1E-5 ), "Histogram sums to %f" % h.sum() return h def feq(a,b,tol): return np.abs(a-b)<=tol # # MAIN # cvimg = amntools.readImage("ship-at-sea.jpg") dimg = cvimg.copy() dohsv = True dointeract = False if dohsv: # I would love to do this on rgb, but calcBackProject has a bug for # 3d histograms. Flakey. # Convert to hsv instead cvimg = cv2.cvtColor(cvimg,cv2.COLOR_BGR2HSV) channels = [0,1] ranges = [0,180,0,256] else: channels = [0,1,2] ranges = [0,256,0,256,0,256]