本文整理了Java中org.opencv.imgproc.Imgproc.findContours()
方法的一些代码示例,展示了Imgproc.findContours()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Imgproc.findContours()
方法的具体详情如下:
包路径:org.opencv.imgproc.Imgproc
类名称: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:
squares.c
。image
由此函数修改。此外,该函数不考虑图像的1像素边界(它用0填充,并在算法中用于邻居分析),因此将剪裁与图像边界接触的轮廓。CV_
前缀(例如,使用cv2.RETR_LIST
和cv2.CHAIN_APPROX_NONE
参数)。如果使用旧的Python接口,则这些参数具有CV_
前缀(例如,使用cv.CV_RETR_LIST
和cv.CV_CHAIN_APPROX_NONE
)。代码示例来源:origin: RaiMan/SikuliX2
public static List<MatOfPoint> getContours(Mat mBase, boolean external) {
Mat mHierarchy = Element.getNewMat();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
if (external) {
Imgproc.findContours(mBase, contours, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
} else {
Imgproc.findContours(mBase, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
}
return contours;
}
代码示例来源: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: nroduit/Weasis
public static List<MatOfPoint> findContours(RenderedImage source, Rectangle area) {
Mat srcImg = ImageConversion.toMat(Objects.requireNonNull(source), area);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierachy = new Mat();
Imgproc.findContours(srcImg, contours, hierachy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
return contours;
}
代码示例来源:origin: openpnp/openpnp
public FluentCv findContours(List<MatOfPoint> contours, String... tag) {
Mat hierarchy = new Mat();
Imgproc.findContours(mat, contours, hierarchy, Imgproc.RETR_LIST,
Imgproc.CHAIN_APPROX_NONE);
return store(mat, tag);
}
代码示例来源:origin: JavaOpenCVBook/code
Imgproc.findContours(contourMat, contours, new Mat(), Imgproc.CHAIN_APPROX_NONE,Imgproc.CHAIN_APPROX_SIMPLE);
System.out.println("Number of found contours: "+contours.size());
for(int i=0;i<contours.size();i++){
代码示例来源:origin: openpnp/openpnp
@Override
public Result process(CvPipeline pipeline) throws Exception {
Mat mat = pipeline.getWorkingImage();
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(mat, contours, hierarchy, retrievalMode.getCode(), approximationMethod.getCode());
hierarchy.release();
return new Result(null, contours);
}
}
代码示例来源:origin: hschott/Camdroid
protected void execute() {
out = gray();
Imgproc.equalizeHist(out, out);
synchronized (mog) {
mog.apply(out, this.mask, (double) (-10 + learning_rate) / 10);
}
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE,
new Size(3, 3));
Imgproc.dilate(mask, mask, kernel);
ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(this.mask, contours, new Mat(),
Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double maxheight = object_max_size * this.in.height() / 100;
double minheight = object_min_size * this.in.height() / 100;
Iterator<MatOfPoint> each = contours.iterator();
each = contours.iterator();
while (each.hasNext()) {
MatOfPoint contour = each.next();
Rect rect = Imgproc.boundingRect(contour);
if (rect.height > minheight && rect.height < maxheight) {
Imgproc.rectangle(out, rect.tl(), rect.br(), new Scalar(255,
0, 0), 1);
}
}
}
}
代码示例来源:origin: nroduit/Weasis
public List<MatOfPoint> getIsoDoseContourPoints(KeyDouble slicePosition, double isoDoseThreshold) {
List<MatOfPoint> contours = new ArrayList<>();
// Convert from threshold in cCy to raw pixel value threshold
double rawThreshold = (isoDoseThreshold / 100) / this.doseGridScaling;
DicomImageElement dosePlane = (DicomImageElement) this.getDosePlaneBySlice(slicePosition.getValue());
int rows = dosePlane.getImage().toMat().rows();
int cols = dosePlane.getImage().toMat().cols();
Mat src = new Mat(rows, cols, CvType.CV_32FC1);
Mat thr = new Mat(rows, cols, CvType.CV_32FC1);
dosePlane.getImage().toMat().convertTo(src, CvType.CV_32FC1);
Mat hierarchy = new Mat();
Imgproc.threshold(src, thr, rawThreshold, 255, Imgproc.THRESH_BINARY);
Mat thrSrc = new Mat(rows, cols, CvType.CV_8U);
thr.convertTo(thrSrc, CvType.CV_8U);
Imgproc.findContours(thrSrc, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
return contours;
}
代码示例来源: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;
}
内容来源于网络,如有侵权,请联系作者删除!