Microstructure of a material determines the transport, chemical and mechanical properties. Geological materials and geomaterials are imaged using microscopy tools. The microscopy images are analyzed to better understand the microstructural topology and morphology. Image segmentation is an essential step prior to the microstructural analysis. In this study, we trained a Random Forest classifier to relate certain features corresponding to each pixel and its neighboring pixels in a scanning electron microscopy (SEM) image of shale to a specific component type; thereby developing a methodology to segment SEM images of shale samples into 4 component types, namely, pore/crack, organic/kerogen, matrix and pyrite. We evaluate the generalization capability of the ML-assisted image-segmentation method by using SEM maps from Wolfcamp and Barnett shale formations. The two formations differ in topology, morphology and distribution of the four components. 16 features were computed and then implemented when building the classifier for the desired segmentation. The features belong to seven categories that define each pixel along with its neighboring pixels based on spatial information. The segmentation workflow was rigorously tested on the inner-region and outer-region pixels. The deployment of the workflow takes an average of 50 seconds to segment 1 image slice of size 2058-by-2606 pixels using an Intel (R) Xeon (R) CPU E5-1603 @ 2080GHz with 32GB RAM. We use F1 score and confusion matrix to present the performance of the segmentation models. Models trained on Wolfcamp-Shale SEM images can robustly detect matrix and pyrite in Barnett-Shale SEM images, but poorly perform for pore/crack and kerogen/organic components, where kerogen/organic pixels are wrongly segmented as pore/crack pixels. Models trained on Barnett-Shale SEM images can robustly detect pyrite in Wolfcamp-Shale SEM images, but poorly perform for the remaining components, where pore/crack pixels are wrongly segmented as kerogen/organic pixels or matrix pixels, and kerogen/organic pixels are wrongly segmented as matrix pixels. Model trained on Barnett shale cannot detect cracks in the Wolfcamp samples because the presence of cracks is very limited in the Barnett samples.