Most schemes for determining the mass function of virialized objects from the statistics of the initial density perturbation field suffer from the "cloud-in-cloud" problem of miscounting the number of low-mass clumps, many of which would have been subsumed into larger objects. We propose a solution based on the theory of the excursion sets of F(r, R(f)), the four-dimensional initial density perturbation field smoothed with a continuous hierarchy of filters of radii R(f). We identify the mass fraction of matter in virialized objects with mass greater than M with the fraction of space in which the initial density contrast lies above a critical overdensity when smoothed on some filter of radius greater than or equal to R(f)(M). The differential mass function is then given by the rate of first upcrossings of the critical overdensity level as one decreases R(f) at constant position r. The shape of the mass function depends on the choice of filter function. The simplest case is "sharp k-space" filtering, in which the field performs a Brownian random walk as the resolution changes. The first upcrossing rate can be calculated analytically and results in a mass function identical to the formula of Press and Schechter-complete with their normalizing "fudge factor" of 2. For general filters (e.g., Gaussian or "top hat") no analogous analytical result seems possible, though we derive useful analytical upper and lower bounds. For these cases, the mass function can be calculated by generating an ensemble of field trajectories numerically. We compare the results of these calculations with group catalogs found from N-body simulations. Compared to the sharp k-space result, less spatially extended filter functions give fewer large-mass and more small-mass objects. Over the limited mass range probed by the N-body simulations, these differences in the predicted abundances are less than a factor of 2 and span the values found in the simulations. Thus the mass functions for sharp k-space and more general filtering all fit the N-body results reasonably well. None of the filter functions is particularly successful in identifying the particles which form low-mass groups in the N-body simulations, illustrating the limitations of the excursion set approach. We have extended these calculations to compute the evolution of the mass function in regions that are constrained to lie within clusters or under-densities at the present epoch. These predictions agree well with N-body results, although the sharp k-space result is slightly preferred over the Gaussian or top hat results.