In this paper, we propose a novel stochastic model based on the quadtree, so that our model effectively represents the variable block size segmentation of images. Then, we construct the Bayes code for the proposed stochastic model. In general, the computational cost to calculate the posterior distribution required in the Bayes code increases exponentially with respect to the data size. However, we introduce an efficient algorithm to calculate it in the polynomial order of the data size without loss of the optimality. Some experiments are performed to confirm the flexibility of the proposed stochastic model and the efficiency of the introduced algorithm.