Generalizing the Motzkin-Straus theorem to edge-weighted graphs, with applications to image segmentation