Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 游戏结果屏幕上的CR无法获取文本信息-如何预处理图像以获得成功?_Java_Ocr_Tesseract_Tess4j - Fatal编程技术网

Java 游戏结果屏幕上的CR无法获取文本信息-如何预处理图像以获得成功?

Java 游戏结果屏幕上的CR无法获取文本信息-如何预处理图像以获得成功?,java,ocr,tesseract,tess4j,Java,Ocr,Tesseract,Tess4j,当我第一次遇到问题时,我读了这篇文章:。我尝试对我的图像进行各种转换,并设法获得了一些显著的改进。然而,这离合理还有很长的路要走 以下是游戏结果屏幕的外观: 以下是Tess4J在尝试OCR未处理图像时给我的信息: 事后报告 阿克斯阿马拉 Umermax Su'rends' 5公里:Irun 非常规小胜利 美国陆军联合司令部 奥斯普塔勒。6mm x失败 sscunsn V目标x失败 失败的x参数。可接受 6650 mes 9104 55毫米0公里0毫米0公里 21毫米77毫米 39毫米警告53毫米

当我第一次遇到问题时,我读了这篇文章:。我尝试对我的图像进行各种转换,并设法获得了一些显著的改进。然而,这离合理还有很长的路要走

以下是游戏结果屏幕的外观: 以下是Tess4J在尝试OCR未处理图像时给我的信息:

事后报告
阿克斯阿马拉
Umermax Su'rends'
5公里:Irun
非常规小胜利
美国陆军联合司令部 奥斯普塔勒。6mm x失败
sscunsn V目标x失败
失败的x参数。可接受
6650 mes 9104
55毫米0公里0毫米0公里
21毫米77毫米
39毫米警告53毫米警告
u毫米mWSMG 2毫米mWSMG
u谈欲望0谈欲望
3武装的Vmwc的欲望0武装的Vauc的欲望
千万:万岁的欲望千万:万岁的欲望
0安曼欲望0安曼欲望
弗莱韦地图Em

这很糟糕。以下是我所期待的:

事后报告
阿马拉酒店 非常规投降
技能:铁
非常规小胜利
美国陆军非常规
可接受接地故障
安全目标失败
失败的参数可接受
6650分9104
56人正常0人正常
21人死亡77人死亡
39人受伤53人受伤
0人失踪2人失踪
0箱损失0箱损失
3辆装甲车损失0辆装甲车损失
0辆其他车辆丢失0辆车辆丢失
0架飞机损失0架飞机损失
将鼠标指向评估以了解详细信息
查看地图结束

我做了一些图像处理和实验,你可以在下面的代码中看到一些。我发现最好的结果是转换为灰度、反转、增加对比度和锐化:

这让我:

