[Dynamic Programming] Image Compression Problem
1. Problem description:
In the computer, the gray value sequence {p1, p1, ... pn} of common pixel points represents the image. The integer pi,1<=i<=n, represents the gray value of pixel i. Usually the gray value range is 0~255. So a maximum of 8 bits are required to represent a pixel.
The principle of compression is to set breakpoints in the sequence {p1,p1,...pn} and divide it into sections. The process of segmentation is to find breakpoints, so that the maximum gray value of the pixels in a segment is relatively small, then this segment of pixels (original need 8 bits) can be represented by fewer bits (such as 7 bits), thus Reduce storage space .
b represents bits, l represents length, segment is, b[i] represents the minimum storage space required for a pixel in each segment (less than 8 bits only makes sense), l[i] represents how many pixels there are in each segment , s[i] indicates how much storage space it takes to compress from 0 to i.
If the limit l[i]<=255, 8 bits are required to represent l[i]. And b[i]<=8, requires 3 bits to represent b[i]. So the storage space required for each segment is l[i]*b[i]+11 bits. Assuming that the original image is divided into m segments, the storage space of bits is required.
The problem of image compression is to determine the optimal segmentation of the pixel sequence {p1, p1, ... pn}, so that the storage space required for this segmentation is the smallest .
2. Optimal substructure properties
Let l[i],b[i],1<=i<=m be an optimal segment of {p1,p1,...pn}, then l[1],b[1] is {p1,... ...,pl[1]} is an optimal segment, and l[i],b[i],2<=i<=m is an optimal segment of {pl[1]+1,...,pn} segment. That is, the image compression problem satisfies the optimal substructure property.
3. Recursive relationship
Let s[i], 1<=i<=n be the storage bits required for the optimal segmentation of the pixel sequence {p1,p1,...pi}, then s[i] is the storage bits of the first ik Plus the storage space of the last k. From the optimal substructure properties, we can get:
, where
4. Construct the optimal solution
The arrays l[i], b[i] record the information required for the optimal segment. The segment length and pixel bits of the last segment of the optimal segment are stored in l[n] and b[n], respectively. The segment length and pixel bits of a segment are stored in l[nl[n]] and b[nl[n]], and so on, to construct the optimal solution in O(n) time.
The specific implementation code of the algorithm is as follows:
The algorithm Compress requires only O(n) space. Since the number of cycles of j in the algorithm Compress is not more than 256, each determined i can be completed in O(1) time. So the time complexity of the whole algorithm is O(n). The execution process of the algorithm Compress can be represented as follows:
In the method Output, after outputting the minimum storage space of s[n], the s[] array is reassigned to store the position of the segment, while backtracking to construct the optimal solution. The result of running the program is as follows: