def loadGeorefData(self): cvlog("Loading georef data...") if os.path.splitext(self.filename)[1] == ".jpg": errorLog, self.georefData = georef.load(self.filename, self.im_data.width(), self.im_data.height(), self.config.scaling) else: errorLog, self.georefData = georef.load(self.filename) return errorLog
def loadImageData(self): cvlog("Loading image...") print("################### self.filename: %s " % (self.filename)) self.im_data.img, self.config.scaling, errorLog = il.load(self.filename) #self.im_data.weedCSV = pd.read_csv(self.config.weedCSV) #Wallace #self.im_data.weedSelection = self.config.weedSelection #Wallace if self.im_data.img is not None: # compensate shadow + generate HSV # self.im_data.img = il.compensate_shadow(self.im_data) ### Saurabh added compensate shadow # self.im_data.writeDebug('ak_comshadow.jpg', self.im_data.img) self.im_data.hsv_img = cv2.cvtColor(self.im_data.img, cv2.COLOR_BGR2HSV) return errorLog
def addTree(self, tree): hash = encode.encode(tree.gpsPos.lat, tree.gpsPos.lon) t = self.geohashTileMapping.get(hash) try: self.tiles[t[0]].geohashContents[t[1]].trees.append(tree) self.contours.append(tree.contour) # Added by Nisith return self.tiles[t[0]].geohashContents[t[1]] except Exception: cvlog("hash: " + str(hash) + " " + str(tree.gpsPos.lat) + " " + str(tree.gpsPos.lon)) cvlog("t: " + str(t)) return None
def createTilesPx(georefData, pixWidth, pixHeight, im_data): print("Creating Tiles in Px") print("pixWidth = " + str(pixWidth)) print("pixHeight = " + str(pixHeight)) hashedTiles = {} key = "" limit = 75 # sck: 3m = 75 sck : original = 100, since 0.04m per pixel, edit = 25 #3 meter/ resolusi. tileCountX = int(pixWidth / limit) cvlog("tileCountX = " + str(tileCountX)) # number of tiles in X direction tileCountY = int(pixHeight / limit) cvlog("tileCountY = " + str(tileCountY)) # number of tiles in Y direction widthPx = [] heightPx = [] widthTile = limit heightTile = limit cvlog("widthTile = " + str(widthTile)) # Tile Width cvlog("heightTile = " + str(heightTile)) # Tile Height for i in range(tileCountX + 1): if pixWidth - (i * widthTile) < 10: continue widthPx.append(i * widthTile) # cvlog("widthPx = "+str(widthPx)) cvlog("widthPxLen = " + str(len(widthPx))) for i in range(tileCountY + 1): if pixHeight - (i * heightTile) < 10: continue heightPx.append(i * heightTile) # cvlog("heightPx = "+str(heightPx)) cvlog("heightPxLen = " + str(len(heightPx))) tileWidth = widthTile tileHeight = heightTile # widthPx = np.linspace(0, pixWidth, sampleCountX,dtype=int) # widthPx = [int(i) for i in width] # print "lons: "+str(lons) # heightPx = np.linspace(0, pixHeight, sampleCountY,dtype=int) # heightPx = [int(i) for i in height] # print "lats: "+str(lats) '''tileWidth = math.floor(pixWidth / (sampleCountX-1)) cvlog("tileWidth"+str(tileWidth)) tileHeight = math.floor(pixHeight / (sampleCountY-1)) cvlog("tileHeight"+str(tileHeight))''' # hCount = 0 vis_tiles = im_data.copy() plt.imshow(vis_tiles) # petak_name = im_data.config.input_file.split('.')[0] petak_name = petak # outDir = im_data.config.output_dir+'/'+petak_name gridCsv = open(petak_name + "_grid_1M.csv", 'w') s = 'Grids\n' gridCsv.write(s) for i in range(len(widthPx)): # wCount = 0 # newline = 1 # Added by Nisith for visualizing grid s = '' for j in range(len(heightPx)): px1 = (int(widthPx[i]), int(heightPx[j])) # cvlog("px1: "+str(px1)) # print type(lons[i]) if i == len(widthPx) - 1 and j == len(heightPx) - 1: px2 = (pixWidth, pixHeight) # cvlog("Bottom-Right Tile") # cvlog(str(px1)) # cvlog(str(px2)) if px1[1] == px2[1]: continue elif i == len(widthPx) - 1 and j != len(heightPx) - 1: px2 = (pixWidth, int(heightPx[j + 1])) # cvlog("Righmost Tile along row "+str( j)) # cvlog(str(px1)) # cvlog(str(px2)) if px1[0] == px2[0]: continue elif i != len(widthPx) - 1 and j == len(heightPx) - 1: px2 = (int(widthPx[i + 1]), pixHeight) # cvlog("Bottomost Tile along column "+str(i)) # cvlog(str(px1)) # cvlog(str(px2 )) if px1[1] == px2[1]: continue else: px2 = (int(widthPx[i + 1]), int(heightPx[j + 1])) # cvlog("In between tiles") # cvlog(str(px1)) # cvlog(str(px2)) # try: # cvlog("px2: "+str(px2)) # cvlog( "len(hashedTiles): "+ str(len(hashedTiles))) tilePx = cvTilePx(len(hashedTiles) + 1, px1, px2, [], i + 1, j + 1) # except Exception: # cvlog("Tile not created") whiteArea = np.array([255, 255, 255]) # print "New" # print px1[0]+1,px1[1]+1,im_data.img[px1[0]+1,px1[1]+1] # print px1[0]+1,px2[1]-1,im_data.img[px1[0]+1,px2[1]-1] # print px2[0]-1,px1[1]+1,im_data.img[px2[0]-1,px1[1]+1] # print px2[0]-1,px2[1]-1,im_data.img[px2[0]-1,px2[1]-1] # print px1[0]+1,px2[1]-1,im_data.img[int((px2[0]-px1[0])/2), int((px2[1]-px1[1])/2)] # print int((px2[0]-px1[0])/2),int((px1[1]-px2[1])/2),im_data.img[int((px2[0]-px1[0])/2), int((px1[1]-px2[1])/2)] # if (im_data.img[px1[0]+1,px1[1]+1]==whiteArea).all() and (im_data.img[px2[0]-1,px1[1]+1]==whiteArea).all() and (im_data.img[px2[0]-1,px2[1]-1]==whiteArea).all() and (im_data.img[px1[0]+1,px2[1]-1]==whiteArea).all() and (im_data.img[int((px2[0]-px1[0])/2), int((px2[1]-px1[1])/2)]).all() : tileNotValid = 0 if (im_data[px1[1] + 1, px1[0] + 1] == whiteArea).all(): tileNotValid += 1 if (im_data[px1[1] + 1, px2[0] - 1] == whiteArea).all(): tileNotValid += 1 if (im_data[px2[1] - 1, px2[0] - 1] == whiteArea).all(): tileNotValid += 1 if (im_data[px2[1] - 1, px1[0] + 1] == whiteArea).all(): tileNotValid += 1 if (im_data[int((px2[1] - px1[1]) / 2), int((px2[0] - px1[0]) / 2)] == whiteArea).all(): tileNotValid += 1 # cvlog("tileNotValidCount="+str(tileNotValid)) if tileNotValid > 1: continue topLeftGpsPos = pixelToLatLon(georefData, px1[0], px1[1]) topRightGpsPos = pixelToLatLon(georefData, px2[0], px1[1]) bottomRightPos = pixelToLatLon(georefData, px2[0], px2[1]) bottomLeftPos = pixelToLatLon(georefData, px1[0], px2[1]) # s= str(px1GPS.lon)+'|'+ str(px1GPS.lat)+'|'+ str(px2GPS.lon)+'|'+ str(px2GPS.lat)+'\n' s = s + 'POLYGON ((' s = s + str(topLeftGpsPos.lon) + ' ' + str(topLeftGpsPos.lat) + ',' s = s + str(topRightGpsPos.lon) + ' ' + str( topRightGpsPos.lat) + ',' s = s + str(bottomRightPos.lon) + ' ' + str( bottomRightPos.lat) + ',' s = s + str(bottomLeftPos.lon) + ' ' + str(bottomLeftPos.lat) + ',' s = s + str(topLeftGpsPos.lon) + ' ' + str(topLeftGpsPos.lat) s = s + '))' + '\n' gridCsv.write(s) key = hashTreePx(widthPx[i], tileWidth) + hashTreePx( heightPx[j], tileHeight) # cvlog("key:" + hashTreePx(widthPx[i] ,tileWidth)+ "+" + hashTreePx(heightPx[j] ,tileHeight) ) hashedTiles[key] = tilePx # cvlog( "key: "+ key) cv2.rectangle(vis_tiles, tilePx.topLeftPix, tilePx.bottomRightPix, (0, 0, 255), 2) gridCsv.close() # im_data.writeDebug('tilesHashed.jpg', vis_tiles) return hashedTiles, tileWidth, tileHeight
def testConversions(georefData, x, y): lat, lon = pixelToLatLon(georefData, x, y) cvlog("lat,lon : " + str(lat) + "," + str(lon)) x, y = latLonToPixel(georefData, lat, lon) cvlog("x,y: " + str(x) + "," + str(y)) e, n = pixelToNE(georefData, x, y) cvlog("e,n: " + str(e) + "," + str(n)) x, y = NEToPixel(georefData, e, n) cvlog("x,y: " + str(x) + "," + str(y)) lat2, lon2 = NEToLatLon(georefData, e, n) cvlog("lat,lon : " + str(lat2) + "," + str(lon2)) e2, n2 = latLonToNE(georefData, lat, lon) cvlog("e,n: " + str(e2) + "," + str(n2)) lat2, lon2 = NEToLatLon(georefData, 800000, 25000) e2, n2 = latLonToNE(georefData, lat2, lon2) cvlog("e,n: " + str(e2) + "," + str(n2)) lat2, lon2 = NEToLatLon(georefData, e2, n2) e2, n2 = latLonToNE(georefData, lat2, lon2) cvlog("e2,n2: " + str(e2) + "," + str(n2))
def initializeJgw(georefData, values, prj, width, height, scaling): for i in range(0, 4): values[i] = float(values[i]) / float(scaling) if prj is not None: prSplit = prj.split("PROJCS[\"UTM Zone ") ### to accomodated our prj format with " " as "_", use > 1 if len(prSplit) > 1: correctEPSG = len(prSplit) - 1 format = prSplit[correctEPSG] prSplit2 = format.split(",") prSplit3 = prSplit2[0].split("N") prSplit4 = prSplit3[0].split("S") format = int(prSplit2[0]) prSplit = prj.split(" Hemisphere") prSplit2 = prSplit[0].split(",") if (prSplit2[1] == " Northern"): format = format + 32600 else: format = format + 32700 else: prSplit = prj.split("PROJCS[\"UTM_Zone_") if len(prSplit) < 2: return "Error: Not supported projection!" #cvlog(str(prSplit[1])) correctEPSG = len(prSplit) - 1 prSplit2 = prSplit[correctEPSG] prSplit3 = prSplit2.split("_") format = int(prSplit3[0]) if (prSplit3[1] == "Northern"): format = format + 32600 else: format = format + 32700 else: format = 32647 # Backup option, may not work correct! print(format) spatialRef = osr.SpatialReference() spatialRef.ImportFromEPSG(format) target = osr.SpatialReference() target.ImportFromEPSG(4326) cvlog("Input EPSG format:" + str(format)) georefData.coordTransNEToGPS = osr.CoordinateTransformation( spatialRef, target) georefData.coordTransGPSToNE = osr.CoordinateTransformation( target, spatialRef) values = np.array(map(float, values)) print values if not values is None: # Add origin to the point list point = ogr.Geometry(ogr.wkbPoint) point.AddPoint(values[4], values[5]) WGS84 = ogr.Geometry(ogr.wkbMultiPoint) WGS84.AddGeometry(point) # Add Bottom-Right point to the point list x = 0 y = 0 print("inside condition") if (values[2] == 0) and (values[1] == 0): x = values[4] + (width - 1) * values[0] + (height - 1) * values[1] y = values[5] + (width - 1) * values[2] + (height - 1) * values[3] else: return "Error: Affine transformation not implemented. " georefData.topLeftNE = cvNEPos(values[5], values[4]) georefData.bottomRightNE = cvNEPos(y, x) point.AddPoint(x, y) WGS84.AddGeometry(point) # Make the transformation from input coordinate system to the output (WGS84) WGS84.Transform(georefData.coordTransNEToGPS) points = Geometry2List(WGS84) georefData.topLeftGps = cvGPSPos(points[0][1], points[0][0]) georefData.bottomRightGps = cvGPSPos(points[1][1], points[1][0]) cvlog("Top-Left- and Bottom-Right-coordinates for the image:" + str(georefData.topLeftGps) + ", " + str(georefData.bottomRightGps)) cvlog('georefData Saurabh image is ' + str(georefData)) georefData.radPerPixX = (georefData.bottomRightGps.lon - georefData.topLeftGps.lon) / (width - 1) georefData.radPerPixY = (georefData.bottomRightGps.lat - georefData.topLeftGps.lat) / (height - 1) georefData.metersPerPixelX = values[0] georefData.metersPerPixelY = values[3] georefData.metersPerPixel = (abs(georefData.metersPerPixelX) + abs(georefData.metersPerPixelY)) / 2 cvlog("Pixel size: " + str(georefData.metersPerPixel) + " meters / pixel") return ""
def writeDebug(self, filename, img): if self.config.writeEnabled == 1: cvlog("Writing " + self.config.debugprefix + "_" + filename) cv2.imwrite(self.config.debugprefix + "_" + filename, img)