事后报告
阿克斯阿马拉
Unmrwenmnna的投降
5公里:[跑步
非常规小胜利
美国,陆军,Unmnvenmona'
可接受。接地3英镑失败
安全M Targzcs 23失败
53个参数失败。可接受
5550家跨国公司9104
55名英国男性0名正常男性
21男mm 77男mm
39人受伤,52人受伤 0名男性MWSMG 2名男性MWSMG
u型储罐ms:0储罐ms:
3辆武装车辆ms:0辆装甲车辆ms:
0个Omar Vamcxes ms:0个Omar Vamcxes ms:
0安曼ms:0安曼ms:
mm meuxe u Hummus m deullx
“Flewew地图E”结束E

我还尝试将引擎模式切换到
TessOcrEngineMode.OEM\u TESSERACT\u CUBE\u COMBINED
,但效果并不明显:

事后报告
阿马拉酒店 Uttaottgetciotal投降
技能:[跑步
非常规小胜利
美国陆军Uttouptgeptciomtl
可接受。接地3英镑失败
安全M Targzcs 23失败
53个参数失败。可接受
5550家跨国公司9104
55名英国男性0名正常男性
21男mm 77男mm
39人受伤,53人受伤 0人失踪2人失踪
u型储罐ms:0储罐ms:
3辆装甲车ms:0辆装甲车ms:
0奥马尔汽车公司ms:0奥马尔汽车公司ms:
0安曼ms:0安曼ms:
mm meuxe u Hummus m细节
l查看地图E l结束E

我应该对Tess4J的配置进行哪些转换或更改,以成功运行游戏结束屏幕的OCR

这是我的代码(注意,这只是一个测试类,但我实现了一系列基本的图像处理方法来执行黑白转换、负片、放大、腐蚀、缩放、锐化等:

package com.lesliesoftware.tesstest;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.lesliesoftware.lcommon.util.StringUtil;
import com.lesliesoftware.lcommon.util.file.FileHelper;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.ITessAPI.TessOcrEngineMode;

public class CMEndScreenProcessImage {

    public static void main (String[] args) {
        File endScreenImageFile = new File ("P:\\TessTest\\test\\CombatMissionEndScreen2019.07.03.png");

        Tesseract tesseract = new Tesseract ();
        try {
            //  Process the image
            //  Read the raw image
            BufferedImage imageData = ImageIO.read (endScreenImageFile);
            
            boolean preProcessImage = true;
            if (preProcessImage)  {
                File intermediateFile = endScreenImageFile;
                
                imageData = convertImageToGreyScale (imageData);
                intermediateFile = writeNewImageFile (intermediateFile, "_GreyScale", imageData);
                
                imageData = convertImageToNegative (imageData);
                intermediateFile = writeNewImageFile (intermediateFile, "_Negative", imageData);
                
                imageData = increaseContrast (imageData, 1.2f, 0.8f);
                intermediateFile = writeNewImageFile (intermediateFile, "_Contrast", imageData);
            
//            imageData = zoomImageBy (imageData, 2.0f, 2.0f);
//            intermediateFile = writeNewImageFile (intermediateFile, "_Zoom", imageData);
            
//            imageData = dilateGrayscaleImage (imageData);
//            intermediateFile = writeNewImageFile (intermediateFile, "_Dilate", imageData);
            
                imageData = sharpenImage (imageData);
                intermediateFile = writeNewImageFile (intermediateFile, "_Sharpen", imageData);
            
//            imageData = erodeGrayscaleImage (imageData);
//            intermediateFile = writeNewImageFile (intermediateFile, "_Erode", imageData);
//            
//            imageData = erodeGrayscaleImage (imageData);
//            intermediateFile = writeNewImageFile (intermediateFile, "_Erode", imageData);
            
                endScreenImageFile = writeNewImageFile (endScreenImageFile, "_Processed", imageData);
                System.out.println ("Final image can be found at: " + endScreenImageFile.getAbsolutePath ());
            }

            //  Setup the tesseract object
            tesseract.setDatapath ("P:/Tess4J/tessdata");
            boolean useCubeCombined = false;
            if (useCubeCombined)  {
                tesseract.setOcrEngineMode (TessOcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);
                tesseract.setLanguage ("eng");
            }

            //  Run the character recognition
            String text = tesseract.doOCR (imageData);

            //  Output the results
            System.out.print (text);
        } catch (IOException exception) {
            System.out.println ("File IO Error!:");
            exception.printStackTrace();
        } catch (TesseractException exception) {
            System.out.println ("OCR Error!:");
            exception.printStackTrace();
        }
    }
    
    
    private static BufferedImage sharpenImage (BufferedImage imageData) {
        // A 3x3 kernel that sharpens an image
        Kernel kernel = new Kernel (3, 3, new float[] {
                -1, -1, -1, 
                -1, 9, -1, 
                -1, -1, -1
        });
 
        BufferedImageOp op = new ConvolveOp(kernel);
 
        imageData = op.filter(imageData, null);
        
        return imageData;
    }


    /**
     * This method will perform erosion operation on the grayscale image img.
     * 
     * Code taken from https://github.com/yusufshakeel/Java-Image-Processing-Project/blob/master/DYimageFX-project/src/dyimagefx/morph/Erosion.java
     * Except the erode code is really a dilation and the dilation is really an erode so method contents have been swapped here
     * 
     * @param img The image on which erosion operation is performed
     */
    public static BufferedImage erodeGrayscaleImage(BufferedImage imageData){
        /**
         * Dimension of the image img.
         */
        int width = imageData.getWidth();
        int height = imageData.getHeight();
        
        //buff
        int buff[];
        
        //output of dilation
        int output[] = new int[width*height];
        
        //perform dilation
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                buff = new int[9];
                int i = 0;
                for(int ty = y - 1; ty <= y + 1; ty++){
                   for(int tx = x - 1; tx <= x + 1; tx++){
                       if(ty >= 0 && ty < height && tx >= 0 && tx < width){
                           //pixel under the mask
                           int rgb = imageData.getRGB(tx, ty);
                           buff[i] = (rgb >> 16) & 0x000000FF;;
//                           buff[i] = imageData.getRed(tx, ty);
                           i++;
                       }
                   }
                }
                
                //sort buff
                java.util.Arrays.sort(buff);
                
                //save highest value
                output[x+y*width] = buff[8];
            }
        }
        
        /**
         * Save the dilation value in image img.
         */
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                int v = output[x+y*width];
                Color colour = new Color (v, v, v);
                imageData.setRGB (x, y, colour.getRGB ());
//                imageData.setPixel(x, y, 255, v, v, v);
            }
        }
        
        return imageData;
    }

    
    /**
     * This method will perform dilation operation on the grayscale image img.
     * 
     * Code taken from https://github.com/yusufshakeel/Java-Image-Processing-Project/blob/master/DYimageFX-project/src/dyimagefx/morph/Dilation.java
     * Except the erode code is really a dilation and the dilation is really an erode so method contents have been swapped here
     * 
     * @param img The image on which dilation operation is performed
     */
    public static BufferedImage dilateGrayscaleImage(BufferedImage imageData){
        /**
         * Dimension of the image img.
         */
        int width = imageData.getWidth();
        int height = imageData.getHeight();
        
        //buff
        int buff[];
        
        //output of erosion
        int output[] = new int[width*height];
        
        //perform erosion
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                buff = new int[9];
                int i = 0;
                for(int ty = y - 1; ty <= y + 1; ty++){
                   for(int tx = x - 1; tx <= x + 1; tx++){
                       /**
                        * 3x3 mask [kernel or structuring element]
                        * [1, 1, 1
                        *  1, 1, 1
                        *  1, 1, 1]
                        */
                       if(ty >= 0 && ty < height && tx >= 0 && tx < width){
                           //pixel under the mask
                           int rgb = imageData.getRGB(tx, ty);
                           buff[i] = (rgb >> 16) & 0x000000FF;;
//                           buff[i] = imageData.getRed(tx, ty);
                           i++;
                       }
                   }
                }
                
                //sort buff
                java.util.Arrays.sort(buff);
                
                //save lowest value
                output[x+y*width] = buff[9-i];
            }
        }
        
        /**
         * Save the erosion value in image img.
         */
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                int v = output[x+y*width];
                Color colour = new Color (v, v, v);
                imageData.setRGB (x, y, colour.getRGB ());
