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

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

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

Imgproc.drawContours介绍

[英]Draws contours outlines or filled contours.

The function draws contour outlines in the image if thickness >= 0 or fills the area bounded by the contours ifthickness<0. The example below shows how to retrieve connected components from the binary image and label them: ``

// C++ code:

#include "cv.h"

#include "highgui.h"

using namespace cv;

int main(int argc, char argv)

Mat src;

// the first command-line parameter must be a filename of the binary

// (black-n-white) image

if(argc != 2 || !(src=imread(argv[1], 0)).data)

return -1;

Mat dst = Mat.zeros(src.rows, src.cols, CV_8UC3);

src = src > 1;

namedWindow("Source", 1);

imshow("Source", src);

vector > contours;

vector hierarchy;

findContours(src, contours, hierarchy,

CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

// iterate through all the top-level contours,

// draw each connected component with its own random color

int idx = 0;

for(; idx >= 0; idx = hierarchy[idx][0])

Scalar color(rand()&255, rand()&255, rand()&255);

drawContours(dst, contours, idx, color, CV_FILLED, 8, hierarchy);

namedWindow("Components", 1);

imshow("Components", dst);

waitKey(0);

Note:

  • An example using the drawContour functionality can be found at opencv_source_code/samples/cpp/contours2.cpp
  • An example using drawContours to clean up a background segmentation result at opencv_source_code/samples/cpp/segment_objects.cpp
  • (Python) An example using the drawContour functionality can be found at opencv_source/samples/python2/contours.py
    [中]绘制轮廓或填充轮廓。
    如果厚度>=0,则该函数在图像中绘制轮廓线;如果厚度<0,则该函数填充轮廓线所包围的区域。下面的示例显示如何从二进制图像检索连接的组件并为其添加标签:``
    //C++代码:
    #包括“cv.h”
    #包括“highgui.h”
    使用名称空间cv;
    int main(int argc、char argv)
    Mat-src;
    //第一个命令行参数必须是二进制文件的文件名
    //(黑白)图像
    if(argc!=2 | |!(src=imread(argv[1],0))。(数据)
    返回-1;
    Mat dst=Mat。零(src.rows、src.cols、CV_8UC3);
    src=src>1;
    名称(“来源”,1);
    imshow(“来源”,src);
    矢量>等高线;
    向量层次;
    findContours(src、等高线、层次结构、,
    CV_RETR_CCOMP,CV_CHAIN_近似_简单);
    //遍历所有顶级轮廓,
    //用自己的随机颜色绘制每个连接的组件
    int-idx=0;
    对于(;idx>=0;idx=hierarchy[idx][0])
    标量颜色(rand()&255,rand()&255,rand()&255);
    绘制轮廓(dst、轮廓、idx、颜色、CV_填充、8、层次);
    名称(“组件”,1);
    imshow(“组件”,dst);
    等待键(0);
    注:
    *使用drawContour功能的示例可在opencv_source_code/samples/cpp/contours2中找到。cpp
    *使用drawContours清理opencv_源代码/samples/cpp/segment_对象的背景分割结果的示例。cpp
    *(Python)使用drawContour功能的示例可以在opencv_source/samples/python2/contours上找到。派克

代码示例

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

public static Mat drawContours(List<MatOfPoint> contours, Mat mBase) {
 Mat mResult = Element.getNewMat();
 Core.subtract(mBase, mBase, mResult);
 Imgproc.drawContours(mResult, contours, -1, new Scalar(255));
 return mResult;
}

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

public static Mat drawContoursInImage(List<MatOfPoint> contours, Mat mBase) {
 Mat mResult = Element.getNewMat();
 Mat mWork = new Mat();
 Imgproc.cvtColor(mBase, mWork, toGray);
 Imgproc.cvtColor(mWork, mResult, toColor);
 Imgproc.drawContours(mResult, contours, -1, new Scalar(0, 0, 255));
 return mResult;
}

代码示例来源: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: openpnp/openpnp

public FluentCv drawContours(List<MatOfPoint> contours, Color color, int thickness,
    String... tag) {
  if (color == null) {
    for (int i = 0; i < contours.size(); i++) {
      Imgproc.drawContours(mat, contours, i, colorToScalar(indexedColor(i)), thickness);
    }
  }
  else {
    Imgproc.drawContours(mat, contours, -1, colorToScalar(color), thickness);
  }
  return store(mat, tag);
}

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

@Override
  public Result process(CvPipeline pipeline) throws Exception {
    if (contoursStageName == null) {
      throw new Exception("contoursStageName is required.");
    }
    Result result = pipeline.getResult(contoursStageName);
    if (result == null || result.model == null) {
      throw new Exception("No model found in results.");
    }
    Mat mat = pipeline.getWorkingImage();
    List<MatOfPoint> contours = (List<MatOfPoint>) result.model;
    if (index < 0) {
      for (int i = 0; i < contours.size(); i++) {
        Imgproc.drawContours(mat, contours, i, FluentCv.colorToScalar(color == null ? FluentCv.indexedColor(i) : color), thickness);
      }
    }
    else {
      Imgproc.drawContours(mat, contours, index, FluentCv.colorToScalar(color == null ? FluentCv.indexedColor(index) : color), thickness);
    }
    return null;
  }
}

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

Imgproc.drawContours(image, contours, i, new Scalar(0,255,0), thickness);
System.out.println("Area: "+currentArea);
Imgproc.drawContours(image, contours, i, new Scalar(0,0,255), thickness);

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

private void drawConvexHull(MatOfPoint currentContour) {
  MatOfInt hull = new MatOfInt();
  Imgproc.convexHull(currentContour, hull);    
  
  List<MatOfPoint> hullContours = new ArrayList<MatOfPoint>();
  MatOfPoint hullMat = new MatOfPoint();
  hullMat.create((int)hull.size().height,1,CvType.CV_32SC2);
  
  for(int j = 0; j < hull.size().height ; j++)
  {
    int index = (int)hull.get(j, 0)[0];
    double[] point = new double[] {
      currentContour.get(index, 0)[0], currentContour.get(index, 0)[1]
    };
    hullMat.put(j, 0, point);
  } 
  hullContours.add(hullMat);
  Imgproc.drawContours(image, hullContours, 0, new Scalar(128,0,0), 2);
}

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

Imgproc.drawContours(mask, multi, i, new Scalar(255, 255, 255), -1);

相关文章

微信公众号

最新文章

更多

Imgproc类方法