PURPOSE: To improve encoding efficiency by using a template optimum to the local feature of a picture element to be an object to be encoded.
CONSTITUTION: At the time of encoding, every time the encoding of the prescribed number of the picture elements is finished, the number of coincidences with the color of a remarked picture element is counted respectively in respect of the picture elements neighboring the remarked picture element in a prescribed-reference range, and a prescribed number of reference picture elements are detected starting from the picture element of the larger number of the coincidences, and the template of a shape including these plural detected reference picture elements is generated newly by a template generating part 1, and henceforward, this new template is used. At the time of decoding, every time the decoding of the prescribed number of the picture elements is finished, the number of the coincidences with the color of the remarked picture element is counted respectively in respect of the picture elements neighboring the remarked picture element finished in its decoding in the prescribed reference range, and the prescribed number of the reference picture elements are detected starting from the picture element of the larger number of the coincidences, and the template of the shape including these plural detected reference picture elements is generated newly by the template generating part 1, and henceforward, this new template is used.