This work provides a three-dimensional energy optimization analysis, looking for perturbations inducing the largest energy growth at a finite time in a boundary-layer flow in the presence of roughness elements. Amplification mechanisms are described which by-pass the asymptotical growth of Tollmien–Schlichting waves. The immersed boundary technique has been coupled with a Lagrangian optimization in a three-dimensional framework. Two types of roughness elements have been studied, characterized by a different height. The results show that even very small roughness elements, inducing only a weak deformation of the base flow, can strongly localize the optimal disturbance. Moreover, the highest value of the energy gain is obtained for a varicose perturbation, pointing out the importance of varicose instabilities for such a flow and a different behavior with respect to the secondary instability theory of boundary layer streaks.