Class OpenCVUtils

java.lang.Object
cn.net.pap.common.opencv.OpenCVUtils

public class OpenCVUtils extends Object
java opencv4.0.1 handle image
  • Constructor Details

    • OpenCVUtils

      public OpenCVUtils()
  • Method Details

    • templateMatching

      public static Boolean templateMatching(String sourceImg, String templateImg, String targetImg)
      大图找小图
      Parameters:
      sourceImg - 原始大图 image abs path test image(https://sm.ms/image/S4wj2dLm5N1pM8c)
      templateImg - 模板小图 image abs path test image(https://sm.ms/image/9RV7wI6QfYJlxhn)
      targetImg - 匹配出来的结果 image abs path test image(https://sm.ms/image/QZPycMl3FSgihJ1)
    • similarityImage

      public static double similarityImage(String image1Path, String image2Path, String type)
      图像相似度
      Parameters:
      image1Path - 图像1路径
      image2Path - 图像2路径
      type - 相似度算法: Histogram
      Returns:
    • resizeAndCenter

      public static org.opencv.core.Mat resizeAndCenter(org.opencv.core.Mat image, int targetWidth, int targetHeight)
      根据目标尺寸与原始图像的比例来缩放图像,并确保在新图像中居中显示
      Parameters:
      image -
      targetWidth -
      targetHeight -
      Returns:
    • matOfKeyPointImage

      public static byte[] matOfKeyPointImage(String imagePath, Boolean resizeFlag, Integer targetWidth, Integer targetHeight)
      图像特征
      Parameters:
      imagePath -
      Returns:
    • matOfKeyPointImage2

      public static float[] matOfKeyPointImage2(String imagePath)
    • matOfKeyPointImage3

      public static float[] matOfKeyPointImage3(String imagePath, Long arrayLength)
      获得图像特征,可以指定特征长度。
      Parameters:
      imagePath -
      arrayLength -
      Returns:
    • hogFeatureExtraction

      public static float[] hogFeatureExtraction(String imagePath, Integer widthOrHeight)
      HOG 特征提取器, 将原始图像灰度化,并且将原始图像缩放至相同大小后获得特征向量.
      Parameters:
      imagePath -
      widthOrHeight -
      Returns:
    • byteArrayToFloatList

      public static List<Float> byteArrayToFloatList(byte[] bytes)
      将byte类型的arr转换成float
      Returns:
    • bytes2Float

      public static float bytes2Float(byte[] arr)
      将byte数组数据转换成float
      Parameters:
      arr -
      Returns:
    • convertArray

      public static float[] convertArray(List<Float> floatList, Integer maxLength)
    • normalize

      public static float[] normalize(float[] data)
      最大最小 归一化
      Parameters:
      data -
      Returns:
    • rotation

      public static void rotation(String inputPath, String outputPath, double angle)
      旋转图像,并保持图像大小不变。
      Parameters:
      inputPath - 待旋转图像的绝对路径
      outputPath - 旋转后图像的绝对路径
      angle - 角度,逆时针为正, 传入 45 代表 逆时针旋转45度。
    • rotation2

      public static void rotation2(String inputPath, String outputPath, double angle)
    • edgeWeight

      public static void edgeWeight(String inputPath, String outputPath, Integer scope)
      边缘像素加深/加浅,使用 Canny边缘检测找到边缘部分,如果是边缘,则进行边缘像素调整。
      Parameters:
      inputPath -
      outputPath -
      scope - -60 加深
    • gaussianBlur

      public static void gaussianBlur(String inputPath, String outputPath)
      高斯模糊 - 柔化
      Parameters:
      inputPath -
      outputPath -
    • unsharpMasking

      public static void unsharpMasking(String inputPath, String outputPath)
      Unsharp Masking 锐化 是一种通过从原始图像中减去一个模糊版本的图像来增强图像边缘的方法。
      Parameters:
      inputPath -
      outputPath -
    • drawRedBox

      public static void drawRedBox(String inputPath, String outputPath, int x, int y, int width, int height)
      区域内套红
      Parameters:
      inputPath -
      outputPath -
      x -
      y -
      width -
      height -
    • removeBlackEdge

      public static void removeBlackEdge(String inputPath, String outputPath, Integer blackEdgeWidth)
      去黑边
      Parameters:
      inputPath -
      outputPath -
      blackEdgeWidth - 外的处理,假设四周黑色边框的宽度是 blackEdgeWidth 个像素
    • croppedInnerImage

      public static void croppedInnerImage(String inputPath, String outputPath, int x, int y, int width, int height)
      去除区域内
      Parameters:
      inputPath -
      outputPath -
      x -
      y -
      width -
      height -
    • croppedOuterImage

      public static void croppedOuterImage(String inputPath, String outputPath, int x, int y, int width, int height)
      去除区域外
      Parameters:
      inputPath -
      outputPath -
      x -
      y -
      width -
      height -
    • cropImage

      public static void cropImage(String inputPath, String outputPath, int x, int y, int width, int height)
      裁剪
      Parameters:
      inputPath -
      outputPath -
      x -
      y -
      width -
      height -
    • invertColors

      public static void invertColors(String inputPath, String outputPath, int x, int y, int width, int height)
      反色
      Parameters:
      inputPath -
      outputPath -
      x -
      y -
      width -
      height -
    • upSizeImage

      public static void upSizeImage(String inputPath, String outputPath, int width, int height)
      补齐图像
      Parameters:
      inputPath -
      outputPath -
      width -
      height -
    • denoiseImage

      public static void denoiseImage(String inputPath, String outputPath)
      去噪 非局部均值去噪(Non-Local Means Denoising,NLMeans)
      Parameters:
      inputPath -
      outputPath -
    • smoothBackground

      public static void smoothBackground(String inputPath, String outputPath, double colorThreshold)
      背景色平滑
      Parameters:
      inputPath -
      outputPath -
      colorThreshold - 定义颜色相似性阈值 50.0
    • filterBackgroundColor

      public static void filterBackgroundColor(String inputPath, String outputPath, double colorThreshold)
      过滤底色 首先将图像灰度化,像素值为0-255,数值越大越浅,比如可以把数值240以上的区域都填充成白色,就达到了过滤底色的目的。
      Parameters:
      inputPath -
      outputPath -
      colorThreshold - 定义底色过滤阈值 240
    • autoCorrectionGetAngle

      public static double autoCorrectionGetAngle(String inputPath)
      图像倾斜角度
      Parameters:
      inputPath -
      Returns:
    • imread

      public static org.opencv.core.Mat imread(String image)
    • imread

      public static org.opencv.core.Mat imread(String image, int flags)
    • embedMessage

      public static org.opencv.core.Mat embedMessage(org.opencv.core.Mat image, String message)
    • extractMessage

      public static String extractMessage(org.opencv.core.Mat image)
    • dctWaterMarkEncode

      public static void dctWaterMarkEncode(String inputImgPath, String textWatermark, String outputImgPath)
      基于离散余弦变换(DCT)的水印编码功能
      Parameters:
      inputImgPath -
      textWatermark -
      outputImgPath -
    • dctWaterMarkDecode

      public static void dctWaterMarkDecode(String inputImgPath, String outputImgPath)
      基于离散余弦变换(DCT)的水印解码功能
      Parameters:
      inputImgPath -
      outputImgPath -
    • faceCompare

      public static double faceCompare(String img_1, String img_2) throws Exception
      人脸比对
      Parameters:
      img_1 -
      img_2 -
      Returns:
      Throws:
      Exception
    • stitchImages

      public static boolean stitchImages(String imgPath1, String imgPath2, String resultPath)
      拼接融合两张图像并保存到指定路径,类似缝合图像的效果(两周图像有重叠的部分,类似分别拍多张图像后合并)
      Parameters:
      imgPath1 - 第一张图像的文件路径
      imgPath2 - 第二张图像的文件路径
      resultPath - 拼接后图像的保存路径
    • correctColor

      public static boolean correctColor(String imgPath, String resultPath)
      CLAHE(限制对比度自适应直方图均衡化)技术进行图像的色彩矫正
      Parameters:
      imgPath - 输入图像
      resultPath - 输出图像
      Returns:
      矫正后的图像
    • whiteBalance

      public static boolean whiteBalance(String imgPath, String resultPath)
      "灰度世界" 白平衡算法
      Parameters:
      imgPath -
      resultPath -
      Returns: