The thickness of two-dimensional (2D) nanomaterials shows a significant effect on their optical and electrical properties. Therefore, a rapid and automatic detection technology of 2D nanomaterials with desired layer-number is required to extend their practical application in optoelectronic devices. In this paper, an image recognition technology was proposed for rapid and reliable identification of thin-layer WS2 samples, which combining a layer-thickness identification criterion and a novel image segmentation algorithm. The criterion stemmed from optical contrast study of monochromatic illumination photographs, and the algorithm was based on Canny operator and edge connection iteration. This optical identification method can seek out thin-layer WS2 samples on complex surfaces, which provides a promising approach for automatic search of thin-layer nanomaterials.