We segment an image of a porous structure by successively identifying individual grains, using a process that requires no manual initialization. Adaptive thresholding is used to extract an incomplete edge map from the image. Then, seed points are created on a rectangular grid. Rays are cast from each point to identify the local grain. The grain with the best shape is selected by energy minimization, and the grain is used to update the edge map. This is repeated until all the grains have been recognized. Tests on scanning electron microscope images of titanium oxide and aluminium oxide show that their process achieves better results than five other contour detection techniques.