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

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

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

Imgproc.findContours介绍

[英]Finds contours in a binary image.

The function retrieves contours from the binary image using the algorithm [Suzuki85]. The contours are a useful tool for shape analysis and object detection and recognition. See squares.c in the OpenCV sample directory.

Note: Source image is modified by this function. Also, the function does not take into account 1-pixel border of the image (it's filled with 0's and used for neighbor analysis in the algorithm), therefore the contours touching the image border will be clipped.

Note: If you use the new Python interface then the CV_ prefix has to be omitted in contour retrieval mode and contour approximation method parameters (for example, use cv2.RETR_LIST and cv2.CHAIN_APPROX_NONE parameters). If you use the old Python interface then these parameters have the CV_ prefix (for example, use cv.CV_RETR_LIST and cv.CV_CHAIN_APPROX_NONE).

Note:

  • An example using the findContour functionality can be found at opencv_source_code/samples/cpp/contours2.cpp
  • An example using findContours to clean up a background segmentation result at opencv_source_code/samples/cpp/segment_objects.cpp
  • (Python) An example using the findContour functionality can be found at opencv_source/samples/python2/contours.py
  • (Python) An example of detecting squares in an image can be found at opencv_source/samples/python2/squares.py
    [中]在二值图像中查找轮廓。
    该函数使用算法[Suzuki85]从二值图像检索轮廓。轮廓线是形状分析、目标检测和识别的有用工具。请参见OpenCV示例目录中的squares.c
    注意:源image由此函数修改。此外,该函数不考虑图像的1像素边界(它用0填充,并在算法中用于邻居分析),因此将剪裁与图像边界接触的轮廓。
    注意:如果使用新的Python接口,则在轮廓检索模式和轮廓近似方法参数中必须省略CV_前缀(例如,使用cv2.RETR_LISTcv2.CHAIN_APPROX_NONE参数)。如果使用旧的Python接口,则这些参数具有CV_前缀(例如,使用cv.CV_RETR_LISTcv.CV_CHAIN_APPROX_NONE)。
    注:
    *使用findContour功能的示例可在opencv_source_code/samples/cpp/contours2中找到。cpp
    *在opencv_源代码/samples/cpp/segment_对象上,使用FindOntours清除背景分割结果的示例。cpp
    *(Python)可以在opencv_source/samples/python2/contours上找到使用findContour功能的示例。派克
    *(Python)在opencv_source/samples/python2/squares中可以找到一个检测图像中正方形的示例。派克

代码示例

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

  1. public static List<MatOfPoint> getContours(Mat mBase, boolean external) {
  2. Mat mHierarchy = Element.getNewMat();
  3. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  4. if (external) {
  5. Imgproc.findContours(mBase, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  6. } else {
  7. Imgproc.findContours(mBase, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  8. }
  9. return contours;
  10. }

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

  1. public static List<Element> detectChanges(Mat base, Mat mChanged) {
  2. int PIXEL_DIFF_THRESHOLD = 3;
  3. int IMAGE_DIFF_THRESHOLD = 5;
  4. Mat mBaseGray = Element.getNewMat();
  5. Mat mChangedGray = Element.getNewMat();
  6. Mat mDiffAbs = Element.getNewMat();
  7. Mat mDiffTresh = Element.getNewMat();
  8. Mat mChanges = Element.getNewMat();
  9. List<Element> rectangles = new ArrayList<>();
  10. Imgproc.cvtColor(base, mBaseGray, toGray);
  11. Imgproc.cvtColor(mChanged, mChangedGray, toGray);
  12. Core.absdiff(mBaseGray, mChangedGray, mDiffAbs);
  13. Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
  14. if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
  15. Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  16. Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat());
  17. Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
  18. Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);
  19. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  20. Mat mHierarchy = Element.getNewMat();
  21. Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  22. rectangles = contoursToRectangle(contours);
  23. Core.subtract(mDiffAbs, mDiffAbs, mChanges);
  24. Imgproc.drawContours(mChanges, contours, -1, new Scalar(255));
  25. //logShow(mDiffAbs);
  26. }
  27. return rectangles;
  28. }

