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. The immersed boundary technique has been coupled with a Lagrangian optimization in a three-dimensional framework. Four roughness elements with different heights have been studied, inducing amplification mechanisms that bypass the asymptotical growth of Tollmien–Schlichting waves. 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. This result demonstrates the relevance of varicose instabilities for such a flow and shows a different behavior with respect to the secondary instability theory of boundary layer streaks.