//                imageData.setPixel(x, y, 255, v, v, v);
            }
        }
        
        return imageData;
    }

    
    private static BufferedImage increaseContrast (BufferedImage imageData, float brighten, float offset) throws IOException {
        //  Create the RescaleOP object
        RescaleOp rescale = new RescaleOp (brighten, offset, null);

        //  Perform the scaling operation - increase brightness
        BufferedImage contrastImageData = rescale.filter (imageData, null);
        
        return contrastImageData;
    }


    private static BufferedImage zoomImageBy (BufferedImage imageData, float xScale, float yScale) throws IOException {
        //  Make an empty image buffer to store image later
        int zoomWidth = (int)(imageData.getWidth () * xScale);
        int zoomHight = (int)(imageData.getHeight () * yScale);
        BufferedImage zoomImageData = new BufferedImage (zoomWidth, zoomHight, imageData.getType ());

        //  Creating a 2D platform on the buffer image for drawing the new image
        Graphics2D graphic = zoomImageData.createGraphics ();

        //  Draw new image starting from 0 0 to the zoomed image size
        graphic.drawImage (imageData, 0, 0, zoomWidth, zoomHight, null);
        graphic.dispose ();
        
        return zoomImageData;
    }


    public static File writeNewImageFile (File inputImageFile, String additionToName, BufferedImage zoomImageData) throws IOException {
        //  Append additionToName to the original file name
        String rootFileNameStr = FileHelper.getFileName (inputImageFile);
        String rootFileNameExt = FileHelper.getFileExtension (inputImageFile);
        File outputImageFile = new File (inputImageFile.getParentFile (), rootFileNameStr + additionToName + StringUtil.DOT + rootFileNameExt);
        
        //  Write the modified image
        ImageIO.write(zoomImageData, rootFileNameExt, outputImageFile);
        return outputImageFile;
    }


    private static BufferedImage convertImageToNegative (BufferedImage imageData) throws IOException {
        //  Loop for each pixel to convert to negative using the algorithm from https://www.geeksforgeeks.org/image-processing-java-set-4-colored-image-negative-image-conversion/?ref=lbp 
        int width = imageData.getWidth(); 
        int height = imageData.getHeight(); 
        for (int y = 0; y < height; y++) 
        { 
            for (int x = 0; x < width; x++) 
            { 
                int p = imageData.getRGB(x,y); 
                int a = (p>>24)&0xff; 
                int r = (p>>16)&0xff; 
                int g = (p>>8)&0xff; 
                int b = p&0xff; 
  
                //subtract RGB from 255 
                r = 255 - r; 
                g = 255 - g; 
                b = 255 - b; 
  
                //set new RGB value 
                p = (a<<24) | (r<<16) | (g<<8) | b; 
                imageData.setRGB(x, y, p); 
            } 
        } 
        
        return imageData;
    }
    

    private static BufferedImage convertImageToGreyScale (BufferedImage imageData) throws IOException {
        //  Loop for each pixel to convert to greyscale - using algorithm from https://www.geeksforgeeks.org/image-processing-in-java-set-3-colored-image-to-greyscale-image-conversion/?ref=lbp  
        int width = imageData.getWidth(); 
        int height = imageData.getHeight(); 
        for (int y = 0; y < height; y++)  { 
            for (int x = 0; x < width; x++)   { 
                //  Here (x,y) denotes the coordinate of image for modifying the pixel value. 
                int p = imageData.getRGB(x,y); 
  
                int a = (p>>24)&0xff; 
                int r = (p>>16)&0xff; 
                int g = (p>>8)&0xff; 
                int b = p&0xff; 
  
                //  calculate average 
                int avg = (r+g+b)/3; 
  
                //  replace RGB value with avg 
                p = (a<<24) | (avg<<16) | (avg<<8) | avg; 
  
                imageData.setRGB(x, y, p); 
            } 
        } 
        
        return imageData;
    }

}


