A relaxation result for a class of variational problems with a lack of compactness