def InitialImageCut(img, cdim, newcdim): """Crops image, so that the crop output can be used in GenDataset. Parameters ---------- img : PIL.Image.Image Image cdim : list-like Coordinate limits (x_min, x_max, y_min, y_max) of image. newcdim : list-like Crop boundaries (x_min, x_max, y_min, y_max). There is currently NO CHECK that newcdim is within cdim! Returns ------- img : PIL.Image.Image Cropped image """ x, y = trf.coord2pix(np.array(newcdim[:2]), np.array(newcdim[2:]), cdim, img.size, origin="upper") # y is backward since origin is upper! box = [x[0], y[1], x[1], y[0]] img = img.crop(box) img.load() return img
def AddPlateCarree_XY(craters, imgdim, cdim=[-180., 180., -90., 90.], origin="upper"): """Adds x and y pixel locations to craters dataframe. Parameters ---------- craters : pandas.DataFrame Crater info imgdim : list, tuple or ndarray Length and height of image, in pixels cdim : list-like, optional Coordinate limits (x_min, x_max, y_min, y_max) of image. Default is [-180., 180., -90., 90.]. origin : "upper" or "lower", optional Based on imshow convention for displaying image y-axis. "upper" means that [0,0] is upper-left corner of image; "lower" means it is bottom-left. """ x, y = trf.coord2pix(craters["Long"].as_matrix(), craters["Lat"].as_matrix(), cdim, imgdim, origin=origin) craters["x"] = x craters["y"] = y
def WarpCraterLoc(craters, geoproj, oproj, oextent, imgdim, llbd=None, origin="upper"): """Wrapper for WarpImage that adds padding to warped image to make it the same size as the original. Parameters ---------- craters : pandas.DataFrame Crater info geoproj : cartopy.crs.Geodetic instance Input lat/long coordinate system oproj : cartopy.crs.Projection instance Output coordinate system oextent : list-like Coordinate limits (x_min, x_max, y_min, y_max) of output imgdim : list, tuple or ndarray Length and height of image, in pixels llbd : list-like Long/lat limits (long_min, long_max, lat_min, lat_max) of image origin : "lower" or "upper" Based on imshow convention for displaying image y-axis. "upper" means that [0,0] is upper-left corner of image; "lower" means it is bottom-left. Returns ------- ctr_wrp : pandas.DataFrame DataFrame that includes pixel x, y positions """ # Get subset of craters within llbd limits if llbd is None: ctr_wrp = craters else: ctr_xlim = ((craters["Long"] >= llbd[0]) & (craters["Long"] <= llbd[1])) ctr_ylim = ((craters["Lat"] >= llbd[2]) & (craters["Lat"] <= llbd[3])) ctr_wrp = craters.loc[ctr_xlim & ctr_ylim, :].copy() # Get output projection coords. # [:,:2] becaus we don't need elevation data # If statement is in case ctr_wrp has nothing in it if ctr_wrp.shape[0]: ilong = ctr_wrp["Long"].as_matrix() ilat = ctr_wrp["Lat"].as_matrix() res = oproj.transform_points(x=ilong, y=ilat, src_crs=geoproj)[:, :2] # Get output ctr_wrp["x"], ctr_wrp["y"] = trf.coord2pix(res[:, 0], res[:, 1], oextent, imgdim, origin=origin) else: ctr_wrp["x"] = [] ctr_wrp["y"] = [] return ctr_wrp
def test_pix2coord(self, origin): x, y = trf.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) cx, cy = trf.pix2coord(x, y, self.cdim, self.imgdim, origin=origin) cxy = np.r_[cx, cy] cxy_gt = np.r_[self.cx, self.cy] assert np.all(np.isclose(cxy, cxy_gt, rtol=1e-7, atol=1e-10))
def test_warpcraters(self, box): """Test image warping and padding. Output of this function was tested by visual inspection. """ box = np.array(box, dtype='int32') # Crop image. img = np.asanyarray(self.img.crop(box)) # Determine long/lat and output projection. llbd = self.get_llbd(box) oproj = ccrs.Orthographic(central_longitude=np.mean(llbd[:2]), central_latitude=np.mean(llbd[2:]), globe=self.iglobe) # Determine coordinates of image limits in input and output projection # coordinates. iextent, oextent, ores = self.get_extents(llbd, self.geoproj, self.iproj, oproj) # Obtain image from igen.WarpImagePad. imgout_WarpImagePad, WIPsize, WIPoffset = igen.WarpImagePad( img, self.iproj, iextent, oproj, oextent, origin="upper", rgcoeff=1.2, fillbg="black") ctr_xlim = ((self.craters["Long"] >= llbd[0]) & (self.craters["Long"] <= llbd[1])) ctr_ylim = ((self.craters["Lat"] >= llbd[2]) & (self.craters["Lat"] <= llbd[3])) ctr_wrp = self.craters.loc[ctr_xlim & ctr_ylim, :].copy() # Get output projection coords. # [:,:2] becaus we don't need elevation data # If statement is in case ctr_wrp has nothing in it if ctr_wrp.shape[0]: ilong = ctr_wrp["Long"].as_matrix() ilat = ctr_wrp["Lat"].as_matrix() res = oproj.transform_points(x=ilong, y=ilat, src_crs=self.geoproj)[:, :2] # Get output ctr_wrp["x"], ctr_wrp["y"] = trf.coord2pix(res[:, 0], res[:, 1], oextent, WIPsize, origin="upper") else: ctr_wrp["x"] = [] ctr_wrp["y"] = [] ctr_wrpctrloc = igen.WarpCraterLoc(self.craters, self.geoproj, oproj, oextent, WIPsize, llbd=llbd, origin="upper") assert np.all(ctr_wrp == ctr_wrpctrloc)
def test_coord2pix(self, origin): x_gt = (self.imgdim[0] * (self.cx - self.cdim[0]) / (self.cdim[1] - self.cdim[0])) y_gt = (self.imgdim[1] * (self.cy - self.cdim[2]) / (self.cdim[3] - self.cdim[2])) yi_gt = (self.imgdim[1] * (self.cdim[3] - self.cy) / (self.cdim[3] - self.cdim[2])) x, y = trf.coord2pix(self.cx, self.cy, self.cdim, self.imgdim, origin=origin) if origin == "upper": y_gt_curr = yi_gt else: y_gt_curr = y_gt xy = np.r_[x, y] xy_gt = np.r_[x_gt, y_gt_curr] assert np.all(np.isclose(xy, xy_gt, rtol=1e-7, atol=1e-10))