org.opencv.imgproc.Imgproc.morphologyEx()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中org.opencv.imgproc.Imgproc.morphologyEx()方法的一些代码示例,展示了Imgproc.morphologyEx()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Imgproc.morphologyEx()方法的具体详情如下:
包路径:org.opencv.imgproc.Imgproc
类名称:Imgproc
方法名:morphologyEx

Imgproc.morphologyEx介绍

[英]Performs advanced morphological transformations.

The function can perform advanced morphological transformations using an erosion and dilation as basic operations.

Opening operation:

dst = open(src, element)= dilate(erode(src, element))

Closing operation:

dst = close(src, element)= erode(dilate(src, element))

Morphological gradient:

dst = morph_grad(src, element)= dilate(src, element)- erode(src, element)

"Top hat":

dst = tophat(src, element)= src - open(src, element)

"Black hat":

dst = blackhat(src, element)= close(src, element)- src

Any of the operations can be done in-place. In case of multi-channel images, each channel is processed independently.

Note:

  • An example using the morphologyEx function for the morphological opening and closing operations can be found at opencv_source_code/samples/cpp/morphology2.cpp
    [中]执行高级形态变换。
    该函数可以使用侵蚀和膨胀作为基本操作来执行高级形态变换。
    开放操作:
    dst=开放(src,元件)=扩张(侵蚀(src,元件))
    关闭操作:
    dst=闭合(src,元件)=侵蚀(扩张(src,元件))
    形态梯度:
    dst=变形梯度(src,元素)=扩张(src,元素)-侵蚀(src,元素)
    “礼帽”:
    dst=tophat(src,element)=src-open(src,element)
    “黑帽子”:
    dst=blackhat(src,元素)=关闭(src,元素)-src
    任何操作都可以就地完成。在多通道图像的情况下,每个通道独立处理。
    注:
    *在opencv_source_code/samples/cpp/morphology2中可以找到使用morphologyEx函数进行形态学打开和关闭操作的示例。cpp

代码示例

代码示例来源:origin: RaiMan/SikuliX2

public static List<Element> detectChanges(Mat base, Mat mChanged) {
 int PIXEL_DIFF_THRESHOLD = 3;
 int IMAGE_DIFF_THRESHOLD = 5;
 Mat mBaseGray = Element.getNewMat();
 Mat mChangedGray = Element.getNewMat();
 Mat mDiffAbs = Element.getNewMat();
 Mat mDiffTresh = Element.getNewMat();
 Mat mChanges = Element.getNewMat();
 List<Element> rectangles = new ArrayList<>();
 Imgproc.cvtColor(base, mBaseGray, toGray);
 Imgproc.cvtColor(mChanged, mChangedGray, toGray);
 Core.absdiff(mBaseGray, mChangedGray, mDiffAbs);
 Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
 if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
  Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat());
  Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
  Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);
  List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  Mat mHierarchy = Element.getNewMat();
  Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  rectangles = contoursToRectangle(contours);
  Core.subtract(mDiffAbs, mDiffAbs, mChanges);
  Imgproc.drawContours(mChanges, contours, -1, new Scalar(255));
  //logShow(mDiffAbs);
 }
 return rectangles;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat open(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat close(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat open(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat close(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat close(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat open(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat close(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_CLOSE, element);
  return outputImage;
}

代码示例来源:origin: JavaOpenCVBook/code

public Mat open(Mat input, int elementSize, int elementShape) {
  Mat outputImage = new Mat();
  Mat element = getKernelFromShape(elementSize, elementShape);
  Imgproc.morphologyEx(input,outputImage, Imgproc.MORPH_OPEN, element);
  return outputImage;
}

代码示例来源:origin: com.sikulix/sikulixapi

public boolean hasChanges(Mat current) {
 int PIXEL_DIFF_THRESHOLD = 5;
 int IMAGE_DIFF_THRESHOLD = 5;
 Mat bg = new Mat();
 Mat cg = new Mat();
 Mat diff = new Mat();
 Mat tdiff = new Mat();
 Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
 Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
 Core.absdiff(bg, cg, diff);
 Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
 if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
  return false;
 }
 Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
 Imgproc.dilate(diff, diff, new Mat());
 Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
 Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);
 List<MatOfPoint> points = new ArrayList<MatOfPoint>();
 Mat contours = new Mat();
 Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
 int n = 0;
 for (Mat pm: points) {
  log(lvl, "(%d) %s", n++, pm);
  printMatI(pm);
 }
 log(lvl, "contours: %s", contours);
 printMatI(contours);
 return true;
}

相关文章

Imgproc类方法