本文整理了Java中org.opencv.imgproc.Imgproc.drawContours()
方法的一些代码示例,展示了Imgproc.drawContours()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Imgproc.drawContours()
方法的具体详情如下:
包路径:org.opencv.imgproc.Imgproc
类名称: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:
代码示例来源: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);
内容来源于网络,如有侵权,请联系作者删除!