def buildPatchFromScan( wrongPixelCenters, folderStore, scanName, scanPath, multispectralImagePath, panchromaticImagePath, actualPixelPointMachine, classifierInformation, isTest=False, ): # Show feedback if not isTest: print "Saving patch..." # Initialize multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) windowLengthInMeters = classifierInformation.getWindowLengthInMeters() patchTestFraction = ( classifierInformation.getTestDataset().countSamples() / float(classifierInformation.getTrainingDataset().countSamples()) if not isTest else 0.2 ) # Set path targetPatchPath = folderStore.fillPatchPath(scanName + " auto") targetPatchFolderPath = os.path.dirname(targetPatchPath) # Define shortcut buildPatchEasily = lambda makePath, pixelCenters: buildPatch( makePath(targetPatchFolderPath), pixelCenters, actualPixelPointMachine, multispectralImage, panchromaticImage, windowLengthInMeters, ) # Shuffle random.shuffle(wrongPixelCenters) # Prepare information = { "patches": {"probability name": scanName, "probability path": scanPath}, "windows": { "window length in meters": windowLengthInMeters, "spatial reference": multispectralImage.getSpatialReference(), }, } if not isTest: # Split patchTestCount = int(patchTestFraction * len(wrongPixelCenters)) # Build information["training set"] = buildPatchEasily( sample_process.makeTrainingPath, wrongPixelCenters[patchTestCount:] ).getStatistics() information["test set"] = buildPatchEasily( sample_process.makeTestPath, wrongPixelCenters[:patchTestCount] ).getStatistics() # Save store.saveInformation(targetPatchPath, information)
def extractSamples(self, positiveLocationPath, multispectralImagePath, panchromaticImagePath): view.sendFeedback('Loading images...\n\tmultispectralImagePath = %s\n\tpanchromaticImagePath = %s' % (multispectralImagePath, panchromaticImagePath)) multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) view.sendFeedback('Generating geoCenters...\n\tmultispectralPixelShiftValue = %s\n\tshiftCount = %s\n\tnegativeRatio = %s' % (self.multispectralPixelShiftValue, self.shiftCount, self.negativeRatio)) positiveMultispectralPixelCenters = self.loadPixelCenters(multispectralImage, positiveLocationPath) negativeMultispectralPixelCenters = self.makePixelCenters(multispectralImage, positiveMultispectralPixelCenters) positiveGeoCenters = multispectralImage.convertPixelLocationsToGeoLocations(positiveMultispectralPixelCenters) negativeGeoCenters = multispectralImage.convertPixelLocationsToGeoLocations(negativeMultispectralPixelCenters) view.sendFeedback('Extracting positive samples...') self.extract(1, positiveGeoCenters, multispectralImage, panchromaticImage) view.sendFeedback('Extracting negative samples...') self.extract(0, negativeGeoCenters, multispectralImage, panchromaticImage)
def extractDataset(targetDatasetPath, paths, parameters): # Unpack multispectralImagePath, panchromaticImagePath, positiveLocationPath = paths windowGeoLength, negativeRatio, multispectralPixelShiftValue, shiftCount = parameters # Show feedback view.sendFeedback('Extracting dataset...\n\ttargetDatasetPath = %s' % targetDatasetPath) # Extract samples extractor = Extractor(targetDatasetPath, windowGeoLength, multispectralPixelShiftValue, shiftCount, negativeRatio) extractor.extractSamples(positiveLocationPath, multispectralImagePath, panchromaticImagePath) # Record targetDataset = extractor.getSampleDatabase() multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) points, spatialReference = point_store.load(positiveLocationPath) store.saveInformation(targetDatasetPath, { 'multispectral image': { 'path': multispectralImagePath, 'pixel width': multispectralImage.getPixelWidth(), 'pixel height': multispectralImage.getPixelHeight(), 'geo transform': multispectralImage.getGeoTransform(), }, 'panchromatic image': { 'path': panchromaticImagePath, 'pixel width': panchromaticImage.getPixelWidth(), 'pixel height': panchromaticImage.getPixelHeight(), 'geo transform': panchromaticImage.getGeoTransform(), }, 'positive location': { 'path': positiveLocationPath, 'location count': len(points), 'spatial reference': spatialReference, }, 'windows': { 'path': targetDatasetPath, 'sample count': targetDataset.countSamples(), 'positive sample count': targetDataset.countPositiveSamples(), 'negative sample count': targetDataset.countNegativeSamples(), }, 'parameters': { 'window geo length': windowGeoLength, 'multispectral pixel shift value': multispectralPixelShiftValue, 'shift count': shiftCount, 'negative ratio': negativeRatio, } }) # Return return targetDataset
def scan(targetProbabilityPath, classifierPath, multispectralImagePath, panchromaticImagePath, scanRatio, regionFrames=None): # Initialize classify = load(classifierPath) classifierInformation = Information(classifierPath) windowLengthInMeters = classifierInformation.getWindowLengthInMeters() multispectralImage = image_store.load(multispectralImagePath) panchromaticImage = image_store.load(panchromaticImagePath) # If no regions are defined, if not regionFrames: # Use the entire image regionFrames = [(0, 0, multispectralImage.width, multispectralImage.height)] # Open probabilityFile probabilityFile = open(targetProbabilityPath, 'wt') # For each window, for regionIndex, regionFrame in enumerate(regionFrames): print 'Scanning region %s/%s...' % (regionIndex + 1, len(regionFrames)) for window in image_process.makeWindowGenerator(multispectralImage, panchromaticImage, windowLengthInMeters, scanRatio, regionFrame): # Classify classification_string = '%s %s' % classify(imageContent=window.extractImage()) windowLocation_string = '%s %s' % window.multispectralWindowLocation probabilityFile.write('%s %s\n' % (windowLocation_string, classification_string)) # Close probabilityFile probabilityFile.close()
def evaluateWindows(probabilityPath, actualLocationPath, multispectralImagePath, windowGeoLength): # Initialize print 'Evaluating windows...' multispectralImage = image_store.load(multispectralImagePath) # Load probabilityPacks = probability_store.load(probabilityPath) windowLocations = [(x[0], x[1]) for x in probabilityPacks] windowPixelWidth, windowPixelHeight = multispectralImage.convertGeoDimensionsToPixelDimensions(windowGeoLength, windowGeoLength) windowCount = len(windowLocations) # Load predicted predictedWindowLocations = set((x[0], x[1]) for x in probabilityPacks if x[2] == 1) # Load actual actualGeoLocations = point_store.load(actualLocationPath)[0] actualPixelLocations = multispectralImage.convertGeoLocationsToPixelLocations(actualGeoLocations) actualPixelPointMachine = point_process.PointMachine(actualPixelLocations, 'INTEGER', windowPixelWidth, windowPixelHeight) actualWindowLocations = set(filter(lambda x: actualPixelPointMachine.getPointsInsideWindow(x), windowLocations)) # Get predictedNotActualWindowLocations = predictedWindowLocations - actualWindowLocations actualNotPredictedWindowLocations = actualWindowLocations - predictedWindowLocations wrongWindowLocations = predictedNotActualWindowLocations.union(actualNotPredictedWindowLocations) wrongPixelCenters = [image_store.getWindowCenter(x, windowPixelWidth, windowPixelHeight) for x in wrongWindowLocations] # Compute actualTrue_predictedFalse = len(actualNotPredictedWindowLocations) actualFalse_predictedTrue = len(predictedNotActualWindowLocations) actualTrue = len(actualWindowLocations) actualFalse = windowCount - actualTrue predictedTrue = len(predictedWindowLocations) predictedFalse = windowCount - predictedTrue percentError, falsePositiveError, falseNegativeError = computePercentages(actualTrue_predictedFalse, actualFalse_predictedTrue, actualTrue, actualFalse) # Save windowPerformance = { 'percent error': percentError, 'false positive error': falsePositiveError, 'false negative error': falseNegativeError, 'actual true': actualTrue, 'actual false': actualFalse, 'predicted true': predictedTrue, 'predicted false': predictedFalse, 'actual true predicted true': actualTrue - actualTrue_predictedFalse, 'actual true predicted false': actualTrue_predictedFalse, 'actual false predicted true': actualFalse_predictedTrue, 'actual false predicted false': actualFalse - actualFalse_predictedTrue, 'window count': windowCount, } return windowPerformance, wrongPixelCenters, actualPixelPointMachine