代码示例来源:origin: nroduit/Weasis

  1. public static List<MatOfPoint> findContours(RenderedImage source, Rectangle area) {
  2. Mat srcImg = ImageConversion.toMat(Objects.requireNonNull(source), area);
  3. List<MatOfPoint> contours = new ArrayList<>();
  4. Mat hierachy = new Mat();
  5. Imgproc.findContours(srcImg, contours, hierachy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
  6. return contours;
  7. }

代码示例来源:origin: openpnp/openpnp

  1. public FluentCv findContours(List<MatOfPoint> contours, String... tag) {
  2. Mat hierarchy = new Mat();
  3. Imgproc.findContours(mat, contours, hierarchy, Imgproc.RETR_LIST,
  4. Imgproc.CHAIN_APPROX_NONE);
  5. return store(mat, tag);
  6. }

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

  1. Imgproc.findContours(contourMat, contours, new Mat(), Imgproc.CHAIN_APPROX_NONE,Imgproc.CHAIN_APPROX_SIMPLE);
  2. System.out.println("Number of found contours: "+contours.size());
  3. for(int i=0;i<contours.size();i++){

代码示例来源:origin: openpnp/openpnp

  1. @Override
  2. public Result process(CvPipeline pipeline) throws Exception {
  3. Mat mat = pipeline.getWorkingImage();
  4. List<MatOfPoint> contours = new ArrayList<>();
  5. Mat hierarchy = new Mat();
  6. Imgproc.findContours(mat, contours, hierarchy, retrievalMode.getCode(), approximationMethod.getCode());
  7. hierarchy.release();
  8. return new Result(null, contours);
  9. }
  10. }

代码示例来源:origin: hschott/Camdroid

  1. protected void execute() {
  2. out = gray();
  3. Imgproc.equalizeHist(out, out);
  4. synchronized (mog) {
  5. mog.apply(out, this.mask, (double) (-10 + learning_rate) / 10);
  6. }
  7. Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE,
  8. new Size(3, 3));
  9. Imgproc.dilate(mask, mask, kernel);
  10. ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  11. Imgproc.findContours(this.mask, contours, new Mat(),
  12. Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  13. double maxheight = object_max_size * this.in.height() / 100;
  14. double minheight = object_min_size * this.in.height() / 100;
  15. Iterator<MatOfPoint> each = contours.iterator();
  16. each = contours.iterator();
  17. while (each.hasNext()) {
  18. MatOfPoint contour = each.next();
  19. Rect rect = Imgproc.boundingRect(contour);
  20. if (rect.height > minheight && rect.height < maxheight) {
  21. Imgproc.rectangle(out, rect.tl(), rect.br(), new Scalar(255,
  22. 0, 0), 1);
  23. }
  24. }
  25. }
  26. }

代码示例来源:origin: nroduit/Weasis

  1. public List<MatOfPoint> getIsoDoseContourPoints(KeyDouble slicePosition, double isoDoseThreshold) {
  2. List<MatOfPoint> contours = new ArrayList<>();
  3. // Convert from threshold in cCy to raw pixel value threshold
  4. double rawThreshold = (isoDoseThreshold / 100) / this.doseGridScaling;
  5. DicomImageElement dosePlane = (DicomImageElement) this.getDosePlaneBySlice(slicePosition.getValue());
  6. int rows = dosePlane.getImage().toMat().rows();
  7. int cols = dosePlane.getImage().toMat().cols();
  8. Mat src = new Mat(rows, cols, CvType.CV_32FC1);
  9. Mat thr = new Mat(rows, cols, CvType.CV_32FC1);
  10. dosePlane.getImage().toMat().convertTo(src, CvType.CV_32FC1);
  11. Mat hierarchy = new Mat();
  12. Imgproc.threshold(src, thr, rawThreshold, 255, Imgproc.THRESH_BINARY);
  13. Mat thrSrc = new Mat(rows, cols, CvType.CV_8U);
  14. thr.convertTo(thrSrc, CvType.CV_8U);
  15. Imgproc.findContours(thrSrc, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
  16. return contours;
  17. }

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

  1. public boolean hasChanges(Mat current) {
  2. int PIXEL_DIFF_THRESHOLD = 5;
  3. int IMAGE_DIFF_THRESHOLD = 5;
  4. Mat bg = new Mat();
  5. Mat cg = new Mat();
  6. Mat diff = new Mat();
  7. Mat tdiff = new Mat();
  8. Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
  9. Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
  10. Core.absdiff(bg, cg, diff);
  11. Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
  12. if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
  13. return false;
  14. }
  15. Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
  16. Imgproc.dilate(diff, diff, new Mat());
  17. Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
  18. Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);
  19. List<MatOfPoint> points = new ArrayList<MatOfPoint>();
  20. Mat contours = new Mat();
  21. Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  22. int n = 0;
  23. for (Mat pm: points) {
  24. log(lvl, "(%d) %s", n++, pm);
  25. printMatI(pm);
  26. }
  27. log(lvl, "contours: %s", contours);
  28. printMatI(contours);
  29. return true;
  30. }

相关文章

Imgproc类方法