tesseract.js识别效率提升:JavaScript图像预处理优化
{"title":"tesseract.js识别效率很低\n现在按照我的思路用js实现以提高准确率\n1.去除干扰信息,干扰信息为黑色相近46/256之内全部清理\n2.去除背景颜色,相近的±30之内的全部设置为白色,灰色的干扰信息改为白色,文字改为黑色\n目前有图片的buffer.from的数据\n可以参考下面java的写法\njava\npublic static String dealImage(String imagePath) throws IOException {\n\nString formatName = imagePath.substring(imagePath.indexOf(\".\") + 1);\n\nFile file = new File(imagePath);\n\nBufferedImage image = ImageIO.read(file);\n\nint width = image.getWidth();\n\nint height = image.getHeight();\n\nBufferedImage outImage = new BufferedImage(width, height, image.getType());\n\nint backgroudColor = image.getRGB(0, 0);\n\nint backgroudR = (backgroudColor >> 16) & 0xff;\n\nint backgroudG = (backgroudColor >> 8) & 0xff;\n\nint backgroudB = backgroudColor & 0xff;\n\nfor (int i = 0; i < width; i++) {\n\nfor (int j = 0; j < height; j++) {\n\nint color = image.getRGB(i, j);\n\nint r = (color >> 16) & 0xff;\n\nint g = (color >> 8) & 0xff;\n\nint b = color & 0xff;\n\nint newColor = color;\n\n// 去除干扰信息,干扰信息为黑色相近46/256之内全部清理\n\nif(r < 64 && g < 64 && b < 64) {\n\nif(j-1 >= 0)\n\nnewColor = image.getRGB(i, j-1);\n\nelse if(i-1 >= 0)\n\nnewColor = image.getRGB(i-1, j);\n\nelse if(j+1 < height)\n\nnewColor = image.getRGB(i, j+1);\n\nelse if(i+1 < width)\n\nnewColor = image.getRGB(i+1, j);\n\nr = (newColor >> 16) & 0xff;\n\ng = (newColor >> 8) & 0xff;\n\nb = newColor & 0xff;\n\n}\n\n// 去除背景颜色,相近的±30之内的全部设置为白色,灰色的干扰信息改为白色,文字改为黑色\n\nif(Math.abs((r – backgroudR)) <= 30 && Math.abs((g – backgroudG)) <= 30 && Math.abs((b – backgroudB)) <= 30) {\n\nnewColor = 0xffffff;\n\n} else if(r > 150 && g > 150 && b > 150){\n\nnewColor = 0xffffff;\n\n} else {\n\nnewColor = 0x000000;\n\n}\n\noutImage.setRGB(i, j, newColor);\n\n}\n\n}\n\nByteArrayOutputStream out = new ByteArrayOutputStream();\n\nImageIO.write(outImage, formatName, out);\n\nString outPath = new File(imagePath).getParent() + File.separator + getFileMd5(out.toByteArray()) + \".\" + formatName;\n\nFile newFile = new File(outPath);\n\nImageIO.write(outImage, formatName, newFile);\n\nlog.debug(\"处理后的验证码文件:\" + outPath);\n\nreturn outPath;\n\n}\n\n/**\n\n* 根据文件字节流获取文件MD5\n\n* @param fileBytes\n\n* @return\n\n*/\n\npublic static String getFileMd5(byte[] fileBytes) {\n\ntry {\n\nMessageDigest md = MessageDigest.getInstance(\"MD5\");\n\nbyte[] mdBytes = md.digest(fileBytes);\n\nBigInteger bigInt = new BigInteger(1, mdBytes);\n\nreturn bigInt.toString(16);\n\n} catch (Exception e) {\n\nlog.error(\"删除文件失败\",e);\n\nreturn null;\n\n}\n\n}\n\n}\n内容:以下是将上述Java代码转换为JavaScript的实现:\n\njavascript\nconst Jimp = require('jimp');\n\nasync function dealImage(imagePath) {\n const image = await Jimp.read(imagePath);\n const width = image.bitmap.width;\n const height = image.bitmap.height;\n const outImage = new Jimp(width, height);\n\n const backgroudColor = image.getPixelColor(0, 0);\n const backgroudR = (backgroudColor >> 16) & 0xff;\n const backgroudG = (backgroudColor >> 8) & 0xff;\n const backgroudB = backgroudColor & 0xff;\n\n for (let i = 0; i < width; i++) {\n for (let j = 0; j < height; j++) {\n const color = image.getPixelColor(i, j);\n let r = (color >> 16) & 0xff;\n let g = (color >> 8) & 0xff;\n let b = color & 0xff;\n let newColor = color;\n\n // 去除干扰信息,干扰信息为黑色相近46/256之内全部清理\n if (r < 64 && g < 64 && b < 64) {\n if (j - 1 >= 0)\n newColor = image.getPixelColor(i, j - 1);\n else if (i - 1 >= 0)\n newColor = image.getPixelColor(i - 1, j);\n else if (j + 1 < height)\n newColor = image.getPixelColor(i, j + 1);\n else if (i + 1 < width)\n newColor = image.getPixelColor(i + 1, j);\n\n r = (newColor >> 16) & 0xff;\n g = (newColor >> 8) & 0xff;\n b = newColor & 0xff;\n }\n\n // 去除背景颜色,相近的±30之内的全部设置为白色,灰色的干扰信息改为白色,文字改为黑色\n if (\n Math.abs(r - backgroudR) <= 30 &&\n Math.abs(g - backgroudG) <= 30 &&\n Math.abs(b - backgroudB) <= 30\n ) {\n newColor = Jimp.rgbToInt(255, 255, 255);\n } else if (r > 150 && g > 150 && b > 150) {\n newColor = Jimp.rgbToInt(255, 255, 255);\n } else {\n newColor = Jimp.rgbToInt(0, 0, 0);\n }\n\n outImage.setPixelColor(newColor, i, j);\n }\n }\n\n const outPath = imagePath.substring(0, imagePath.lastIndexOf('.')) + '_processed' + imagePath.substring(imagePath.lastIndexOf('.'));\n await outImage.writeAsync(outPath);\n\n console.log('处理后的验证码文件:', outPath);\n return outPath;\n}\n\ndealImage('path/to/image.jpg');\n\n\n你可以使用Jimp库来进行图像处理操作。首先使用Jimp.read读取图像,然后使用getPixelColor和setPixelColor获取和设置像素颜色。最后使用writeAsync将处理后的图像保存到指定路径。\n\n请注意,这只是一个示例,具体实现可能需要根据你的需求进行调整。
原文地址: https://www.cveoy.top/t/topic/qe5i 著作权归作者所有。请勿转载和采集!