1. A method for encoding a sequence of images of pixels, wherein each image includes one or more blocks processed in a predetermined order, comprising for each block the steps of: determining a just-noticeable-distortion (JND) for the block; performing template matching on the block to select candidate blocks in the corresponding image and weights according to a structural similarity and a perceptual distortion of the block with respect to the candidate blocks, wherein the perceptual distortion is a function of the JND, and wherein the weights are equal and a function of a count of a number of the candidate blocks; determining a prediction residual, wherein the prediction residual is a difference between the input block and an output of a filter combining the candidate blocks; transforming the prediction residual to produce a transformed prediction residual; quantizing the transformed prediction residual according to the JND to produce a quantized prediction residual, and wherein the template matching and the quantizing are optimized jointly using the perceptual distortion; and entropy encoding the quantized prediction residual and the weights for a bitstream, wherein the steps are performed in an encoder.