The image analysis software for different instruments often have some built-in routines to do this. The free software ImageJ can do it. So can the free software Gwyddion.
Are you wanting something further, such as a defined recipe? When so, you might be best to read the tutorials in these software applications.
I suggest that you follow these links to start with in addressing your issue:
Townsend et al, “Surface texture metrology for metal additive manufacturing: a review”, 2016 - https://www.researchgate.net/profile/Richard_Leach2/publication/303838249_Surface_texture_metrology_for_metal_additive_manufacturing_a_review/links/5779ff7808ae1b18a7e626a7.pdf
Rishi et al., “Particle Size and Shape Analysis using Imagej with Customized Tools for Segmentation of Particles”, 2015 - http://www.ijert.org/view-pdf/14339/particle-size-and-shape-analysis-using-imagej-with-customized-tools-for-segmentation-of-particles
The usual ISO-compliant grain analysis techniques based on image segmentation are reviewed in these papers.
In addition, further useful information about surface texture metrology is available by checking out:
Watersheds as 3D-motif method / Barré et al., “Watershed lines and catchment basins: a new 3D-motif Method”, 1999 - http://booksc.org/book/4728592/efd31f
Volume parameters / Das Neves Franco et al., “3D surface parameters (ISO 25178-2): Actual meaning of Spk and its relationship to Vmp”, 2014 - https://www.researchgate.net/publication/268750909_3D_surface_parameters_ISO_25178-2_Actual_meaning_of_Spk_and_its_relationship_to_Vmp
Autocorrelation and power spectrum density / Necas et al., “One- Dimensional autocorrelation and power spectrum density functions of irregular regions”, 2013 - https://www.researchgate.net/publication/233394946_One-dimensional_autocorrelation_and_power_spectrum_density_functions_of_irregular_regions
Areal Field Parameters: http://www.digitalsurf.fr/en/guide-areal-feature-parameters.html and http://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwj5sfuQ3KPSAhWEnRoKHXhzBXEQFggcMAA&url=http%3A%2F%2Fwww.springer.com%2Fcda%2Fcontent%2Fdocument%2Fcda_downloaddocument%2F9783642364570-c2.pdf%3FSGWID%3D0-0-45-1391727-p174915717&usg=AFQjCNHiY8Fqfk9Yc4SfMS-EqNQb8_W3Qw&sig2=RSkWdhAa5jb61b6pZ55ozg
Best regards
Article One-dimensional autocorrelation and power spectrum density f...
Article 3D surface parameters (ISO 25178-2): Actual meaning of Spk a...
Article Surface texture metrology for metal additive manufacturing: a review
It is necessary to remember one important fact: AFM measures height of peaks with extremely high resolution (less than 1 nm), but the lateral sizes are depended on geometry of the tip (it changes when scanning!). The tip can be flattened or contaminated. It is necessary to estimate diameter of the tip for precision analysis of lateral grain size. It is possible to use electron microscopy (I prefer TEM) or just scanning of a sharp calibration grids.
The example of degradated AFM probe's tip (TEM) is attached.