The chief concern of engineers is stabilization on building a road on saturated clay.The analysis of soil microstructure is of great'importance for preventing the collapse.sinking and distortion of groundwork.Porosity is one of parameters to analyse soil microstructure.This paper deals with a method based on the gray scale of soil microstructure graph to compute porosity.The scientific quality and feasibility can be guaranteed by the algorithm of porosity based on tri-dimensional space.The graphs can be directly used.The algorithm is proved to be more scientific.