diff --git a/image_sdk/README.md b/image_sdk/README.md new file mode 100644 index 00000000..9a7a3d42 --- /dev/null +++ b/image_sdk/README.md @@ -0,0 +1,46 @@ +# 图像预处理SDK +在OCR文字识别的时候,我们得到的图像一般情况下都不是正的,多少都会有一定的倾斜。 +所以需要将图片转正。并且图片有可能是透视视角拍摄,需要重新矫正。 + +### SDK功能 +-图像转正 +-图像二值化,灰度化,去燥等经典算法。 + +### 完善中的功能: +-完善透视矫正 +-完善文字方向检测算法,判断转正后的图片角度,以便进一步旋转图片使得文字水平。 + +## 运行例子 +1. 首先下载例子代码 +```bash +git clone https://github.com/mymagicpower/AIAS.git +``` + +2. 导入examples项目到IDE中: +``` +cd image_sdk +``` + +3. 运行成功后,命令行应该看到下面的信息: +```text +319.0 , 865.0 +319.0 , 113.0 +785.0 , 113.0 +785.0 , 865.0 +startLeft = 319 +startUp = 113 +width = 467 +height = 753 +``` +输出图片效果如下: +![ocr_result](https://djl-model.oss-cn-hongkong.aliyuncs.com/AIAS/image_sdk/images/rotation.png) + + +### 帮助 +[点击下载SDK](https://djl-model.oss-cn-hongkong.aliyuncs.com/jars/aais-image-lib-0.1.0.jar). + +添加依赖lib - aais-image-lib-0.1.0.jar: +![lib](https://djl-model.oss-cn-hongkong.aliyuncs.com/AIAS/image_sdk/images/lib.png) + +## QQ群: +![Screenshot](https://djl-model.oss-cn-hongkong.aliyuncs.com/AIAS/OCR/OCR_QQ.png) diff --git a/image_sdk/build/output/binary/binaryNative.png b/image_sdk/build/output/binary/binaryNative.png new file mode 100644 index 00000000..c4abfaad Binary files /dev/null and b/image_sdk/build/output/binary/binaryNative.png differ diff --git a/image_sdk/build/output/binary/binaryzation.png b/image_sdk/build/output/binary/binaryzation.png new file mode 100644 index 00000000..9bbd3ac8 Binary files /dev/null and b/image_sdk/build/output/binary/binaryzation.png differ diff --git a/image_sdk/build/output/canny.jpg b/image_sdk/build/output/canny.jpg new file mode 100644 index 00000000..b440e4b7 Binary files /dev/null and b/image_sdk/build/output/canny.jpg differ diff --git a/image_sdk/build/output/contours.jpg b/image_sdk/build/output/contours.jpg new file mode 100644 index 00000000..72351dbb Binary files /dev/null and b/image_sdk/build/output/contours.jpg differ diff --git a/image_sdk/build/output/cutRect.jpg b/image_sdk/build/output/cutRect.jpg new file mode 100644 index 00000000..321c4a1a Binary files /dev/null and b/image_sdk/build/output/cutRect.jpg differ diff --git a/image_sdk/build/output/gray/grayColByAdapThreshold.png b/image_sdk/build/output/gray/grayColByAdapThreshold.png new file mode 100644 index 00000000..031be0ad Binary files /dev/null and b/image_sdk/build/output/gray/grayColByAdapThreshold.png differ diff --git a/image_sdk/build/output/gray/grayColByPartAdapThreshold.png b/image_sdk/build/output/gray/grayColByPartAdapThreshold.png new file mode 100644 index 00000000..00391be5 Binary files /dev/null and b/image_sdk/build/output/gray/grayColByPartAdapThreshold.png differ diff --git a/image_sdk/build/output/gray/grayNative.png b/image_sdk/build/output/gray/grayNative.png new file mode 100644 index 00000000..1233872b Binary files /dev/null and b/image_sdk/build/output/gray/grayNative.png differ diff --git a/image_sdk/build/output/gray/grayRowByKLargest.png b/image_sdk/build/output/gray/grayRowByKLargest.png new file mode 100644 index 00000000..2714124b Binary files /dev/null and b/image_sdk/build/output/gray/grayRowByKLargest.png differ diff --git a/image_sdk/build/output/gray/grayRowByMidle.png b/image_sdk/build/output/gray/grayRowByMidle.png new file mode 100644 index 00000000..54cb1cf4 Binary files /dev/null and b/image_sdk/build/output/gray/grayRowByMidle.png differ diff --git a/image_sdk/build/output/maxRect.jpg b/image_sdk/build/output/maxRect.jpg new file mode 100644 index 00000000..049c80b7 Binary files /dev/null and b/image_sdk/build/output/maxRect.jpg differ diff --git a/image_sdk/build/output/nativeRotatedImg.jpg b/image_sdk/build/output/nativeRotatedImg.jpg new file mode 100644 index 00000000..dffb907d Binary files /dev/null and b/image_sdk/build/output/nativeRotatedImg.jpg differ diff --git a/image_sdk/build/output/noise/connectedRemoveNoise.png b/image_sdk/build/output/noise/connectedRemoveNoise.png new file mode 100644 index 00000000..20542457 Binary files /dev/null and b/image_sdk/build/output/noise/connectedRemoveNoise.png differ diff --git a/image_sdk/build/output/noise/nativeRemoveNoise.png b/image_sdk/build/output/noise/nativeRemoveNoise.png new file mode 100644 index 00000000..20542457 Binary files /dev/null and b/image_sdk/build/output/noise/nativeRemoveNoise.png differ diff --git a/image_sdk/build/output/rotatedImg.jpg b/image_sdk/build/output/rotatedImg.jpg new file mode 100644 index 00000000..8e03ccca Binary files /dev/null and b/image_sdk/build/output/rotatedImg.jpg differ diff --git a/image_sdk/build/output/srcImg.jpg b/image_sdk/build/output/srcImg.jpg new file mode 100644 index 00000000..9e9e4933 Binary files /dev/null and b/image_sdk/build/output/srcImg.jpg differ diff --git a/image_sdk/doc/img/lib.png b/image_sdk/doc/img/lib.png new file mode 100644 index 00000000..a7fe5d0e Binary files /dev/null and b/image_sdk/doc/img/lib.png differ diff --git a/image_sdk/doc/img/rotation.png b/image_sdk/doc/img/rotation.png new file mode 100644 index 00000000..0348b131 Binary files /dev/null and b/image_sdk/doc/img/rotation.png differ diff --git a/image_sdk/image_sdk.iml b/image_sdk/image_sdk.iml new file mode 100644 index 00000000..c4f6fdab --- /dev/null +++ b/image_sdk/image_sdk.iml @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/image_sdk/lib/aais-image-lib-0.1.0.jar b/image_sdk/lib/aais-image-lib-0.1.0.jar new file mode 100644 index 00000000..8ea9e381 Binary files /dev/null and b/image_sdk/lib/aais-image-lib-0.1.0.jar differ diff --git a/image_sdk/pom.xml b/image_sdk/pom.xml new file mode 100644 index 00000000..c0144a8e --- /dev/null +++ b/image_sdk/pom.xml @@ -0,0 +1,88 @@ + + + + + 4.0.0 + + calvin + image-sdk + 0.1 + + + UTF-8 + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + 3.8.1 + + + + + + com.google.code.gson + gson + 2.8.6 + + + org.slf4j + slf4j-api + 1.7.30 + + + commons-cli + commons-cli + 1.4 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.12.1 + + + org.bytedeco + javacv-platform + 1.5.2 + + + org.projectlombok + lombok + 1.18.18 + provided + + + junit + junit + 4.13.1 + + + + \ No newline at end of file diff --git a/image_sdk/src/main/java/me/calvin/example/TestBinaryUtils.java b/image_sdk/src/main/java/me/calvin/example/TestBinaryUtils.java new file mode 100644 index 00000000..7b9fb26b --- /dev/null +++ b/image_sdk/src/main/java/me/calvin/example/TestBinaryUtils.java @@ -0,0 +1,45 @@ +package me.calvin.example; + +import me.calvin.opencv.utils.BinaryUtils; +import me.calvin.opencv.utils.GeneralUtils; +import me.calvin.opencv.utils.GrayUtils; +import org.bytedeco.opencv.opencv_core.Mat; +import org.junit.Test; + +public class TestBinaryUtils { + + @Test + /** + * 测试opencv自带的二值化 + */ + public void testBinaryNative(){ + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/binary/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByAdapThreshold(src); + + src = BinaryUtils.binaryNative(src); + + GeneralUtils.saveImg(src , destPath + "binaryNative.png"); + } + + @Test + /** + * 测试自定义二值化 + */ + public void testBinaryzation(){ + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/binary/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByAdapThreshold(src); + + src = BinaryUtils.binaryzation(src); + + GeneralUtils.saveImg(src , destPath + "binaryzation.png"); + + } +} \ No newline at end of file diff --git a/image_sdk/src/main/java/me/calvin/example/TestGrayUtils.java b/image_sdk/src/main/java/me/calvin/example/TestGrayUtils.java new file mode 100644 index 00000000..9ec4709a --- /dev/null +++ b/image_sdk/src/main/java/me/calvin/example/TestGrayUtils.java @@ -0,0 +1,95 @@ +package me.calvin.example; + +import me.calvin.opencv.utils.GeneralUtils; +import me.calvin.opencv.utils.GrayUtils; +import org.bytedeco.opencv.opencv_core.Mat; +import org.junit.Test; + + +/** + * 测试灰度化 + */ +public class TestGrayUtils { + @Test + /** + * 测试opencv自带的灰度化方法 + */ + public void testGrayNative(){ + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/gray/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayNative(src); + + GeneralUtils.saveImg(src , destPath + "grayNative.png"); + } + + @Test + /** + * 测试细粒度灰度化方法 + * 均值灰度化减噪 + */ + public void testGrayColByMidle() { + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/gray/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByMidle(src); + + GeneralUtils.saveImg(src , destPath + "grayRowByMidle.png"); + } + + + @Test + /** + * 测试细粒度灰度化方法 + * k值灰度化减噪 + */ + public void testGrayColByKLargest() { + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/gray/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByKLargest(src); + + GeneralUtils.saveImg(src , destPath + "grayRowByKLargest.png"); + } + + @Test + /** + * 测试细粒度灰度化方法 + * 局部自适应阀值灰度化减噪 + */ + public void testGrayColByPartAdapThreshold() { + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/gray/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByPartAdapThreshold(src); + + GeneralUtils.saveImg(src , destPath + "grayColByPartAdapThreshold.png"); + } + + + @Test + /** + * 测试细粒度灰度化方法 + * 全局自适应阀值灰度化减噪 + */ + public void testGrayColByAdapThreshold() { + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/gray/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByAdapThreshold(src); + + GeneralUtils.saveImg(src , destPath + "grayColByAdapThreshold.png"); + } + + +} \ No newline at end of file diff --git a/image_sdk/src/main/java/me/calvin/example/TestRemoveNoiseUtils.java b/image_sdk/src/main/java/me/calvin/example/TestRemoveNoiseUtils.java new file mode 100644 index 00000000..ce37bc28 --- /dev/null +++ b/image_sdk/src/main/java/me/calvin/example/TestRemoveNoiseUtils.java @@ -0,0 +1,56 @@ +package me.calvin.example; + +import me.calvin.opencv.utils.BinaryUtils; +import me.calvin.opencv.utils.GeneralUtils; +import me.calvin.opencv.utils.GrayUtils; +import me.calvin.opencv.utils.NoiseUtils; +import org.bytedeco.opencv.opencv_core.Mat; +import org.junit.Test; + +/** + * 测试降噪 + */ +public class TestRemoveNoiseUtils { + + @Test + /** + * 测试8邻域降噪 + */ + public void testNativeRemoveNoise(){ + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/noise/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByPartAdapThreshold(src); + + src = BinaryUtils.binaryzation(src); + + // 8邻域降噪 + src = NoiseUtils.navieRemoveNoise(src , 1); + + GeneralUtils.saveImg(src , destPath + "nativeRemoveNoise.png"); + + } + + @Test + /** + * 连通域降噪 + */ + public void testConnectedRemoveNoise(){ + String imgPath = "src/test/resources/1.png"; + String destPath = "build/output/noise/"; + + Mat src = GeneralUtils.matFactory(imgPath); + + src = GrayUtils.grayColByPartAdapThreshold(src); + + src = BinaryUtils.binaryzation(src); + + // 连通域降噪 + src = NoiseUtils.connectedRemoveNoise(src , 1); + + GeneralUtils.saveImg(src , destPath + "connectedRemoveNoise.png"); + + } +} \ No newline at end of file diff --git a/image_sdk/src/main/java/me/calvin/example/TestRotation.java b/image_sdk/src/main/java/me/calvin/example/TestRotation.java new file mode 100644 index 00000000..c8835a79 --- /dev/null +++ b/image_sdk/src/main/java/me/calvin/example/TestRotation.java @@ -0,0 +1,57 @@ +package me.calvin.example; + +import me.calvin.opencv.utils.*; +import org.bytedeco.opencv.opencv_core.*; + +import java.io.IOException; +import java.util.stream.LongStream; + +import static org.bytedeco.opencv.global.opencv_imgproc.*; +import static org.bytedeco.opencv.helper.opencv_imgcodecs.cvLoadImage; + +public class TestRotation { + + public static void main(String[] args) throws IOException { + IplImage marcel_ = cvLoadImage("src/test/resources/ticket_r2.png"); + Mat marcel = new Mat(marcel_); + + GeneralUtils.saveImg(marcel, "build/output/srcImg.jpg"); + + // 边缘检测 + Mat cannyMat = GeneralUtils.canny(marcel); + GeneralUtils.saveImg(cannyMat, "build/output/canny.jpg"); + + // 获取所有轮廓 + MatVector contours = ContourUtils.getContours(cannyMat); + + Mat resultImage = cannyMat.clone(); + LongStream.range(0, contours.size()) + .mapToObj(contours::get) + .forEach( + contour -> { + Mat points = new Mat(); + approxPolyDP(contour, points, arcLength(contour, true) * 0.02, true); + drawContours(resultImage, new MatVector(points), -1, Scalar.BLUE); + }); + GeneralUtils.saveImg(resultImage, "build/output/contours.jpg"); + + // 获取最大外接矩形 + RotatedRect rect = RectUtils.getMaxRect(contours); + Mat rectMat = marcel.clone(); + Scalar scalar = new Scalar(255, 0, 0, 1); + Rect r = rect.boundingRect(); + rectMat = DrawUtils.drawRect(rectMat, r, scalar); + GeneralUtils.saveImg(rectMat, "build/output/maxRect.jpg"); + + // 旋转矩形 + Mat rotatedImg = RotationUtils.rotation(cannyMat, rect); + GeneralUtils.saveImg(rotatedImg, "build/output/rotatedImg.jpg"); + + Mat nativeRotatedImg = RotationUtils.rotation(marcel, rect); + GeneralUtils.saveImg(nativeRotatedImg, "build/output/nativeRotatedImg.jpg"); + + // 裁剪矩形 + Mat cutMat = RectUtils.cutRect(rotatedImg, nativeRotatedImg); + GeneralUtils.saveImg(cutMat, "build/output/cutRect.jpg"); + } +} diff --git a/image_sdk/src/main/resources/log4j2.xml b/image_sdk/src/main/resources/log4j2.xml new file mode 100644 index 00000000..4ec55f77 --- /dev/null +++ b/image_sdk/src/main/resources/log4j2.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + diff --git a/image_sdk/src/test/resources/1.png b/image_sdk/src/test/resources/1.png new file mode 100644 index 00000000..19edbeac Binary files /dev/null and b/image_sdk/src/test/resources/1.png differ diff --git a/image_sdk/src/test/resources/ticket_r1.png b/image_sdk/src/test/resources/ticket_r1.png new file mode 100644 index 00000000..14a55e78 Binary files /dev/null and b/image_sdk/src/test/resources/ticket_r1.png differ diff --git a/image_sdk/src/test/resources/ticket_r2.png b/image_sdk/src/test/resources/ticket_r2.png new file mode 100644 index 00000000..5c5037bc Binary files /dev/null and b/image_sdk/src/test/resources/ticket_r2.png differ diff --git a/image_sdk/src/test/resources/tickets.png b/image_sdk/src/test/resources/tickets.png new file mode 100644 index 00000000..bff5dc54 Binary files /dev/null and b/image_sdk/src/test/resources/tickets.png differ