The design of optimal stack filters under the MAE criterion is addressed in this paper. In our work, the Hasse diagram is adopted to represent the positive Boolean functions to solve the optimization problem. After problem transformation, the finding of the optimal stack filter is equivalent to the finding of the optimal on-set such that the total cost of the on-set is minimal. An efficient algorithm is developed that makes use of an important property of the optimal on-set to avoid fruitless search. It thereby greatly reduces the complexity in finding the corresponding optimal stack filter. A design example is illustrated in detail to demonstrate the optimization procedures. The proposed algorithm can generate the optimal stack filter in 1 s for the window size of 14 pixels. It can still generate the optimal stack filter for the window size of 21, although it takes about 4 h. Experimental results for real images reveal that the proposed algorithm essentially extends the maximum filter window size to make the stack filter optimization problem computationally tractable.