• 使用Java对书籍照片进行字符分割


    使用java来处理印刷体汉字图片,并进行分割,最终保存单个汉字图片,便于后续的文字匹配或者识别

    处理逻辑

    • 对图片进行二值化,可以将图片分割成多个小图片,依次二值化,效果更好
    • 对字符进行描黑处理,将相邻的字符连成一片,便于后续确定字符行的位置
    • 使用连通分量来将这些黑色块进行分类,一个黑色块对应一个连通分量,所有的黑色像素都在连通分量里面
    • 根据连通分量可以得到字符行(黑色块)的上下边界,从而可以提取出来对应位置的字符行
    • 对字符行进行字符分割,分割的位置可以通过字符行在横轴上的投影来确定

    问题

    • 有一些字符不能正确的分割,比如【“儿】会将双引号和“儿”左半边合在一起
    • 对于倒S形的行,没有进行变形处理,只是简单的分割
    • 没有进行噪点处理,分割后的字符会有问题,比如“一”字下面有个噪点,结果就是“一”字横在图片上方
    • 处理速度暂时提高不了,1500*2000的800汉字图片,需要2秒左右执行完
    • 这些分割后的字符暂时做不了匹配,也做不了识别,后面看有没有机会做下
    • 深度学习文字识别

    引入jar包

    
        net.coobird
        thumbnailator
        0.4.12
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    执行方式

    先执行图片二值化处理 test0811() 方法,再执行字符分割 test1012() 方法

    java实现代码

    import net.coobird.thumbnailator.Thumbnails;
    import org.junit.Test;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.*;
    
    public class Test20220810 {
    
        // 横向切分块数
        private static final int widthSplitNum = 12;
    
        // 纵向切分块数
        private static final int heightSplitNum = 14;
    
        // 面积小于50像素的字符,舍弃掉
        private static final int ignoreArea = 50;
    
        /**
         * 批量二值化图片,可用
         *
         * @throws IOException
         */
        @Test
        public void test0811() throws IOException {
            // 指定原始照片文件夹
            String fromPic = "E:\\documents\\DCIM\\unfinished\\test2";
            File directory = new File(fromPic);
    
            if (!directory.isDirectory()) {
                return;
            }
    
            for (File file : directory.listFiles()) {
    
                BufferedImage bufferedImage = ImageIO.read(file);
    
                if (bufferedImage.getWidth() < 1000) {
                    continue;
                }
    
                // 压缩图片
                BufferedImage compactImage = Thumbnails.of(bufferedImage).size(2000, 2000).asBufferedImage();
    
                // 切割图片,分别处理,防止出现大片黑斑
                Map<Integer, BufferedImage> map = splitImage(compactImage);
    
                // 二值化
                for (Integer key : map.keySet()) {
                    BufferedImage binaryImage = binaryImage(map.get(key));
                    map.put(key, binaryImage);
                }
    
                // 合并图片
                BufferedImage destImage = mergeImage(map);
    
    			// 黑白图片输出目录
                File newFile = new File("E:\\documents\\DCIM\\unfinished\\test\\" + file.getName().replace(".jpg", "") + ".png");
                ImageIO.write(destImage, "png", newFile);
            }
        }
    
        @Test
        public void test1012() throws IOException {
            long start = System.currentTimeMillis();
            // 待分割的黑白图片存放路径
            String filePath = "E:\\documents\\DCIM\\unfinished\\test2";
            File directory = new File(filePath);
    
            if (!directory.isDirectory()) {
                return;
            }
    
            int i = 200;
            for (File file : directory.listFiles()) {
                BufferedImage bufferedImage = ImageIO.read(file);
    
                // 去除图片左右两边的污点
                BufferedImage cutLRImage = cutLR(bufferedImage);
    
                // 将字符行染黑
                BufferedImage expandWhite = expandWhite2(cutLRImage);
    
                // 根据上面确定的字符行位置,提取出字符行
                List<BufferedImage> list = getCharRow4(expandWhite, cutLRImage);
    
    
                // 切割字符
                for (BufferedImage item : list) {
                    List<BufferedImage> charImages = splitChar3(item);
    
                    for (BufferedImage item2 : charImages) {
                    	// 单个字符的存储路径
                        File newFile = new File("d:\\temp\\" + (i++) + ".png");
                        ImageIO.write(item2, "png", newFile);
                        double height = item2.getHeight() * 1.5;
                        if (item2.getWidth() > height && item2.getWidth() < item2.getHeight() * 3) {
                            System.out.println((i - 1) + ".png");
                        }
                    }
                }
            }
    
            System.out.println("耗时2:" + (System.currentTimeMillis() - start) + "ms");
        }
    
        @Test
        public void test1018() throws IOException {
            long start = System.currentTimeMillis();
    
            // 黑白图片
            String fromPic = "d:\\IMG_20221007_143930.png";
            //String fromPic = "d:\\IMG_20221001_084253.png";
            //String fromPic = "d:\\IMG_20221007_153516.png";
            File file = new File(fromPic);
    
            BufferedImage bufferedImage = ImageIO.read(file);
    
            // 去除图片左右两边的污点
            BufferedImage cutLRImage = cutLR(bufferedImage);
    
            // 将字符行染黑
            BufferedImage expandWhite = expandWhite2(cutLRImage);
    
    
            //System.out.println("耗时-1:" + (System.currentTimeMillis()- start) + "ms");
    
            File newFile2 = new File("d:\\bb.png");
            ImageIO.write(cutLRImage, "png", newFile2);
            //System.out.println("耗时0:" + (System.currentTimeMillis()- start) + "ms");
    
            // 根据上面确定的字符行位置,提取出字符行
            List<BufferedImage> list = getCharRow4(expandWhite, cutLRImage);
    
            //System.out.println("耗时1:" + (System.currentTimeMillis()- start) + "ms");
    
            int i = 200;
            // 切割字符
            for (BufferedImage item : list) {
                List<BufferedImage> charImages = splitChar3(item);
    
                /*for (BufferedImage item2 : charImages) {
                    File newFile = new File("d:\\temp\\" + (i++) + ".png");
                    ImageIO.write(item2, "png", newFile);
                    double height = item2.getHeight()*1.5;
                    if (item2.getWidth() > height && item2.getWidth() < item2.getHeight() * 3) {
                        System.out.println((i-1) + ".png");
                    }
                }*/
            }
    
            System.out.println("耗时2:" + (System.currentTimeMillis()- start) + "ms");
    
        }
    
        /**
         * 同一水平线上的两个黑色像素点,如果他们之家的间距小于设定的阈值,那么将他们之间的所有白色像素点变成黑色像素点
         * @param bufferedImage
         * @return
         */
        private BufferedImage expandWhite2(BufferedImage bufferedImage) {
            BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
    
            //long start1 = System.currentTimeMillis();
            grayImage.setData(bufferedImage.getData());
            //System.out.println("expandWhite2耗时1:" + (System.currentTimeMillis()- start1) + "ms");
    
    
    
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
    
            map3 = new HashMap<>();
            for (int col=0; col<height; col++) {
                for (int row = 0; row < width; row++) {
                    int color = bufferedImage.getRGB(row, col);
                    if ((color & 0xff) == 0) {
                        map3.put(col, 1);
                        break;
                    }
                }
            }
    
            // 抹黑字符,确定字符行的位置,便于后续的调整行间距
            // 从左往右扫描,遇到黑色像素,就将他左边的像素抹黑
            int threshold = 120;
    
            // 第一个黑色像素点下标
            int start = -1;
            int end = -1;
            // 两个黑色像素点之间的白色像素点个数
            int whiteCount = 0;
            for (int j=0; j<height; j++) {
                if (!map3.containsKey(j)) {
                    continue;
                }
    
                for (int i=0; i<width; i++) {
    
                    int color = grayImage.getRGB(i, j);
                    int red = color & 0xFF;
    
                    // 黑色像素点
                    if (red == 0) {
                        if (start == -1) {
                            start = i;
                        } else {
                            if (whiteCount == 0) {
                                start = i;
                            } else {
                                if (whiteCount < threshold) {
                                    for (int k=start+1; k<i; k++) {
                                        grayImage.setRGB(k, j, 0);
                                    }
                                }
                                start = i;
                                whiteCount = 0;
                            }
                        }
                    } else if (red == 255 && start != -1) {
                        // 白色像素点
                        whiteCount++;
                    }
                }
    
                start = -1;
                whiteCount = 0;
            }
    
            //System.out.println("expandWhite2耗时2" + (System.currentTimeMillis()- start1) + "ms");
            return grayImage;
        }
    
        /**将白色像素膨胀为两倍,黑色像素保持不变,便于分割字符串
         *
         * @param bufferedImage
         * @return
         */
        private BufferedImage expandWhite3(BufferedImage bufferedImage) {
            BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth()*2, bufferedImage.getHeight(), bufferedImage.getType());
    
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
    
            for (int col=0; col<height; col++) {
                for (int row = 0; row < width; row++) {
                    int color = bufferedImage.getRGB(row, col);
                    if (color == -1) {
                        map3.put(col, 1);
                    } else {
                        grayImage.setRGB(row, col, 0);
                    }
                }
            }
    
            //long start1 = System.currentTimeMillis();
            grayImage.setData(bufferedImage.getData());
            //System.out.println("expandWhite2耗时1:" + (System.currentTimeMillis()- start1) + "ms");
    
    
    
    
            map3 = new HashMap<>();
            for (int col=0; col<height; col++) {
                for (int row = 0; row < width; row++) {
                    int color = bufferedImage.getRGB(row, col);
                    if ((color & 0xff) == 0) {
                        map3.put(col, 1);
                        break;
                    }
                }
            }
    
            // 抹黑字符,确定字符行的位置,便于后续的调整行间距
            // 从左往右扫描,遇到黑色像素,就将他左边的像素抹黑
            int threshold = 120;
    
            // 第一个黑色像素点下标
            int start = -1;
            int end = -1;
            // 两个黑色像素点之间的白色像素点个数
            int whiteCount = 0;
            for (int j=0; j<height; j++) {
                if (!map3.containsKey(j)) {
                    continue;
                }
    
                for (int i=0; i<width; i++) {
    
                    int color = grayImage.getRGB(i, j);
                    int red = color & 0xFF;
    
                    // 黑色像素点
                    if (red == 0) {
                        if (start == -1) {
                            start = i;
                        } else {
                            if (whiteCount == 0) {
                                start = i;
                            } else {
                                if (whiteCount < threshold) {
                                    for (int k=start+1; k<i; k++) {
                                        grayImage.setRGB(k, j, 0);
                                    }
                                }
                                start = i;
                                whiteCount = 0;
                            }
                        }
                    } else if (red == 255 && start != -1) {
                        // 白色像素点
                        whiteCount++;
                    }
                }
    
                start = -1;
                whiteCount = 0;
            }
    
            //System.out.println("expandWhite2耗时2" + (System.currentTimeMillis()- start1) + "ms");
            return grayImage;
        }
    
        /**
         * 处理整个图片
         * 截图,去除左右边框
         * @param bufferedImage
         * @return
         */
        private BufferedImage cutLR(BufferedImage bufferedImage) {
            //long start = System.currentTimeMillis();
    
            // 横坐标上的映射图片,如果首行连续空白像素的个数超过阈值,就认为连续空白像素前面的都是污点,需要清除掉
            int threshold = 20;
    
            int ignoreHeight = 3;
    
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
    
            // 存储黑色行号,key为x轴坐标,value为对应x坐标上的黑色像素个数
            Map<Integer, Integer> map = new HashMap<>();
    
            // 统计
            for (int i = 0; i < width; i++) {
                int blackCount = 0;
                for (int j = 0; j < height; j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        blackCount++;
                        if (blackCount > ignoreHeight) {
                            break;
                        }
                    }
                }
    
                map.put(i, blackCount);
            }
            //System.out.println("耗时-4:" + (System.currentTimeMillis()- start) + "ms");
    
            int whiteCount = 0;
            int startIndex = -1;
            int endIndex = width-1;
    
            // 找出起始像素点
            for (int i = 0; i < width/2; i++) {
                int value = map.get(i);
    
                // x轴上像素个数小于3的都认为是空白
                if (value < ignoreHeight) {
                    whiteCount++;
                    if (whiteCount >= threshold) {
                        startIndex = i;
                    }
                } else {
                    whiteCount = 0;
                }
            }
    
            // 找出结束像素点
            whiteCount = 0;
            for (int i = width-1; i > width/2; i--) {
                int value = map.get(i);
    
                if (value < ignoreHeight) {
                    whiteCount++;
                    if (whiteCount >= threshold) {
                        endIndex = i;
                    }
                } else {
                    whiteCount = 0;
                }
            }
    
    
            // 截掉图片左右两个空白部分
            BufferedImage grayImage = bufferedImage.getSubimage(startIndex, 0, endIndex-startIndex, height);
    
            /*BufferedImage grayImage = new BufferedImage(endIndex-startIndex, height, bufferedImage.getType());
            int newWidth = endIndex-startIndex;
    
            for (int i = 0; i < newWidth; i++) {
                for (int j = 0; j < height; j++) {
                    int color = bufferedImage.getRGB(startIndex+i, j);
                    grayImage.setRGB(i, j, color);
                }
            }*/
    
            //System.out.println("耗时-3:" + (System.currentTimeMillis()- start) + "ms");
    
            return grayImage;
        }
    
        /**
         * 去除单个字符上下边框
         * @param bufferedImage
         * @return
         */
        private BufferedImage cutUD(BufferedImage bufferedImage) {
            int startIndex = 0;
    
            // 找到起始像素点
            out:
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                for (int i = 0; i < bufferedImage.getWidth(); i++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        startIndex = j;
                        break out;
                    }
                }
            }
    
            // 找到结束像素点
            int endIndex = bufferedImage.getWidth()-1;
            out:
            for (int j = bufferedImage.getHeight()-1; j > -1; j--) {
                for (int i = 0; i < bufferedImage.getWidth(); i++) {
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        endIndex = j;
                        break out;
                    }
                }
            }
    
            // 截掉图片上下两个空白部分
    
            //BufferedImage grayImage = bufferedImage.getSubimage(0,startIndex,bufferedImage.getWidth(), endIndex-startIndex+1);
    
            /*BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), endIndex-startIndex+1, bufferedImage.getType());
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < endIndex-startIndex+1; j++) {
                    int color = bufferedImage.getRGB(i, startIndex + j);
                    grayImage.setRGB(i, j, color);
                }
            }*/
    
            return bufferedImage.getSubimage(0, startIndex, bufferedImage.getWidth(), endIndex-startIndex+1);
        }
    
        /**
         * 去除单个字符左右边框
         * @param bufferedImage
         * @return
         */
        private BufferedImage cutLR2(BufferedImage bufferedImage) {
            int startIndex = 0;
    
            // 找到起始像素点
            out:
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        startIndex = i;
                        break out;
                    }
                }
            }
    
            // 找到结束像素点
            int endIndex = bufferedImage.getWidth()-1;
            out:
            for (int i = bufferedImage.getWidth()-1; i > -1; i--) {
            for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        endIndex = i;
                        break out;
                    }
                }
            }
    
            return bufferedImage.getSubimage(startIndex, 0, endIndex-startIndex+1, bufferedImage.getHeight());
        }
    
        /**
         * 对一行字符进行分割
         * 紧贴分割,且不会造成将一个正常字符分割成两个部分的情况
         * 如果过度分割,那么就进行合并
         * 合并的原理就是只要两个相邻的字符合并后不超过最大字符宽度,就认为是一个字符,否则就是两个字符
         * @param bufferedImage
         * @return
         */
        private List<BufferedImage> splitChar3(BufferedImage bufferedImage) {
            List<BufferedImage> charImages = new ArrayList<>();
    
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            // 横坐标上的映射图片,如果x坐标上的像素个数小于这个阈值,就认为是噪点,舍弃掉,值越大,达到分割的间距越小,分的越细
            //int threshold = 2;
            // 字符间距,值越小,达到分割的间距越小,分的越细
            //int interval = 1;
    
            // 横坐标上的映射图片,如果x坐标上的像素个数小于这个阈值,就认为是噪点,舍弃掉,值越大,达到分割的间距越小,分的越细
            int threshold = 1;
            // 字符间距,值越小,达到分割的间距越小,分的越细
            int interval = 2;
    
            // 存储黑色像素在x轴上的映射,key为x轴坐标,value为对应x坐标上的黑色像素个数
            Map<Integer, Integer> map = new HashMap<>();
    
            // 存储字符起始和结束下标,key为起始下标,value为结束下标后一位,二者差就是字符实际宽度
            Map<Integer, Integer> charsMap = new LinkedHashMap<>();
            // 统计
            for (int i = 0; i < width; i++) {
                int blackCount = 0;
                for (int j = 0; j < height; j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        blackCount++;
                        if (blackCount > threshold) {
                            break;
                        }
                    }
                }
    
                map.put(i, blackCount);
            }
    
            // 从左到右遍历时,记录白色像素点的宽度,超过这个值,就表示当前处于字符之间的空白处
            int whiteCount = 0;
            int startIndex = -1;
    
            // 单个字符的最小宽度,用于判断当前字符是否需要合并
            // 存在“出”字被分成两个部分,左边宽度大于bufferedImage.getHeight()*2/3,右边宽度为1像素,导致不能合并,需要增大minWidth
            //int minWidth = bufferedImage.getHeight()*2/3;
            // 对于字符行是弯曲的情况,直接使用图片高度不适用
            //int minWidth = bufferedImage.getHeight();
    
            // 对于“小”字这种宽度大于高度的,如果他所在行的有效高度正好等于他的高度,就会造成”小“被分割成两个部分,所以需要额外增加1/20和一个像素
            int maxHeight = getMaxHeight(bufferedImage);
            int minWidth = maxHeight + maxHeight/20 + 1;
    
            // 单个字符的最大宽度
            // 存在“呀,”合并的情况,因为它们的宽度小于bufferedImage.getHeight()*5/4,需要缩小他的值
            //int maxWidth = bufferedImage.getHeight()*5/4;
            // 对于字符行是弯曲的情况,直接使用图片高度不适用
            //int maxWidth = bufferedImage.getHeight();
    
            int maxWidth = minWidth;
            int lastKey = -1;
            int lastValue = -1;
            // 找出起始像素点
            for (int i = 0; i < width; i++) {
                int value = map.get(i);
    
                // 字符起始下标
                if (startIndex == -1) {
                    // 对于高度小于设定阈值的坐标,直接舍弃
                    if (value >= threshold) {
                        startIndex = i;
                    }
                } else {
                    // x轴上像素个数小于3的都认为是空白
                    if (value < threshold) {
                        whiteCount++;
                    } else {
                        // 对于一个字符中间存在空白的情况,需要排除掉
                        whiteCount = 0;
                    }
    
                    if (whiteCount > interval) {
                        int charWidth = i-interval-startIndex;
                        // 防止一个字符被分割成两个部分
                        if (charWidth < minWidth) {
                            // 如果是类似“儿”这样的左右分开的字符
                            if (lastKey == -1) {
                                lastKey = startIndex;
                                lastValue = i-interval;
                            } else {
                                if (i-interval-lastKey > maxWidth) {
                                    charsMap.put(lastKey, lastValue);
                                    lastKey = startIndex;
                                    lastValue = i-interval;
                                } else {
                                    /*charsMap.put(lastKey, i-interval);
                                    lastKey = -1;
                                    lastValue = -1;*/
    
                                    // 存在一个字符被分割成多个情况,这时需要多次合并
                                    lastValue = i-interval;
                                }
                            }
                        } else {
                            if (lastKey != -1) {
                                charsMap.put(lastKey, lastValue);
                                lastKey = -1;
                                lastValue = -1;
                            }
                            charsMap.put(startIndex, i-interval);
                        }
    
                        startIndex = -1;
                        whiteCount = 0;
                    }
                }
            }
    
            // 处理行尾的字符
            if (lastKey != -1 && startIndex != -1) {
                int i = width;
    
                // 后一个字符的宽度
                int charWidth = i-interval-startIndex;
                // 防止一个字符被分割成两个部分
                if (charWidth < minWidth) {
                    // 如果是类似“儿”这样的左右分开的字符
                    if (i-interval-lastKey > maxWidth) {
                        charsMap.put(lastKey, lastValue);
                        charsMap.put(startIndex, i-interval);
                    } else {
                        // 存在一个字符被分割成多个情况,这时需要多次合并
                        charsMap.put(lastKey, i-interval);
                    }
                } else {
                    charsMap.put(lastKey, lastValue);
                    charsMap.put(startIndex, i-interval);
                }
                lastKey = -1;
                lastValue = -1;
                startIndex = -1;
            }
    
    
            if (lastKey != -1 && startIndex == -1) {
                // 过滤噪点,暂时不考虑与最后一个字符合并
                if (lastValue - lastKey > 1) {
                    charsMap.put(lastKey, lastValue);
                    lastKey = -1;
                    lastValue = -1;
                }
            }
    
            if (startIndex != -1 && lastKey == -1) {
                charsMap.put(startIndex, width);
                startIndex = -1;
            }
    
            // 根据上面已经标注好的字符下标,提取字符图片
            for (Integer key : charsMap.keySet()) {
                int value = charsMap.get(key);
                if (value <= key) {
                    continue;
                }
    
                BufferedImage grayImage = null;
    
                try {
                    grayImage = bufferedImage.getSubimage(key,0,value-key,height);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                /*BufferedImage grayImage = new BufferedImage(value-key, height, bufferedImage.getType());
                for (int i = 0; i < value-key; i++) {
                    for (int j = 0; j < height; j++) {
                        int color = bufferedImage.getRGB(key+i, j);
                        grayImage.setRGB(i, j, color);
                    }
                }*/
    
    
                // 清除无效字符
                if (isInvalidChar(grayImage)) {
                    continue;
                }
    
                // 清除字符上下空白部分
                BufferedImage cutUDImage = cutUD(grayImage);
    
                // 再次进行分割
                //if (cutUDImage.getWidth() > bufferedImage.getHeight() && cutUDImage.getWidth() > cutUDImage.getHeight()*2) {
                // 案例:“这样”字符宽80像素,高39像素,maxWidth=42,不满足cutUDImage.getWidth() > maxWidth*1.92,没有进行分割
                //if (cutUDImage.getWidth() > maxHeight*1.92) {
                if (cutUDImage.getWidth() > maxHeight*splitThreshold) {
                    List<BufferedImage> list2 = splitChar4(cutUDImage);
                    charImages.addAll(list2);
                } else {
                    charImages.add(cutUDImage);
                }
            }
    
            // “个儿”这两个字符会分成三个部分,需要将后面两个部分合在一起
            for (int i=0; i<charImages.size()-1; i++) {
                BufferedImage bufferedImage1 = charImages.get(i);
    
                if (bufferedImage1.getWidth() * 2 < bufferedImage1.getHeight()
                        && bufferedImage1.getWidth() * 3 > bufferedImage1.getHeight()) {
                    BufferedImage bufferedImage2 = charImages.get(i+1);
    
                    // 两个字符的宽度和高度都差不多,且高度都大致等于宽度的两倍
                    if (bufferedImage2.getWidth() * 1.8 < bufferedImage2.getHeight()
                            && bufferedImage2.getWidth() * 3 > bufferedImage2.getHeight()
                            && Math.abs(bufferedImage1.getHeight()-bufferedImage2.getHeight()) < 5) {
    
                        // 两个字符合并后中间的空白宽度
                        int blankWidth = 4;
    
                        int newWidth = bufferedImage1.getWidth() + blankWidth + bufferedImage2.getWidth();
                        int newHeight = Math.max(bufferedImage1.getHeight(), bufferedImage2.getHeight());
                        // 进行合并
                        BufferedImage grayImage2 = new BufferedImage(newWidth, newHeight, bufferedImage.getType());
    
                        // 设置白色背景
                        for (int row=0; row<newWidth; row++) {
                            for (int col=0; col<newHeight; col++) {
                                grayImage2.setRGB(row, col, 0xFFFFFF);
                            }
                        }
    
                        for (int row=0; row<bufferedImage1.getWidth(); row++) {
                            for (int col=0; col<bufferedImage1.getHeight(); col++) {
                                grayImage2.setRGB(row, col, bufferedImage1.getRGB(row, col));
                            }
                        }
    
                        for (int row=0; row<bufferedImage2.getWidth(); row++) {
                            for (int col=0; col<bufferedImage2.getHeight(); col++) {
                                grayImage2.setRGB(row+bufferedImage1.getWidth()+blankWidth, col, bufferedImage2.getRGB(row, col));
                            }
                        }
    
                        charImages.set(i, grayImage2);
                        charImages.remove(i+1);
    
                        i++;
                    }
                }
            }
    
            return charImages;
        }
    
        private double splitThreshold = 1.4;
    
        /**
         * 调小分割的间隙,再进行分割
         * @param bufferedImage
         * @return
         */
        private List<BufferedImage> splitChar4(BufferedImage bufferedImage) {
            List<BufferedImage> charImages = new ArrayList<>();
    
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            // 横坐标上的映射图片,如果x坐标上的像素个数小于这个阈值,就认为是噪点,舍弃掉,值越大,达到分割的间距越小,分的越细
            //int threshold = 2;
            // 字符间距,值越小,达到分割的间距越小,分的越细
            //int interval = 1;
    
            // 横坐标上的映射图片,如果x坐标上的像素个数小于这个阈值,就认为是噪点,舍弃掉,值越大,达到分割的间距越小,分的越细
            int threshold = 2;
            // 字符间距,值越小,达到分割的间距越小,分的越细
            int interval = 1;
    
            // 存储黑色像素在x轴上的映射,key为x轴坐标,value为对应x坐标上的黑色像素个数
            Map<Integer, Integer> map = new HashMap<>();
    
            // 存储字符起始和结束下标,key为起始下标,value为结束下标
            Map<Integer, Integer> charsMap = new LinkedHashMap<>();
            // 统计
            for (int i = 0; i < width; i++) {
                int blackCount = 0;
                for (int j = 0; j < height; j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        blackCount++;
                    }
                }
    
                map.put(i, blackCount);
            }
    
            // 从左到右遍历时,记录白色像素点的宽度,超过这个值,就表示当前处于字符之间的空白处
            int whiteCount = 0;
            int startIndex = -1;
    
            // 单个字符的最小宽度,用于判断当前字符是否需要合并
            // 存在“出”字被分成两个部分,左边宽度大于bufferedImage.getHeight()*2/3,右边宽度为1像素,导致不能合并,需要增大minWidth
            //int minWidth = bufferedImage.getHeight()*2/3;
            int minWidth = bufferedImage.getHeight();
    
            // 单个字符的最大宽度
            // 存在“呀,”合并的情况,因为它们的宽度小于bufferedImage.getHeight()*5/4,需要缩小他的值
            //int maxWidth = bufferedImage.getHeight()*5/4;
            int maxWidth = height;
            int lastKey = -1;
            int lastValue = -1;
    
            // 下标从左到右顺序:lastKey lastValue startIndex i
            // 字符切割逻辑,从左往右依次扫描,碰到第一个黑色像素,标记为startIndex,继续扫描,碰到第一个白色像素,标记为i,
            // 这时认为i-startIndex为第一个字符的宽度,如果此宽度大于设定的参数,就认为他是一个完整的字符,添加进去,重置startIndex变量
            // 如果小于设定的参数,就认为他不是一个完整的字符,将startIndex赋值给lastKey,i赋值给lastValue,继续扫描,
            // 当扫描到第二个字符的起始黑色像素点时,标记为startIndex,继续扫描,碰到第一个白色像素,标记为i,
            // 如果第二个字符是完整的字符,即宽度超过设定值,那么第一个不完整的字符肯定是完整的字符,因为没有找到他的剩余部分
            // 如果第二个字符也不是完整的字符,那么计算第二个字符的宽度加上之前第一个不完整字符的宽度即i-lastKey,
            // 如果超过了设定的参数,就认为他们两个可以组成完整的字符,否则继续扫描剩余的部分
            // 总结起来就是,我从左往右扫描,当扫描到的黑色像素块超过我设定的阈值,那么我就认为这些黑色像素块是一个字符,以此类推
            // 找出起始像素点
            for (int i = 0; i < width; i++) {
                int value = map.get(i);
    
                // 字符起始下标
                if (startIndex == -1) {
                    // 对于高度小于设定阈值的坐标,直接舍弃
                    if (value >= threshold) {
                        startIndex = i;
                    }
                } else {
                    // x轴上像素个数小于3的都认为是空白
                    if (value < threshold) {
                        whiteCount++;
                    } else {
                        // 对于一个字符中间存在空白的情况,需要排除掉
                        whiteCount = 0;
                    }
    
                    if (whiteCount > interval) {
                        int charWidth = i-interval-startIndex;
                        // 防止一个字符被分割成两个部分
                        if (charWidth < minWidth) {
                            // 如果是类似“儿”这样的左右分开的字符
                            if (lastKey == -1) {
                                lastKey = startIndex;
                                lastValue = i-interval;
                            } else {
                                if (i-interval-lastKey > maxWidth) {
                                    charsMap.put(lastKey, lastValue);
                                    lastKey = startIndex;
                                    lastValue = i-interval;
                                } else {
                                    /*charsMap.put(lastKey, i-interval);
                                    lastKey = -1;
                                    lastValue = -1;*/
    
                                    // 存在一个字符被分割成多个情况,这时需要多次合并
                                    lastValue = i-interval;
                                }
                            }
                        } else {
                            if (lastKey != -1) {
                                charsMap.put(lastKey, lastValue);
                                lastKey = -1;
                                lastValue = -1;
                            }
                            charsMap.put(startIndex, i-interval);
                        }
    
                        startIndex = -1;
                        whiteCount = 0;
                    }
                }
            }
    
            // 处理行尾的字符
            if (lastKey != -1 && startIndex != -1) {
                int i = width;
    
                // 后一个字符的宽度
                int charWidth = i-interval-startIndex;
                // 防止一个字符被分割成两个部分
                if (charWidth < minWidth) {
                    // 如果是类似“儿”这样的左右分开的字符
                    if (i-interval-lastKey > maxWidth) {
                        charsMap.put(lastKey, lastValue);
                        charsMap.put(startIndex, i-interval);
                    } else {
                        // 存在一个字符被分割成多个情况,这时需要多次合并
                        charsMap.put(lastKey, i-interval);
                    }
                } else {
                    charsMap.put(lastKey, lastValue);
                    charsMap.put(startIndex, i-interval);
                }
                lastKey = -1;
                lastValue = -1;
                startIndex = -1;
            }
    
    
            if (lastKey != -1 && startIndex == -1) {
                // 过滤噪点,暂时不考虑与最后一个字符合并
                if (lastValue - lastKey > 1) {
                    charsMap.put(lastKey, lastValue);
                    lastKey = -1;
                    lastValue = -1;
                }
            }
    
            if (startIndex != -1 && lastKey == -1) {
                charsMap.put(startIndex, width);
                startIndex = -1;
            }
    
            // 根据上面已经标注好的字符下标,提取字符图片
            for (Integer key : charsMap.keySet()) {
                int value = charsMap.get(key);
    
                BufferedImage grayImage = bufferedImage.getSubimage(key,0,value-key,height);
    
    
                /*BufferedImage grayImage = new BufferedImage(value-key, bufferedImage.getHeight(), bufferedImage.getType());
                for (int i = 0; i < value-key; i++) {
                    for (int j = 0; j < bufferedImage.getHeight(); j++) {
                        int color = bufferedImage.getRGB(key+i, j);
                        grayImage.setRGB(i, j, color);
                    }
                }*/
    
    
                // 清除无效字符
                if (isInvalidChar(grayImage)) {
                    continue;
                }
    
                // 清除字符上下空白部分
                BufferedImage cutUDImage = cutUD(grayImage);
    
    
                int maxHeight = getMaxHeight(bufferedImage);
                if (cutUDImage.getWidth() > maxHeight*splitThreshold) {
                    // 如果两个字符相互重合不能使用一条直线分割,使用连通量进行分割
                    List<BufferedImage> list2 = splitChar5(cutUDImage);
                    charImages.addAll(list2);
                } else {
                    charImages.add(cutUDImage);
                }
    
            }
    
            return charImages;
        }
    
        // 用于处理连在一起的字符串
        private int[] array2;
    
        /**
         * 根据连通分量来分割字符
         * 只处理含有两个连通分量的图片
         * @param image
         * @return
         */
        private List<BufferedImage> splitChar5(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
            int size = width * height;
    
            // 最小连通分量的面积,小于阈值的过滤掉
            int threshold = 20;
    
            // 用于存储每个单元格所属的集合
            array2 = new int[size];
            // 初始化各个单元格所属的集合
            for (int j = 0; j < size; j++) {
                array2[j] = -1;
            }
    
            //long startTime = System.currentTimeMillis();
    
            // 先合并左右单元格中间的竖着的墙
            for (int col=0; col<height; col++) {
                for (int row=0; row<width-1; row++) {
                    int firstColor = image.getRGB(row, col);
    
                    int secondColor = image.getRGB(row+1, col);
    
                    int firstIndex = col*width+row;
                    if ((firstColor & 0xff) == 0 && firstColor == secondColor) {
                        // 将两个单元格连通
                        array2[firstIndex] = firstIndex+1;
                    }
                }
            }
    
            //System.out.println("startTime1:" + (System.currentTimeMillis()- startTime) + "ms");
    
    
            // 输出array
            /*for (int j=0; j
    
            // 再拆除上下单元格之间的横着的墙,只拆除上下两行最右边的两个单元格就行了,能保证两行可以合并到同一个联通分量里面就行
            for (int col=height-1; col>0; col--) {
                for (int row=width-1; row>-1; row--) {
                    int firstColor = image.getRGB(row, col);
                    int secondColor = image.getRGB(row, col-1);
    
                    // 上下两个单元格的右边如果都是黑色,就不合并
                    if ((firstColor & 0xff) == 0 && firstColor == secondColor) {
                        // 第一个像素在第二个像素的下面
                        int firstIndex = col*width+row;
                        int secondIndex = firstIndex - width;
    
                        union2(firstIndex, secondIndex);
                    }
                }
            }
    
    
            //System.out.println("startTime3:" + (System.currentTimeMillis()- startTime) + "ms");
    
            // 统计所有连通分量的大小
            Map<Integer, Integer> map = new HashMap<>();
    
            // 将联通分量里面的元素全部设置为统一编号,便于统计,用倒序比正序快得多
            for (int i=size-1; i>-1; i--) {
                int row = array2[i];
                if (row == -1) {
                    continue;
                }
    
                int result1 = find2(i);
    
                Integer key = map.get(result1);
                if (key == null) {
                    map.put(result1, 1);
                } else if (key < threshold) {
                    map.put(result1, key+1);
                }
    
                array2[i] = result1;
            }
    
            // 存储连通量的上下两个极值在数组中的位置,key为连通量编号,也是连通量的上极值,value为连通量的下极值
            Map<Integer, Integer> map2 = new LinkedHashMap<>();
            for (int minY=0; minY<size; minY++) {
                int maxY = array2[minY];
                if (maxY == -1) {
                    continue;
                }
    
                // 过滤掉面积小于1500像素的联通分量
                if (!map2.containsKey(maxY) && map.get(maxY) == threshold) {
                    map2.put(maxY, minY);
                }
            }
    
            //System.out.println("startTime6:" + (System.currentTimeMillis()- startTime) + "ms");
    
            List<BufferedImage> list = new ArrayList<>();
            if (map2.size() != 2) {
            	list.add(image);
                return list;
            }
    
            // 根据联通分量来抠图
            Map<Integer, BufferedImage> map5 = new LinkedHashMap<>();
            for (int row = 0; row < width; row++) {
                for (int col=0; col<height; col++) {
                    int index = col * width + row;
                    int maxY = array2[index];
                    if (maxY == -1 || map2.get(maxY) == null) {
                        continue;
                    }
    
    
                    int minY = map2.get(maxY);
    
                    if (map2.containsKey(maxY)) {
                        int newHeight = maxY/width - minY/width+1;
    
                        int blankHeight = minY/width;
    
                        if (map5.containsKey(maxY)) {
                            BufferedImage grayImage = map5.get(maxY);
                            int color = image.getRGB(row, col);
    
                            grayImage.setRGB(row, col-blankHeight, color);
                        } else {
                            // 找出能放下字符行的四边形的四个角
                            BufferedImage grayImage = new BufferedImage(width, newHeight, image.getType());
                            // 设置白色背景
                            for (int i=0; i<width; i++) {
                                for (int j=0; j<newHeight; j++) {
                                    grayImage.setRGB(i, j, 0xFFFFFF);
                                }
                            }
    
                            int color = image.getRGB(row, col);
    
                            grayImage.setRGB(row, col-blankHeight, color);
    
                            map5.put(maxY, grayImage);
                        }
    
    
                    }
    
                }
            }
    
            for (Integer key : map5.keySet()) {
                // 清除字符上下空白部分
                BufferedImage cutLR2 = cutLR2(map5.get(key));
    
                list.add(cutLR2);
            }
    
            return list;
        }
    
        private boolean isInvalidChar(BufferedImage bufferedImage) {
            int blackCount = 0;
            // 统计
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int b = color & 0xff;
                    if (b == 0) {
                        blackCount++;
                    }
                }
            }
    
            return blackCount < ignoreArea;
        }
    
        /**
         * 获取一行文字的最大有效高度
         * 用于处理倒S形的文字行
         * 从左到右扫描列,通过记录每一列的第一个和最后一个黑色像素,从而得到该列的有效高度,最后取所有列最大的有效高度,也即是字符的最大高度
         * @param bufferedImage
         * @return
         */
        private static int getMaxHeight(BufferedImage bufferedImage) {
            int maxHeight = 0;
            // 以图片左上角点为坐标原点
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
    
                int start = bufferedImage.getHeight()-1;
                int end = 0;
    
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    int color = bufferedImage.getRGB(i, j);
                    int red = color & 0xFF;
                    if (red == 0) {
                        start = j;
                        break;
                    }
                }
    
                for (int j = bufferedImage.getHeight()-1; j > -1; j--) {
                    int color = bufferedImage.getRGB(i, j);
                    int red = color & 0xFF;
                    if (red == 0) {
                        end = j;
                        break;
                    }
                }
    
                if ((end - start) > maxHeight) {
                    maxHeight = end - start;
                }
    
            }
    
            return maxHeight + 1;
        }
    
        /**
         * 切分图片
         *
         * @param bufferedImage
         * @return
         */
        private static Map<Integer, BufferedImage> splitImage(BufferedImage bufferedImage) {
            Map<Integer, BufferedImage> map = new HashMap<>();
    
            int w = bufferedImage.getWidth();
            int h = bufferedImage.getHeight();
            int type = bufferedImage.getType();
    
            int blockWidth = w / widthSplitNum;
            int blockHeight = h / heightSplitNum;
    
            for (int i = 0; i < widthSplitNum; i++) {
                for (int j = 0; j < heightSplitNum; j++) {
                    BufferedImage grayImage = new BufferedImage(blockWidth, blockHeight, type);
    
                    int mStart = i * blockWidth;
                    int mEnd = (i + 1) * blockWidth;
                    int nStart = j * blockHeight;
                    int nEnd = (j + 1) * blockHeight;
    
                    for (int m = mStart; m < mEnd; m++) {
                        for (int n = nStart; n < nEnd; n++) {
                            grayImage.setRGB(m - mStart, n - nStart, bufferedImage.getRGB(m, n));
                        }
                    }
    
                    map.put(i * heightSplitNum + j, grayImage);
                }
            }
    
            return map;
        }
    
        public BufferedImage mergeImage(Map<Integer, BufferedImage> map) {
            if (map == null || map.get(0) == null) {
                return null;
            }
    
            int blockWidth = map.get(0).getWidth();
            int blockHeight = map.get(0).getHeight();
    
            BufferedImage destImage = new BufferedImage(blockWidth * widthSplitNum, blockHeight * heightSplitNum, BufferedImage.TYPE_BYTE_BINARY);
    
    
            for (int i = 0; i < widthSplitNum; i++) {
                for (int j = 0; j < heightSplitNum; j++) {
                    BufferedImage grayImage = map.get(i * heightSplitNum + j);
    
                    for (int m = 0; m < grayImage.getWidth(); m++) {
                        for (int n = 0; n < grayImage.getHeight(); n++) {
                            destImage.setRGB(i * blockWidth + m, j * blockHeight + n, grayImage.getRGB(m, n));
                        }
                    }
                }
            }
    
            return destImage;
        }
    
        /**
         * 二值化图片
         *
         * @param bufferedImage 原图片
         * @return 二值化后的图片
         */
        private static BufferedImage binaryImage(BufferedImage bufferedImage) {
            BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
    
            int threshold = getMeanThreshold(bufferedImage);
    
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int r = (color >> 16) & 0xff;
                    int g = (color >> 8) & 0xff;
                    int b = color & 0xff;
                    int gray = (int) (0.2126 * r + 0.7152 * g + 0.0722 * b);
                    if (gray > threshold) {
                        // 白色
                        grayImage.setRGB(i, j, 0xFFFFFF);
                    } else {
                        // 黑色
                        grayImage.setRGB(i, j, 0);
                    }
                }
            }
    
            return grayImage;
        }
    
        /**
         * 获取图片的阀值,采用基于灰度平均值的阈值
         *
         * @param bufferedImage 原图片
         * @return 二值化的阈值
         */
        private static int getMeanThreshold2(BufferedImage bufferedImage) {
            BufferedImage grayImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType());
    
            Map<Integer, Integer> map = new HashMap<>();
    
    
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    // getRGB()方法,根据手册,其返回的int型数据(32位)为ARGB格式,其中ARGB各占8bit
                    int color = bufferedImage.getRGB(i, j);
                    int r = (color >> 16) & 0xff;
                    int g = (color >> 8) & 0xff;
                    int b = color & 0xff;
                    //int gray = (int) (0.2126 * r + 0.7152 * g + 0.0722 * b);
                    int gray = (int) (0.33 * r + 0.34 * g + 0.33 * b);
    
                    if (map.containsKey(gray)) {
                        map.put(gray, map.get(gray)+1);
                    } else {
                        map.put(gray, 1);
                    }
                }
            }
    
            // 起始像素值
            int index = 0;
            for (int i = 0; i < bufferedImage.getWidth(); i++) {
                for (int j = 0; j < bufferedImage.getHeight(); j++) {
                    int rgb = getRgb(map, index);
                    grayImage.setRGB(i, j, rgb);
                }
            }
    
    
            int sum = 0;
    
            int j = grayImage.getHeight()/2;
            for (int i = 0; i < grayImage.getWidth(); i++) {
                int color = grayImage.getRGB(i, j);
                int r = (color >> 16) & 0xff;
                sum += r;
                //System.out.println(r);
            }
    
            int threshold = sum / grayImage.getWidth();
            System.out.println(threshold);
            return threshold - 15;
        }
    
        /**
         * 递归
         * @param map
         * @param index
         * @return
         */
        private static int getRgb(Map<Integer, Integer> map, int index) {
            int rgb = 0;
            while (map.get(index) == null && index < 256) {
                index++;
            }
    
            Integer count = map.get(index);
    
            if (count != null) {
                if (count > 0) {
                    map.put(index, count-1);
                    rgb = (clamp(index) << 16) | (clamp(index) << 8) | clamp(index);
                } else {
                    rgb = getRgb(map, index+1);
                }
            }
    
            return rgb;
        }
    
        /**
         * 获取图片的阀值,采用基于灰度平均值的阈值
         *
         * @param bufferedImage 原图片
         * @return 二值化的阈值
         */
        private static int getMeanThreshold(BufferedImage bufferedImage) {
            double aa = 0.83;
            int w = bufferedImage.getWidth();
            int h = bufferedImage.getHeight();
            int num = 0;
            long sum = 0;
            for (int i = 0; i < w; i++) {
                for (int j = 0; j < h; j++) {
                    int color = bufferedImage.getRGB(i, j);
                    int r = (color >> 16) & 0xff;
                    int g = (color >> 8) & 0xff;
                    int b = color & 0xff;
                    int gray = (int) (0.2126 * r + 0.7152 * g + 0.0722 * b);
                    sum += gray;
                    num += 1;
                }
            }
    
            // 测试表明,阀值取平均值的1.2倍效果最好。
            // 越大越黑
            int threshold = (int) (sum / num);
            if (threshold * aa < 255) {
                threshold = (int) (aa * sum / num);
            }
    
            return threshold;
        }
    
        /**
         * 如果像素点的值超过了0-255的范围,予以调整
         *
         * @param value 输入值
         * @return 输出值
         */
        private static int clamp(int value) {
            return value > 255 ? 255 : (Math.max(value, 0));
        }
    
        // 用于存储每个单元格所属的集合
        private int[] array;
    
        // 映射到右边边界上,用于判断哪些行有黑色像素,对于没有黑色像素的行直接跳过
        private Map<Integer, Integer> map3;
    
        /**
         * 减少union的时间
         * @param image
         * @return
         * @throws IOException
         */
        private List<BufferedImage> getCharRow4(BufferedImage image, BufferedImage bufferedImage) {
            int width = image.getWidth();
            int height = image.getHeight();
            int size = width * height;
    
            // 最小连通分量的面积,小于阈值的过滤掉
            int threshold = 500;
    
            // 用于存储每个单元格所属的集合
            array = new int[size];
            // 初始化各个单元格所属的集合
            for (int j = 0; j < size; j++) {
                array[j] = -1;
            }
    
            //long startTime = System.currentTimeMillis();
    
            // 先合并左右单元格中间的竖着的墙
            for (int col=0; col<height; col++) {
                if (!map3.containsKey(col)) {
                    continue;
                }
    
                for (int row=0; row<width-1; row++) {
                    int firstColor = image.getRGB(row, col);
    
                    int secondColor = image.getRGB(row+1, col);
    
                    int firstIndex = col*width+row;
                    if ((firstColor & 0xff) == 0 && firstColor == secondColor) {
                        // 将两个单元格连通
                        array[firstIndex] = firstIndex+1;
                    }
                }
            }
    
            //System.out.println("startTime1:" + (System.currentTimeMillis()- startTime) + "ms");
    
    
            // 输出array
            /*for (int j=0; j
    
    
            boolean flag = false;
            // 再拆除上下单元格之间的横着的墙,只拆除上下两行最右边的两个单元格就行了,能保证两行可以合并到同一个联通分量里面就行
            for (int col=height-1; col>0; col--) {
                if (!map3.containsKey(col)) {
                    continue;
                }
                // 每次换行都要执行一次合并,防止因为两行的最右边是边界,没有白色像素
                flag = true;
                for (int row=width-1; row>-1; row--) {
                    int firstColor = image.getRGB(row, col);
                    int secondColor = image.getRGB(row, col-1);
    
                    // 如果两个黑色像素前面是白色像素,那么就表示他们是各自行的末尾,那么就进行合并操作
                    if (!flag && (firstColor == -1 || secondColor == -1)) {
                        flag = true;
                    }
    
                    // 上下两个单元格的右边如果都是黑色,就不合并
                    if (flag && ((firstColor & 0xff) == 0 && firstColor == secondColor)) {
                        // 第一个像素在第二个像素的下面
                        int firstIndex = col*width+row;
                        int secondIndex = firstIndex - width;
    
                        union(firstIndex, secondIndex);
                        flag = false;
                    }
                }
            }
    
    
            //System.out.println("startTime3:" + (System.currentTimeMillis()- startTime) + "ms");
    
            // 统计所有连通分量的大小
            Map<Integer, Integer> map = new HashMap<>();
    
            // 将联通分量里面的元素全部设置为统一编号,便于统计,用倒序比正序快得多
            for (int i=size-1; i>-1; i--) {
                int row = array[i];
                if (row == -1) {
                    continue;
                }
    
                int result1 = find(i);
    
                Integer key = map.get(result1);
                if (key == null) {
                    map.put(result1, 1);
                } else if (key < threshold) {
                    map.put(result1, key+1);
                }
    
                array[i] = result1;
            }
    
            // 存储连通量的上下两个极值在数组中的位置,key为连通量编号,也是连通量的上极值,value为连通量的下极值
            Map<Integer, Integer> map2 = new LinkedHashMap<>();
            for (int minY=0; minY<size; minY++) {
                int maxY = array[minY];
                if (maxY == -1) {
                    continue;
                }
    
                // 过滤掉面积小于1500像素的联通分量
                if (!map2.containsKey(maxY) && map.get(maxY) == threshold) {
                    map2.put(maxY, minY);
                }
            }
    
            //System.out.println("startTime6:" + (System.currentTimeMillis()- startTime) + "ms");
    
    
            // 根据联通分量来抠图
            List<BufferedImage> list = new ArrayList<>();
            Map<Integer, BufferedImage> map5 = new LinkedHashMap<>();
    
            for (int col=0; col<height; col++) {
                if (!map3.containsKey(col)) {
                    continue;
                }
    
                for (int row = 0; row < width; row++) {
                    int index = col * width + row;
                    int maxY = array[index];
                    if (maxY == -1 || map2.get(maxY) == null) {
                        continue;
                    }
    
    
                    int minY = map2.get(maxY);
    
                    if (map2.containsKey(maxY)) {
                        int newHeight = maxY/width - minY/width+1;
    
                        int blankHeight = minY/width;
    
                        if (map5.containsKey(maxY)) {
                            BufferedImage grayImage = map5.get(maxY);
                            int color = bufferedImage.getRGB(row, col);
    
                            grayImage.setRGB(row, col-blankHeight, color);
                        } else {
                            // 找出能放下字符行的四边形的四个角
                            BufferedImage grayImage = new BufferedImage(width, newHeight, image.getType());
                            // 设置白色背景
                            for (int i=0; i<width; i++) {
                                for (int j=0; j<newHeight; j++) {
                                    grayImage.setRGB(i, j, 0xFFFFFF);
                                }
                            }
    
                            int color = bufferedImage.getRGB(row, col);
    
                            grayImage.setRGB(row, col-blankHeight, color);
    
                            map5.put(maxY, grayImage);
                        }
    
    
                    }
    
                }
            }
    
            for (Integer key : map5.keySet()) {
                list.add(map5.get(key));
            }
    
            /*for (int maxY : map2.keySet()) {
                int minY = map2.get(maxY);
    
                int newHeight = maxY/width - minY/width+1;
    
                int blankHeight = minY/width;
    
                // 找出能放下字符行的四边形的四个角
                BufferedImage grayImage = new BufferedImage(width, newHeight, image.getType());
                // 设置白色背景
                for (int i=0; i
    
            //System.out.println("startTime7:" + (System.currentTimeMillis()- startTime) + "ms");
    
            int fileName = 0;
            for (Integer key: map5.keySet()) {
                File newFile = new File("d:\\temp\\" + (fileName++) + ".png");
                try {
                    ImageIO.write(map5.get(key), "png", newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            return list;
        }
    
        private void printArray(int width, int height) {
            for (int j=0; j<height; j++) {
                for (int i=0; i<width; i++) {
                    int index = j * width + i;
                    System.out.print(fillBlank(array[index]+",", 8));
                }
    
                System.out.println();
            }
        }
    
        public String fillBlank(String str, int length) {
            int strLength = str.length();
            if (strLength < length) {
                for (int i=0; i<length-strLength; i++) {
                    str += " ";
                }
            }
            return str;
        }
    
        /**
         * 返回 i 所在集合的最大值
         *
         * @param i 单元格编号
         * @return
         */
        public int find(int i) {
            int result = i;
            while (array[result] != -1) {
                result = array[result];
            }
            return result;
        }
    
        /**
         * 将 i 和 j 所在集合进行合并
         *
         * @param i 单元格编号
         * @param j 单元格编号
         */
        public void union(int i, int j) {
            int result1 = find(i);
            int result2 = find(j);
            if (result1 == result2){
                return;
            }
            if(result1 > result2) {
                array[result2] = result1;
            }
            else {
                array[result1] = result2;
            }
        }
    
    
        public int find2(int i) {
            int result = i;
            while (array2[result] != -1) {
                result = array2[result];
            }
            return result;
        }
    
        public void union2(int i, int j) {
            int result1 = find2(i);
            int result2 = find2(j);
            if (result1 == result2){
                return;
            }
            if(result1 > result2) {
                array2[result2] = result1;
            }
            else {
                array2[result1] = result2;
            }
        }
    
        private void setRecursion(int j, int value) {
    
            if (array[j] != 0) {
                if (j == array[j]) {
                    return;
                }
    
                setRecursion(array[j], value);
            }
    
            array[j] = value;
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
    • 734
    • 735
    • 736
    • 737
    • 738
    • 739
    • 740
    • 741
    • 742
    • 743
    • 744
    • 745
    • 746
    • 747
    • 748
    • 749
    • 750
    • 751
    • 752
    • 753
    • 754
    • 755
    • 756
    • 757
    • 758
    • 759
    • 760
    • 761
    • 762
    • 763
    • 764
    • 765
    • 766
    • 767
    • 768
    • 769
    • 770
    • 771
    • 772
    • 773
    • 774
    • 775
    • 776
    • 777
    • 778
    • 779
    • 780
    • 781
    • 782
    • 783
    • 784
    • 785
    • 786
    • 787
    • 788
    • 789
    • 790
    • 791
    • 792
    • 793
    • 794
    • 795
    • 796
    • 797
    • 798
    • 799
    • 800
    • 801
    • 802
    • 803
    • 804
    • 805
    • 806
    • 807
    • 808
    • 809
    • 810
    • 811
    • 812
    • 813
    • 814
    • 815
    • 816
    • 817
    • 818
    • 819
    • 820
    • 821
    • 822
    • 823
    • 824
    • 825
    • 826
    • 827
    • 828
    • 829
    • 830
    • 831
    • 832
    • 833
    • 834
    • 835
    • 836
    • 837
    • 838
    • 839
    • 840
    • 841
    • 842
    • 843
    • 844
    • 845
    • 846
    • 847
    • 848
    • 849
    • 850
    • 851
    • 852
    • 853
    • 854
    • 855
    • 856
    • 857
    • 858
    • 859
    • 860
    • 861
    • 862
    • 863
    • 864
    • 865
    • 866
    • 867
    • 868
    • 869
    • 870
    • 871
    • 872
    • 873
    • 874
    • 875
    • 876
    • 877
    • 878
    • 879
    • 880
    • 881
    • 882
    • 883
    • 884
    • 885
    • 886
    • 887
    • 888
    • 889
    • 890
    • 891
    • 892
    • 893
    • 894
    • 895
    • 896
    • 897
    • 898
    • 899
    • 900
    • 901
    • 902
    • 903
    • 904
    • 905
    • 906
    • 907
    • 908
    • 909
    • 910
    • 911
    • 912
    • 913
    • 914
    • 915
    • 916
    • 917
    • 918
    • 919
    • 920
    • 921
    • 922
    • 923
    • 924
    • 925
    • 926
    • 927
    • 928
    • 929
    • 930
    • 931
    • 932
    • 933
    • 934
    • 935
    • 936
    • 937
    • 938
    • 939
    • 940
    • 941
    • 942
    • 943
    • 944
    • 945
    • 946
    • 947
    • 948
    • 949
    • 950
    • 951
    • 952
    • 953
    • 954
    • 955
    • 956
    • 957
    • 958
    • 959
    • 960
    • 961
    • 962
    • 963
    • 964
    • 965
    • 966
    • 967
    • 968
    • 969
    • 970
    • 971
    • 972
    • 973
    • 974
    • 975
    • 976
    • 977
    • 978
    • 979
    • 980
    • 981
    • 982
    • 983
    • 984
    • 985
    • 986
    • 987
    • 988
    • 989
    • 990
    • 991
    • 992
    • 993
    • 994
    • 995
    • 996
    • 997
    • 998
    • 999
    • 1000
    • 1001
    • 1002
    • 1003
    • 1004
    • 1005
    • 1006
    • 1007
    • 1008
    • 1009
    • 1010
    • 1011
    • 1012
    • 1013
    • 1014
    • 1015
    • 1016
    • 1017
    • 1018
    • 1019
    • 1020
    • 1021
    • 1022
    • 1023
    • 1024
    • 1025
    • 1026
    • 1027
    • 1028
    • 1029
    • 1030
    • 1031
    • 1032
    • 1033
    • 1034
    • 1035
    • 1036
    • 1037
    • 1038
    • 1039
    • 1040
    • 1041
    • 1042
    • 1043
    • 1044
    • 1045
    • 1046
    • 1047
    • 1048
    • 1049
    • 1050
    • 1051
    • 1052
    • 1053
    • 1054
    • 1055
    • 1056
    • 1057
    • 1058
    • 1059
    • 1060
    • 1061
    • 1062
    • 1063
    • 1064
    • 1065
    • 1066
    • 1067
    • 1068
    • 1069
    • 1070
    • 1071
    • 1072
    • 1073
    • 1074
    • 1075
    • 1076
    • 1077
    • 1078
    • 1079
    • 1080
    • 1081
    • 1082
    • 1083
    • 1084
    • 1085
    • 1086
    • 1087
    • 1088
    • 1089
    • 1090
    • 1091
    • 1092
    • 1093
    • 1094
    • 1095
    • 1096
    • 1097
    • 1098
    • 1099
    • 1100
    • 1101
    • 1102
    • 1103
    • 1104
    • 1105
    • 1106
    • 1107
    • 1108
    • 1109
    • 1110
    • 1111
    • 1112
    • 1113
    • 1114
    • 1115
    • 1116
    • 1117
    • 1118
    • 1119
    • 1120
    • 1121
    • 1122
    • 1123
    • 1124
    • 1125
    • 1126
    • 1127
    • 1128
    • 1129
    • 1130
    • 1131
    • 1132
    • 1133
    • 1134
    • 1135
    • 1136
    • 1137
    • 1138
    • 1139
    • 1140
    • 1141
    • 1142
    • 1143
    • 1144
    • 1145
    • 1146
    • 1147
    • 1148
    • 1149
    • 1150
    • 1151
    • 1152
    • 1153
    • 1154
    • 1155
    • 1156
    • 1157
    • 1158
    • 1159
    • 1160
    • 1161
    • 1162
    • 1163
    • 1164
    • 1165
    • 1166
    • 1167
    • 1168
    • 1169
    • 1170
    • 1171
    • 1172
    • 1173
    • 1174
    • 1175
    • 1176
    • 1177
    • 1178
    • 1179
    • 1180
    • 1181
    • 1182
    • 1183
    • 1184
    • 1185
    • 1186
    • 1187
    • 1188
    • 1189
    • 1190
    • 1191
    • 1192
    • 1193
    • 1194
    • 1195
    • 1196
    • 1197
    • 1198
    • 1199
    • 1200
    • 1201
    • 1202
    • 1203
    • 1204
    • 1205
    • 1206
    • 1207
    • 1208
    • 1209
    • 1210
    • 1211
    • 1212
    • 1213
    • 1214
    • 1215
    • 1216
    • 1217
    • 1218
    • 1219
    • 1220
    • 1221
    • 1222
    • 1223
    • 1224
    • 1225
    • 1226
    • 1227
    • 1228
    • 1229
    • 1230
    • 1231
    • 1232
    • 1233
    • 1234
    • 1235
    • 1236
    • 1237
    • 1238
    • 1239
    • 1240
    • 1241
    • 1242
    • 1243
    • 1244
    • 1245
    • 1246
    • 1247
    • 1248
    • 1249
    • 1250
    • 1251
    • 1252
    • 1253
    • 1254
    • 1255
    • 1256
    • 1257
    • 1258
    • 1259
    • 1260
    • 1261
    • 1262
    • 1263
    • 1264
    • 1265
    • 1266
    • 1267
    • 1268
    • 1269
    • 1270
    • 1271
    • 1272
    • 1273
    • 1274
    • 1275
    • 1276
    • 1277
    • 1278
    • 1279
    • 1280
    • 1281
    • 1282
    • 1283
    • 1284
    • 1285
    • 1286
    • 1287
    • 1288
    • 1289
    • 1290
    • 1291
    • 1292
    • 1293
    • 1294
    • 1295
    • 1296
    • 1297
    • 1298
    • 1299
    • 1300
    • 1301
    • 1302
    • 1303
    • 1304
    • 1305
    • 1306
    • 1307
    • 1308
    • 1309
    • 1310
    • 1311
    • 1312
    • 1313
    • 1314
    • 1315
    • 1316
    • 1317
    • 1318
    • 1319
    • 1320
    • 1321
    • 1322
    • 1323
    • 1324
    • 1325
    • 1326
    • 1327
    • 1328
    • 1329
    • 1330
    • 1331
    • 1332
    • 1333
    • 1334
    • 1335
    • 1336
    • 1337
    • 1338
    • 1339
    • 1340
    • 1341
    • 1342
    • 1343
    • 1344
    • 1345
    • 1346
    • 1347
    • 1348
    • 1349
    • 1350
    • 1351
    • 1352
    • 1353
    • 1354
    • 1355
    • 1356
    • 1357
    • 1358
    • 1359
    • 1360
    • 1361
    • 1362
    • 1363
    • 1364
    • 1365
    • 1366
    • 1367
    • 1368
    • 1369
    • 1370
    • 1371
    • 1372
    • 1373
    • 1374
    • 1375
    • 1376
    • 1377
    • 1378
    • 1379
    • 1380
    • 1381
    • 1382
    • 1383
    • 1384
    • 1385
    • 1386
    • 1387
    • 1388
    • 1389
    • 1390
    • 1391
    • 1392
    • 1393
    • 1394
    • 1395
    • 1396
    • 1397
    • 1398
    • 1399
    • 1400
    • 1401
    • 1402
    • 1403
    • 1404
    • 1405
    • 1406
    • 1407
    • 1408
    • 1409
    • 1410
    • 1411
    • 1412
    • 1413
    • 1414
    • 1415
    • 1416
    • 1417
    • 1418
    • 1419
    • 1420
    • 1421
    • 1422
    • 1423
    • 1424
    • 1425
    • 1426
    • 1427
    • 1428
    • 1429
    • 1430
    • 1431
    • 1432
    • 1433
    • 1434
    • 1435
    • 1436
    • 1437
    • 1438
    • 1439
    • 1440
    • 1441
    • 1442
    • 1443
    • 1444
    • 1445
    • 1446
    • 1447
    • 1448
    • 1449
    • 1450
    • 1451
    • 1452
    • 1453
    • 1454
    • 1455
    • 1456
    • 1457
    • 1458
    • 1459
    • 1460
    • 1461
    • 1462
    • 1463
    • 1464
    • 1465
    • 1466
    • 1467
    • 1468
    • 1469
    • 1470
    • 1471
    • 1472
    • 1473
    • 1474
    • 1475
    • 1476
    • 1477
    • 1478
    • 1479
    • 1480
    • 1481
    • 1482
    • 1483
    • 1484
    • 1485
    • 1486
    • 1487
    • 1488
    • 1489
    • 1490
    • 1491
    • 1492
    • 1493
    • 1494
    • 1495
    • 1496
    • 1497
    • 1498
    • 1499
    • 1500
    • 1501
    • 1502
    • 1503
    • 1504
    • 1505
    • 1506
    • 1507
    • 1508
    • 1509
    • 1510
    • 1511
    • 1512
    • 1513
    • 1514
    • 1515
    • 1516
    • 1517
    • 1518
    • 1519
    • 1520
    • 1521
    • 1522
    • 1523
    • 1524
    • 1525
    • 1526
    • 1527
    • 1528
    • 1529
    • 1530
    • 1531
    • 1532
    • 1533
    • 1534
    • 1535
    • 1536
    • 1537
    • 1538
    • 1539
    • 1540
    • 1541
    • 1542
    • 1543
    • 1544
    • 1545
    • 1546
    • 1547
    • 1548
    • 1549
    • 1550
    • 1551
    • 1552
    • 1553
    • 1554
    • 1555
    • 1556
    • 1557
    • 1558
    • 1559
    • 1560
    • 1561
    • 1562
    • 1563
    • 1564
    • 1565
    • 1566
    • 1567
    • 1568
    • 1569
    • 1570
    • 1571
    • 1572
    • 1573
    • 1574
    • 1575
    • 1576
    • 1577
    • 1578
    • 1579
    • 1580
    • 1581
    • 1582
    • 1583
    • 1584
    • 1585
    • 1586
    • 1587
    • 1588
    • 1589
    • 1590
    • 1591
    • 1592
    • 1593
    • 1594
    • 1595
    • 1596
    • 1597
    • 1598
    • 1599
    • 1600
    • 1601
    • 1602
    • 1603
    • 1604
    • 1605
    • 1606
    • 1607
    • 1608
    • 1609
    • 1610
    • 1611
    • 1612
    • 1613
    • 1614
    • 1615
    • 1616
    • 1617
    • 1618
    • 1619
    • 1620
    • 1621
    • 1622
    • 1623
    • 1624
    • 1625
    • 1626
    • 1627
    • 1628
    • 1629
    • 1630
    • 1631
    • 1632
    • 1633
    • 1634
    • 1635
    • 1636
    • 1637
    • 1638
    • 1639
    • 1640
    • 1641
    • 1642
    • 1643
    • 1644
    • 1645
    • 1646
    • 1647
    • 1648
    • 1649
    • 1650
    • 1651
    • 1652
    • 1653
    • 1654
    • 1655
    • 1656
    • 1657
    • 1658
    • 1659
    • 1660
    • 1661
    • 1662
    • 1663
    • 1664
    • 1665
    • 1666
    • 1667
    • 1668
    • 1669
    • 1670
    • 1671
    • 1672
    • 1673
    • 1674
    • 1675
    • 1676
    • 1677
    • 1678
    • 1679
    • 1680
    • 1681
    • 1682
    • 1683
    • 1684
    • 1685
    • 1686
    • 1687
    • 1688
    • 1689
    • 1690
    • 1691
    • 1692
    • 1693
    • 1694
    • 1695
    • 1696
    • 1697
    • 1698
    • 1699
    • 1700
    • 1701
    • 1702
    • 1703
    • 1704
    • 1705
    • 1706
    • 1707
    • 1708
    • 1709
    • 1710
    • 1711
    • 1712
    • 1713
    • 1714
    • 1715
    • 1716
    • 1717
    • 1718
    • 1719
    • 1720
    • 1721
    • 1722
    • 1723
    • 1724
    • 1725
    • 1726
    • 1727
    • 1728
    • 1729
    • 1730
    • 1731
    • 1732
    • 1733
    • 1734
    • 1735
    • 1736
    • 1737
    • 1738
    • 1739
    • 1740
    • 1741
    • 1742
    • 1743
    • 1744
    • 1745
    • 1746
    • 1747
    • 1748
    • 1749
    • 1750
    • 1751
    • 1752
    • 1753
    • 1754

    效果截图

    原始图片

    在这里插入图片描述

    二值化图片

    在这里插入图片描述

    描黑图片

    在这里插入图片描述

    字符行切割图片

    在这里插入图片描述

    字符切割图片

    在这里插入图片描述

  • 相关阅读:
    【数据结构与算法分析】0基础带你学数据结构与算法分析09--线索二叉树 (TBT)
    重生之我是一名程序员 33
    【云原生之Docker实战】使用Docker部署家庭个人在线音乐平台
    Android 9.0 屏蔽设备的定位功能
    Win7电脑无法进入睡眠模式?
    Github每日精选(第12期):去中心化的社交平台mastodon
    【LLM】大模型幻觉问题的原因和缓解方法
    C++类和对象经典oj
    [elastic 8.x]java客户端连接elasticsearch与操作索引与文档
    background-repeat
  • 原文地址:https://blog.csdn.net/sinat_34067387/article/details/127407293