ABSTRACT Porosity is an important parameter that affects the deformation, mechanical properties, and constitutive relationships of soil structures. It can be used as an important basis for deterioration research and safety assessment in the field of earth site protection. By collecting scanning electron microscope images (SEM) of the rammed earth of the ancient city wall of Pingyao, a world cultural heritage, and using software such as ImageJ and MATLAB to analyze the characteristics of the images from two-dimensional and three-dimensional perspectives, the relationship between threshold and porosity was studied and summarized. It was found that using the Boltzmann function eigenvalue as the control threshold can well characterize the two-dimensional porosity of soil. Using the idea of integration, the binary slice images with different thresholds are simplified and accumulated to construct a three-dimensional image porosity calculation model. Write a three-dimensional pore characteristic parameter program to extract the statistical image grayscale information and perform normal fitting. Based on the probability interval of the image grayscale distribution, calculate the threshold interval that can effectively reflect the three-dimensional pore characteristics of the soil. This research has important theoretical and practical significance for the protection research of earthen sites.