A novel algorithm for shape detection based on mathematical morphology is presented. Two stages are involved. In the first stage, a shape model is learned automatically from learning examples belonging to the same object class. It is a collection of subparts with the description of relations among subparts, represented by a fuzzy graph. In the second stage, the generated model is used to detect similar shapes from images of complex real scenes. Subparts of the shape are detected in sequence based on their saliency, and then the geometric configuration among those detected subparts is checked. A morphological component detector is proposed to detect each subpart by using a soft structuring element, derived from the shape model. Satisfactory results are shown when testing the algorithm on synthetic and real images.