PanheadGG
PanheadGG
发布于 2025-12-27 / 39 阅读

数据结构实验三:树的实验

题目

四叉树编码 (quedreecoding),又称四分树、四元树编码,是一种基于递归细分区域的栅格数据压缩方法。其原理是将2n×2n像元阵列划分为四个大小相同的象限,逐层检测子区域属性一致性,若相同则停止分割,直至达到单个像元或符合预设条件的复合类型,最终形成树状分层结构。

编写一个程序实现四叉树图像压缩,主要思路是对于某个象限中的像素最大值与最小值相差不超过某个值e,则不再进行分割,该象限中的像素值用象限内像素均值替代。步骤如下:

1.读取一幅灰度图,构造四叉树

2.通过四叉树还原该图片,并显示出来

3.调整e的大小,实现不同清晰度的图片显示,要求不少于三幅图片。

注意:不局限于定义中的2n*2n图像,任意图像都可以四分切割。

代码

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QuadtreeCoding {
    static class NodeImage{
        static int TOP_LEFT = 0;
        static int TOP_RIGHT = 1;
        static int BOTTOM_LEFT = 2;
        static int BOTTOM_RIGHT = 3;

        int[] data;
        NodeImage[] children = null;
        int width;
        int height;

        public NodeImage(BufferedImage image) {
            this.width = image.getWidth();
            this.height = image.getHeight();
            this.data = new int[width*height];

            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    int rgb = image.getRGB(x, y);
                    int r = (rgb >> 16) & 0xFF;
                    int g = (rgb >> 8) & 0xFF;
                    int b = rgb & 0xFF;
                    int gray = (int) (0.299 * r + 0.587 * g + 0.114 * b);
                    this.data[x+y*width] = gray;
                }
            }
        }

        private NodeImage(int[] grayImage, int width, int height) {
            this.data = grayImage;
            this.width = width;
            this.height = height;
        }

        public BufferedImage getImage(int e) {
            int max = Integer.MIN_VALUE;
            int min = Integer.MAX_VALUE;
            for (int value : data) {
                max = Math.max(max, value);
                min = Math.min(min, value);
            }

            if (max - min <= e || width == 1 || height == 1) {
                int total = 0;
                for (int value : data) {
                    total += value;
                }
                int average = total / data.length;
                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        int rgb = (average << 16) | (average << 8) | average;
                        image.setRGB(x, y, rgb);
                    }
                }
                return image;
            }

            children = new NodeImage[4];
            int subWidth = width / 2;
            int subHeight = height / 2;

            int[][] subData = new int[4][];

            subData[TOP_LEFT] = new int[subWidth * subHeight];
            int index = 0;
            for (int y = 0; y < subHeight; y++) {
                for (int x = 0; x < subWidth; x++) {
                    subData[TOP_LEFT][index++] = this.data[x + y * this.width];
                }
            }
            children[TOP_LEFT] = new NodeImage(subData[TOP_LEFT], subWidth, subHeight);

            subData[TOP_RIGHT] = new int[(width - subWidth) * subHeight];
            index = 0;
            for (int y = 0; y < subHeight; y++) {
                for (int x = subWidth; x < width; x++) {
                    subData[TOP_RIGHT][index++] = this.data[x + y * this.width];
                }
            }
            children[TOP_RIGHT] = new NodeImage(subData[TOP_RIGHT], width - subWidth, subHeight);

            subData[BOTTOM_LEFT] = new int[subWidth * (height - subHeight)];
            index = 0;
            for (int y = subHeight; y < height; y++) {
                for (int x = 0; x < subWidth; x++) {
                    subData[BOTTOM_LEFT][index++] = this.data[x + y * this.width];
                }
            }
            children[BOTTOM_LEFT] = new NodeImage(subData[BOTTOM_LEFT], subWidth, height - subHeight);

            subData[BOTTOM_RIGHT] = new int[(width - subWidth) * (height - subHeight)];
            index = 0;
            for (int y = subHeight; y < height; y++) {
                for (int x = subWidth; x < width; x++) {
                    subData[BOTTOM_RIGHT][index++] = this.data[x + y * this.width];
                }
            }
            children[BOTTOM_RIGHT] = new NodeImage(subData[BOTTOM_RIGHT], width - subWidth, height - subHeight);

            BufferedImage[] subImages = new BufferedImage[4];
            for (int i = 0; i < 4; i++) {
                subImages[i] = children[i].getImage(e);
            }

            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            for (int y = 0; y < subHeight; y++) {
                for (int x = 0; x < subWidth; x++) {
                    result.setRGB(x, y, subImages[TOP_LEFT].getRGB(x, y));
                }
            }

            for (int y = 0; y < subHeight; y++) {
                for (int x = subWidth; x < width; x++) {
                    result.setRGB(x, y, subImages[TOP_RIGHT].getRGB(x - subWidth, y));
                }
            }

            for (int y = subHeight; y < height; y++) {
                for (int x = 0; x < subWidth; x++) {
                    result.setRGB(x, y, subImages[BOTTOM_LEFT].getRGB(x, y - subHeight));
                }
            }

            for (int y = subHeight; y < height; y++) {
                for (int x = subWidth; x < width; x++) {
                    result.setRGB(x, y, subImages[BOTTOM_RIGHT].getRGB(x - subWidth, y - subHeight));
                }
            }

            return result;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File("image.png"));
        NodeImage nodeImage = new NodeImage(image);

        int[] eArr = new int[]{5,10,20,50,100,200};

        for(int e:eArr){
            BufferedImage image1 = nodeImage.getImage(e);
            ImageIO.write(image1, "png", new File("result-e="+e+".png"));
            System.out.println("保存图片: result-e="+e+".png");
        }

    }
}

示例

输入图片示例

image.png

输出图片示例

result-e=5

result-e=10

result-e=20

result-e=50

result-e=100

result-e=200


评论