Java 如何将图像量化为MxN块

Java 如何将图像量化为MxN块,java,image,graphics,ascii,Java,Image,Graphics,Ascii,当然,我正在写一个关于图像到ASCII转换的程序。我已经将图像转换为灰度,但我不知道如何将图像量化为MxN块 对于每个MxN大小的子图像,计算其平均灰度值 将计算出的平均灰度值存储到新图像中。 节目如下: public static char[][] imageToASCII(Image img, int blockWidth, int blockHeight) { { // Convert image from type Image to BufferedImage

当然,我正在写一个关于图像到ASCII转换的程序。我已经将图像转换为灰度,但我不知道如何将图像量化为MxN块

对于每个MxN大小的子图像,计算其平均灰度值 将计算出的平均灰度值存储到新图像中。 节目如下:

public static char[][] imageToASCII(Image img, int blockWidth, int blockHeight)
{
    {
        // Convert image from type Image to BufferedImage
        BufferedImage bufImg = convert(img);

        // Scan through each row of the image
        for(int j=0; j<bufImg.getHeight(); j++)
        {
            // Scan through each columns of the image
            for(int i=0; i<bufImg.getWidth(); i++)
            {
                // Returns an integer pixel in the default RGB color model
                int values=bufImg.getRGB(i,j);
                // Convert the single integer pixel value to RGB color
                Color oldColor = new Color(values);

                int red = oldColor.getRed();        // get red value
                int green = oldColor.getGreen();    // get green value
                int blue = oldColor.getBlue();  // get blue value

                // Convert RGB to grayscale using formula
                // gray = 0.299 * R + 0.587 * G + 0.114 * B
                double grayVal = 0.299*red + 0.587*green + 0.114*blue;

                // Assign each channel of RGB with the same value
                Color newColor = new Color((int)grayVal, (int)grayVal, (int)grayVal);

                // Get back the integer representation of RGB color
                // and assign it back to the original position
            bufImg.setRGB(i, j, newColor.getRGB());

        }           
    return null;

也许您只需要将图像分割为MxN大小的块。这类似于视频编码中的微块