Пример #1
0
def get_craters(lroc_csv_path, head_csv_path, sub_cdim, R_km):
    sys.path.append('../../DeepMoon/')
    import input_data_gen as igen
    craters = igen.ReadLROCHeadCombinedCraterCSV(filelroc=lroc_csv_path,
                                                 filehead=head_csv_path)
    craters = igen.ResampleCraters(craters, sub_cdim, None, arad=R_km)
    return craters
Пример #2
0
 def test_resamplecraters(self, llbd, minpix):
     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_sub = self.craters.loc[ctr_xlim & ctr_ylim, :]
     imgheight = int(3000. * (llbd[3] - llbd[2]) / 180.)
     pixperkm = trf.km2pix(imgheight, llbd[3] - llbd[2])
     minkm = minpix / pixperkm
     ctr_sub = ctr_sub[ctr_sub['Diameter (km)'] >= minkm]
     ctr_sub.reset_index(drop=True, inplace=True)
     ctr_rs = igen.ResampleCraters(self.craters, llbd, imgheight,
                                   minpix=minpix)
     assert np.all(ctr_rs == ctr_sub)
Пример #3
0
        size = comm.Get_size()
        print("Thread {0} of {1}".format(rank, size))
        istart = rank * amt
    else:
        istart = 0

    # Read source image and crater catalogs.
    img = Image.open(source_image_path).convert("L")
    craters = igen.ReadLROCHeadCombinedCraterCSV(filehead=head_csv_path)

    # Sample subset of image.  Co-opt igen.ResampleCraters to remove all
    # craters beyond cdim (either sub or source).
    if sub_cdim != source_cdim:
        img = igen.InitialImageCut(img, source_cdim, sub_cdim)
    # This always works, since sub_cdim < source_cdim.
    craters = igen.ResampleCraters(craters, sub_cdim, None, arad=R_km)

    # Generate input images.
    igen.GenDataset(img,
                    craters,
                    outhead,
                    rawlen_range=rawlen_range,
                    rawlen_dist=rawlen_dist,
                    ilen=ilen,
                    cdim=sub_cdim,
                    arad=R_km,
                    minpix=minpix,
                    tglen=tglen,
                    binary=True,
                    rings=True,
                    ringwidth=ringwidth,
Пример #4
0
def GenDataset(box_list, img, craters, outhead, arad, cdim):

    truncate = True
    ringwidth = 1
    rings = True
    binary = True
    minpix = 1.
    tglen = 256
    ilen = 256
    amt = len(box_list)
    origin = "upper"

    # Get craters.
    igen.AddPlateCarree_XY(craters, list(img.size), cdim=cdim, origin=origin)
    iglobe = ccrs.Globe(semimajor_axis=arad * 1000.,
                        semiminor_axis=arad * 1000.,
                        ellipse=None)

    # Initialize output hdf5s.
    [
        imgs_h5, imgs_h5_inputs, imgs_h5_tgts, imgs_h5_llbd, imgs_h5_box,
        imgs_h5_dc, imgs_h5_cll, craters_h5
    ] = init_files(outhead, amt, ilen, tglen)

    for i in range(amt):

        # Current image number.
        img_number = "img_{:05d}".format(i)

        # Determine image size to crop.
        box = box_list[i]
        #print("Generating {} current crop: ({})".format(img_number,box))

        # Load necessary because crop may be a lazy operation; im.load() should
        # copy it.  See <http://pillow.readthedocs.io/en/3.1.x/
        # reference/Image.html>.
        im = img.crop(box)
        im.load()

        # Obtain long/lat bounds for coordinate transform.
        ix = box[::2]
        iy = box[1::2]
        llong, llat = trf.pix2coord(ix,
                                    iy,
                                    cdim,
                                    list(img.size),
                                    origin=origin)
        llbd = np.r_[llong, llat[::-1]]

        # Downsample image.
        im = im.resize([ilen, ilen], resample=Image.NEAREST)

        # Remove all craters that are too small to be seen in image.
        ctr_sub = igen.ResampleCraters(craters,
                                       llbd,
                                       im.size[1],
                                       arad=arad,
                                       minpix=minpix)

        # Convert Plate Carree to Orthographic.
        [imgo, ctr_xy, distortion_coefficient,
         clonglat_xy] = (igen.PlateCarree_to_Orthographic(im,
                                                          llbd,
                                                          ctr_sub,
                                                          iglobe=iglobe,
                                                          ctr_sub=True,
                                                          arad=arad,
                                                          origin=origin,
                                                          rgcoeff=1.2,
                                                          slivercut=0.5))

        if imgo is None:
            print("Discarding narrow image")
            continue

        imgo_arr = np.asanyarray(imgo)
        assert imgo_arr.sum() > 0, ("Sum of imgo is zero!  There likely was "
                                    "an error in projecting the cropped "
                                    "image.")

        # Make target mask.  Used Image.BILINEAR resampling because
        # Image.NEAREST creates artifacts.  Try Image.LANZCOS if BILINEAR still
        # leaves artifacts).
        tgt = np.asanyarray(
            imgo.resize((tglen, tglen), resample=Image.BILINEAR))
        mask = igen.make_mask(ctr_xy,
                              tgt,
                              binary=binary,
                              rings=rings,
                              ringwidth=ringwidth,
                              truncate=truncate)

        # Output everything to file.
        output_to_file(img_number, i, imgs_h5_inputs, imgo_arr, imgs_h5_tgts,
                       mask, imgs_h5_box, box, imgs_h5_llbd, llbd, imgs_h5_dc,
                       distortion_coefficient, imgs_h5_cll, clonglat_xy,
                       craters_h5, ctr_xy, imgs_h5)
    imgs_h5.close()
    craters_h5.close()
Пример #5
0
    def test_gendataset(self, tmpdir, ringwidth):
        amt = 10
        zeropad = 2
        outhead = str(tmpdir.join('gentest'))

        igen.GenDataset(self.img, self.craters, outhead,
                        rawlen_range=[300, 1000], rawlen_dist='log',
                        ilen=self.imlen, tglen=self.imlen, cdim=self.cdim,
                        minpix=1, ringwidth=ringwidth, amt=amt, istart=0,
                        seed=self.seed)

        imgs_h5 = h5py.File(outhead + '_images.hdf5', 'r')
        craters_h5 = pd.HDFStore(outhead + '_craters.hdf5', 'r')

        for i in range(amt):
            # Find image number.
            img_number = "img_{i:0{zp}d}".format(i=i, zp=zeropad)

            # Load box.
            box = np.array(imgs_h5['pix_bounds'][img_number][...])

            im = self.img.crop(box)
            im.load()

            # Obtain long/lat bounds for coordinate transform.
            ix = box[::2]
            iy = box[1::2]
            llong, llat = trf.pix2coord(ix, iy, self.cdim, list(self.img.size),
                                        origin='upper')
            llbd = np.r_[llong, llat[::-1]]

            # Downsample image.
            im = im.resize([self.imlen, self.imlen], resample=Image.NEAREST)

            # Remove all craters that are too small to be seen in image.
            ctr_sub = igen.ResampleCraters(self.craters, llbd, im.size[1],
                                           minpix=1)

            # Convert Plate Carree to Orthographic.
            [imgo, ctr_xy, distortion_coefficient, clonglat_xy] = (
                igen.PlateCarree_to_Orthographic(
                    im, llbd, ctr_sub, iglobe=self.iglobe, ctr_sub=True,
                    slivercut=0.5))
            imgo_arr = np.asanyarray(imgo)

            # Make target mask.
            tgt = np.asanyarray(imgo.resize((self.imlen, self.imlen),
                                            resample=Image.BILINEAR))
            mask = igen.make_mask(ctr_xy, tgt, binary=True, rings=True,
                                  ringwidth=ringwidth, truncate=True)

            assert np.all(imgo_arr == imgs_h5['input_images'][i, ...])
            assert np.all(mask == imgs_h5['target_masks'][i, ...])
            assert np.all(llbd == imgs_h5['longlat_bounds'][img_number][...])
            assert (distortion_coefficient ==
                    imgs_h5['pix_distortion_coefficient'][img_number][0])
            assert np.all(clonglat_xy[["x", "y"]] ==
                          imgs_h5['cll_xy'][img_number][...])
            assert np.all(ctr_xy == craters_h5[img_number])

        imgs_h5.close()
        craters_h5.close()