def label_ice_shelves(mask): mask_grounded = 1 mask_floating = 2 nrows,ncols = mask.shape # identify "ice rises" mask1 = np.zeros_like(mask) mask1[mask == mask_grounded] = 2 mask1[nrows/2,ncols/2] = 1 # in continent "ice rises" are 1, everything else is zero continent = cc.cc(mask1, True) # incorporate the continental ice sheet in continent continent[mask == mask_grounded] += 1 # create a mask with ice shelves connected by "ice rises" mask2 = np.zeros_like(mask) mask2[mask == mask_floating] = 1 mask2[continent == 2] = 1 # label ice shelves shelves = cc.cc(mask2, False) # remove ice rises from ice shelves shelves[continent == 2] = 0 return continent, shelves
I, t = test_image() print "Preparing input took %f s" % t # original plt.figure(figsize=(12,4)) plt.subplot(1,3,1) plt.imshow(np.ma.array(I,mask=I==0), interpolation='nearest') plt.xticks([]) plt.yticks([]) plt.title("original image") plt.xlabel("land is blue, floating ice is red") # labeled components tic = time.clock() result1 = cc.cc(I) toc = time.clock() print "Computing labels took %f s" % (toc - tic) plt.subplot(1,3,2) plt.imshow(np.ma.array(result1, mask=result1==0), interpolation='nearest') plt.xticks([]) plt.yticks([]) plt.title("labeled image") plt.xlabel("a unique label for each component") # "iceberg" identification tic = time.clock() result2 = cc.cc(I, identify_icebergs=True) toc = time.clock()
I, t = test_image() print "Preparing input took %f s" % t # original plt.figure(figsize=(12, 4)) plt.subplot(1, 3, 1) plt.imshow(np.ma.array(I, mask=I == 0), interpolation='nearest') plt.xticks([]) plt.yticks([]) plt.title("original image") plt.xlabel("land is blue, floating ice is red") # labeled components tic = time.clock() result1 = cc.cc(I) toc = time.clock() print "Computing labels took %f s" % (toc - tic) plt.subplot(1, 3, 2) plt.imshow(np.ma.array(result1, mask=result1 == 0), interpolation='nearest') plt.xticks([]) plt.yticks([]) plt.title("labeled image") plt.xlabel("a unique label for each component") # "iceberg" identification tic = time.clock() result2 = cc.cc(I, identify_icebergs=True) toc = time.clock() print "Computing labels took %f s" % (toc - tic)
#!/usr/bin/env python import cc import netCDF4 as NC import numpy as np import pylab as plt import argparse parser = argparse.ArgumentParser() parser.description = "Identify icebergs using a PISM-style mask read from a file." parser.add_argument("FILE", nargs=1) options = parser.parse_args() nc = NC.Dataset(options.FILE[0]) pism_mask = np.squeeze(nc.variables['mask'][:]) pism_mask_grounded = 2 pism_mask_floating = 3 cc_mask = np.zeros_like(pism_mask) cc_mask[pism_mask == pism_mask_grounded] = 1 cc_mask[pism_mask == pism_mask_floating] = 2 result = cc.cc(cc_mask, True) # result is zero everywhere, except 1 if it is an iceberg result[pism_mask == pism_mask_grounded] = 2 # mark grounded areas plt.imshow(result, interpolation='nearest') plt.title("PISM mask read from %s after identifying icebergs" % options.FILE[0]) plt.show()