This paper introduces an approach for handling complex labelling problems driven by local constraints. The purpose is illustrated by two applications: detection of the road network on radar satellite images, and recognition of the cortical sulci on MRI images. Features must be initially extracted from the data to build a "feature graph" with structural relations. The goal is to endow each feature with a label representing either a specific object (recognition), or a class of objects (detection). Some contextual constraints have to be respected during this labelling. They are modelled by Markovian potentials assigned to the labellings of "feature clusters". The solution of the labelling problem is the minimum of the energy defined by the sum of the local potentials. This paper develops a method for learning these local potentials using a "congregation" of neural networks and supervised learning.