def getGdcTransformFromPixelTransform(imageSize, pixelTransform, refImageGdcTransform): '''Converts an image-to-image transform chained with a GDC transform to a pixel to GDC transform for this image.''' # Convert the image-to-image transform parameters to a class temp = numpy.array([tform[0:3], tform[3:6], tform[6:9]] ) imageToRefTransform = transform.ProjectiveTransform(temp) newImageSize = ImageFetcher.miscUtilities.getImageSize(newImagePath) # Generate a list of point pairs imagePoints = [] gdcPoints = [] # Loop through an evenly spaced grid of pixels in the new image # - For each pixel, compute the desired output coordinate pointPixelSpacing = (newImageSize[0] + newImageSize[1]) / 20 # Results in about 100 points for r in range(0, newImageSize[0], pointPixelSpacing): for c in range(0, newImageSize[1], pointPixelSpacing): # Get pixel in new image and matching pixel in the reference image, # then pass that into the GDC transform. thisPixel = numpy.array([float(c), float(r)]) pixelInRefImage = pixelTransform.forward(thisPixel) gdcCoordinate = refImageGdcTransform.forward(pixelInRefImage) imagePoints.append(thisPixel) gdcPoints.append(gdcCoordinate) # Compute a transform object that converts from the new image to projected coordinates imageToGdcTransform = transform.getTransform(numpy.asarray(worldPoints), numpy.asarray(gdcPoints)) return imageToGdcTransform
def _textToTransform(self, text): '''Converts a database text entry to a transform''' # Make rows seperated by ; t = text.replace('[','').replace(']',' ') parts = [float(x) for x in t.split()] mat = numpy.array([[parts[0], parts[1], parts[2]], [parts[3], parts[4], parts[5]], [parts[6], parts[7], parts[8]]], dtype='float64') tform = transform.ProjectiveTransform(mat) return tform
def convertTransformToGeo(imageToRefImageTransform, newImagePath, refImagePath, refImageGeoTransform=None): '''Converts an image-to-image homography to the ProjectiveTransform class used elsewhere in geocam. Either the reference image must be geo-registered, or the geo transform for it must be provided.''' # Convert the image-to-image transform parameters to a class temp = numpy.array([imageToRefImageTransform[0:3], imageToRefImageTransform[3:6], imageToRefImageTransform[6:9]] ) imageToRefTransform = transform.ProjectiveTransform(temp) newImageSize = IrgGeoFunctions.getImageSize(newImagePath) refImageSize = IrgGeoFunctions.getImageSize(refImagePath) # Get a pixel to GDC transform for the reference image refPixelToGdcTransform = registration_common.getPixelToGdcTransform( refImagePath, refImageGeoTransform) # Generate a list of point pairs imagePoints = [] projPoints = [] gdcPoints = [] print 'transform = \n' + str(imageToRefTransform.matrix) # Loop through an evenly spaced grid of pixels in the new image # - For each pixel, compute the desired output coordinate pointPixelSpacing = (newImageSize[0] + newImageSize[1]) / 20 # Results in about 100 points for r in range(0, newImageSize[0], pointPixelSpacing): for c in range(0, newImageSize[1], pointPixelSpacing): # Get pixel in new image and matching pixel in the reference image thisPixel = numpy.array([float(c), float(r)]) pixelInRefImage = imageToRefTransform.forward(thisPixel) # If any pixel transforms outside the reference image our transform # is probably invalid but continue on skipping this pixel. if ((not registration_common.isPixelValid(thisPixel, newImageSize)) or (not registration_common.isPixelValid(pixelInRefImage, refImageSize))): continue # Compute the location of this pixel in the projected coordinate system # used by the transform.py file. if (not refImageGeoTransform): # Use the geo information of the reference image gdcCoordinate = refPixelToGdcTransform.forward(pixelInRefImage) projectedCoordinate = transform.lonLatToMeters(gdcCoordinate) else: # Use the user-provided transform projectedCoordinate = refImageGeoTransform.forward(pixelInRefImage) gdcCoordinate = transform.metersToLatLon(projectedCoordinate) imagePoints.append(thisPixel) projPoints.append(projectedCoordinate) gdcPoints.append(gdcCoordinate) #print str(thisPixel) + ' --> ' + str(gdcCoordinate) + ' <--> ' + str(projectedCoordinate) # Compute a transform object that converts from the new image to projected coordinates #print 'Converting transform to world coordinates...' #testImageToProjectedTransform = transform.getTransform(numpy.asarray(worldPoints), # numpy.asarray(imagePoints)) testImageToProjectedTransform = transform.ProjectiveTransform.fit(numpy.asarray(projPoints), numpy.asarray(imagePoints)) testImageToGdcTransform = transform.ProjectiveTransform.fit(numpy.asarray(gdcPoints), numpy.asarray(imagePoints)) #print refPixelToGdcTransform #print testImageToProjectedTransform #for i, w in zip(imagePoints, worldPoints): # print str(i) + ' --> ' + str(w) + ' <--> ' + str(testImageToProjectedTransform.forward(i)) return (testImageToProjectedTransform, testImageToGdcTransform, refPixelToGdcTransform)
def getIdentityTransform(): '''Return an identity transform from the transform.py file''' return transform.ProjectiveTransform(numpy.matrix([[1,0,0],[0,1,0],[0,0,1]],dtype='float64'))