package com.lesliesoftware.test;
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.awt.image.BufferedImageOp;
导入java.awt.image.ConvolveOp;
导入java.awt.image.Kernel;
导入java.awt.image.RescaleOp;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入com.lesliesoftware.lcommon.util.StringUtil;
导入com.lesliesoftware.lcommon.util.file.FileHelper;
导入net.sourceforge.tess4j.Tesseract;
导入net.sourceforge.tess4j.TesseractException;
导入net.sourceforge.tess4j.ITessAPI.TessOcrEngineMode;
公共类CMEndScreenProcessImage{
公共静态void main(字符串[]args){
File endScreenImageFile=新文件(“P:\\TessTest\\test\\CombatMissionEndScreen2019.07.03.png”);
Tesseract Tesseract=新的Tesseract();
试一试{
//处理图像
//读取原始图像
BuffereImage imageData=ImageIO.read(endScreenImageFile);
布尔预处理图像=真;
如果(预处理图像){
File intermediateFile=endScreenImageFile;
imageData=转换ImageTogreyScale(imageData);
中间文件=writeNewImageFile(中间文件,“\u灰度”,图像数据);
imageData=转换ImageToNegative(imageData);
中间文件=writeNewImageFile(中间文件,负数,图像数据);
imageData=增加对比度(imageData,1.2f,0.8f);
中间文件=writeNewImageFile(中间文件,“_对比度”,图像数据);
//imageData=zoomImageBy(imageData,2.0f,2.0f);
//中间文件=writeNewImageFile(中间文件“_Zoom”,图像数据);
//imageData=灰度图像(imageData);
//中间文件=writeNewImageFile(中间文件“_deplate”,图像数据);
imageData=锐化图像(imageData);
中间文件=writeNewImageFile(中间文件,“\u锐化”,图像数据);
//imageData=灰度图像(imageData);
//中间文件=writeNewImageFile(中间文件,“\u腐蚀”,图像数据);
//            
//imageData=灰度图像(imageData);
//