本文整理汇总了Java中org.opencv.imgproc.Imgproc.contourArea方法的典型用法代码示例。如果您正苦于以下问题:Java Imgproc.contourArea方法的具体用法?Java Imgproc.contourArea怎么用?Java Imgproc.contourArea使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.opencv.imgproc.Imgproc
的用法示例。
在下文中一共展示了Imgproc.contourArea方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: gridDetection
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static void gridDetection(Mat img){
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(img,contours,new Mat(),Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = 0;
MatOfPoint max_contour = new MatOfPoint();
Iterator<MatOfPoint> iterator = contours.iterator();
while (iterator.hasNext()){
MatOfPoint contour = iterator.next();
double area = Imgproc.contourArea(contour);
if(area > maxArea){
maxArea = area;
max_contour = contour;
}
}
double epsilon = 0.1*Imgproc.arcLength(new MatOfPoint2f(max_contour.toArray()),true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(max_contour.toArray()),approx,epsilon,true);
RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(max_contour.toArray()));
Mat grid = Thresholding.orderPoints(approx);
Thresholding.approx = approx;
Thresholding.grid = grid;
Thresholding.rect = rect;
}
示例2: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Filters out contours that do not meet certain criteria.
*
* @param inputContours
* is the input list of contours
* @param output
* is the the output list of contours
* @param minArea
* is the minimum area of a contour that will be kept
* @param minPerimeter
* is the minimum perimeter of a contour that will be kept
* @param minWidth
* minimum width of a contour
* @param maxWidth
* maximum width
* @param minHeight
* minimum height
* @param maxHeight
* maximimum height
* @param Solidity
* the minimum and maximum solidity of a contour
* @param minVertexCount
* minimum vertex Count of the contours
* @param maxVertexCount
* maximum vertex Count
* @param minRatio
* minimum ratio of width to height
* @param maxRatio
* maximum ratio of width to height
*/
private void filterContours(List<MatOfPoint> inputContours, double minArea, double minPerimeter, double minWidth,
double maxWidth, double minHeight, double maxHeight, double[] solidity, double maxVertexCount,
double minVertexCount, double minRatio, double maxRatio, List<MatOfPoint> output) {
final MatOfInt hull = new MatOfInt();
output.clear();
// operation
for (int i = 0; i < inputContours.size(); i++) {
final MatOfPoint contour = inputContours.get(i);
final Rect bb = Imgproc.boundingRect(contour);
if (bb.width < minWidth || bb.width > maxWidth)
continue;
if (bb.height < minHeight || bb.height > maxHeight)
continue;
final double area = Imgproc.contourArea(contour);
if (area < minArea)
continue;
if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()), true) < minPerimeter)
continue;
Imgproc.convexHull(contour, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[] { contour.get(index, 0)[0], contour.get(index, 0)[1] };
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
if (solid < solidity[0] || solid > solidity[1])
continue;
if (contour.rows() < minVertexCount || contour.rows() > maxVertexCount)
continue;
final double ratio = bb.width / (double) bb.height;
if (ratio < minRatio || ratio > maxRatio)
continue;
output.add(contour);
}
}
示例3: largeContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static List<MatOfPoint> largeContours(Mat im, int area) {
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(im, contours, hierarchy, 1, Imgproc.RETR_LIST);
// FIXME: stream requires Android sdk >= 24
// return contours.stream()
// .filter(cnt -> Imgproc.contourArea(cnt) > area)
// .collect(Collectors.toList());
List<MatOfPoint> ret = new ArrayList<>();
for (MatOfPoint cnt : contours) {
if (Imgproc.contourArea(cnt) > area) {
ret.add(cnt);
}
}
return ret;
}
示例4: getContourArea
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
private static ArrayList<Rect> getContourArea(Mat mat) {
Mat hierarchy = new Mat();
Mat image = mat.clone();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Rect rect = null;
double maxArea = 300;
ArrayList<Rect> arr = new ArrayList<Rect>();
for (int i = 0; i < contours.size(); i++) {
Mat contour = contours.get(i);
double contourArea = Imgproc.contourArea(contour);
if (contourArea > maxArea) {
rect = Imgproc.boundingRect(contours.get(i));
arr.add(rect);
}
}
return arr;
}
示例5: GetMaxArea
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
private double GetMaxArea(List<MatOfPoint> allConturs){
double currentMax = 0;
for (MatOfPoint c: allConturs){
double area= Imgproc.contourArea(c);
if(area>currentMax){
currentMax = area;
}
}
return currentMax;
}
示例6: GetMinArea
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
private double GetMinArea(List<MatOfPoint> allConturs){
double currentMax = Double.MAX_VALUE;
for (MatOfPoint c: allConturs){
double area= Imgproc.contourArea(c);
if(area<currentMax){
currentMax = area;
}
}
return currentMax;
}
示例7: maxPoint
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
private int maxPoint(List<MatOfPoint> matOfPoints)
{
int matOfPoint= 0;
double maxArea = 0;
for (int i = 0; i < matOfPoints.size(); i++) {
double area = Imgproc.contourArea(matOfPoints.get(i).clone());
if (maxArea < area) {
maxArea = area;
matOfPoint = i;
}
}
return matOfPoint;
}
示例8: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Filters out contours that do not meet certain criteria.
* @param inputContours is the input list of contours
* @param output is the the output list of contours
* @param minArea is the minimum area of a contour that will be kept
* @param minPerimeter is the minimum perimeter of a contour that will be kept
* @param minWidth minimum width of a contour
* @param maxWidth maximum width
* @param minHeight minimum height
* @param maxHeight maximimum height
* @param Solidity the minimum and maximum solidity of a contour
* @param minVertexCount minimum vertex Count of the contours
* @param maxVertexCount maximum vertex Count
* @param minRatio minimum ratio of width to height
* @param maxRatio maximum ratio of width to height
*/
private void filterContours(List<MatOfPoint> inputContours, double minArea,
double minPerimeter, double minWidth, double maxWidth, double minHeight, double
maxHeight, double[] solidity, double maxVertexCount, double minVertexCount, double
minRatio, double maxRatio, List<MatOfPoint> output) {
final MatOfInt hull = new MatOfInt();
output.clear();
//operation
for (int i = 0; i < inputContours.size(); i++) {
final MatOfPoint contour = inputContours.get(i);
final Rect bb = Imgproc.boundingRect(contour);
if (bb.width < minWidth || bb.width > maxWidth) continue;
if (bb.height < minHeight || bb.height > maxHeight) continue;
final double area = Imgproc.contourArea(contour);
if (area < minArea) continue;
if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()), true) < minPerimeter) continue;
Imgproc.convexHull(contour, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[] { contour.get(index, 0)[0], contour.get(index, 0)[1]};
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
if (solid < solidity[0] || solid > solidity[1]) continue;
if (contour.rows() < minVertexCount || contour.rows() > maxVertexCount) continue;
final double ratio = bb.width / (double)bb.height;
if (ratio < minRatio || ratio > maxRatio) continue;
output.add(contour);
}
}
示例9: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
@Override
public List<MatOfPoint> filterContours(List<MatOfPoint> contours) {
List<MatOfPoint> output = new ArrayList<>();
for (MatOfPoint contour : contours) {
final Rect boundingRect = Imgproc.boundingRect(contour);
final double rectArea = boundingRect.area();
final double targetAreaPercent = rectArea / imageArea * 100;
if (!area.inRangeInclusive(targetAreaPercent)) {
continue;
}
final double targetAspect = boundingRect.width / boundingRect.height;
if (!aspectRatio.inRangeInclusive(targetAspect)) {
continue;
}
final double targetArea = Imgproc.contourArea(contour);
final double targetFullness = targetArea / rectArea * 100;
if (!fullness.inRangeInclusive(targetFullness)) {
continue;
}
output.add(contour);
}
return output;
}
示例10: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
@Override
public List<MatOfPoint> filterContours(List<MatOfPoint> contours) {
List<MatOfPoint> output = new ArrayList<>();
// Generated and tested by GRIP, variables slightly renamed
final MatOfInt hull = new MatOfInt();
for (int i = 0; i < contours.size(); i++) {
final MatOfPoint contour = contours.get(i);
final Rect bb = Imgproc.boundingRect(contour);
if (bb.width < width.start || bb.width > width.end)
continue;
if (bb.height < height.start || bb.height > height.end)
continue;
final double area = Imgproc.contourArea(contour);
if (area < minArea)
continue;
if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()), true) < minPerimeter)
continue;
Imgproc.convexHull(contour, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[]{contour.get(index, 0)[0], contour.get(index, 0)[1]};
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
if (solid < solidity.start || solid > solidity.end)
continue;
if (contour.rows() < vertexCount.start || contour.rows() > vertexCount.end)
continue;
final double ratio = bb.width / (double) bb.height;
if (ratio < widthToHeightRatio.start || ratio > widthToHeightRatio.end)
continue;
output.add(contour);
}
return output;
}
示例11: scoreContour
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Score contour based on a variety of factors
* @param mop contour to score
* @return contour score (range from 0 to infinity?)
*/
private double scoreContour(MatOfPoint mop) {
// Score on solidity -- ideally find 100% solid rectangle
final MatOfInt hull = new MatOfInt(); // is this expensive?
final double area = Imgproc.contourArea(mop);
Imgproc.convexHull(mop, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[] { mop.get(index, 0)[0], mop.get(index, 0)[1]};
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
double scoreSolidity = solid;
// Score aspect ratio -- exact ratio is 0.4
final Rect bb = Imgproc.boundingRect(mop);
final double ratio = bb.width / (double)bb.height;
final double targetRatio = 0.4;
final double minRatio = 0.0;
final double maxRatio = 1.0;
double scoreAspectRatio = 0;
if (ratio < minRatio || ratio > maxRatio) {
scoreAspectRatio = 0;
} else {
if (ratio > targetRatio) {
scoreAspectRatio = 1 - (ratio - targetRatio) / (maxRatio - targetRatio);
} else {
scoreAspectRatio = 1 - (targetRatio - ratio) / (targetRatio - minRatio);
}
scoreAspectRatio *= 100;
}
// Compute final score
double score = scoreSolidity + scoreAspectRatio;
return score;
}
示例12: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Filters out contours that do not meet certain criteria.
*
* @param inputContours
* is the input list of contours
* @param output
* is the the output list of contours
* @param minArea
* is the minimum area of a contour that will be kept
* @param minPerimeter
* is the minimum perimeter of a contour that will be kept
* @param minWidth
* minimum width of a contour
* @param maxWidth
* maximum width
* @param minHeight
* minimum height
* @param maxHeight
* maximimum height
* @param Solidity
* the minimum and maximum solidity of a contour
* @param minVertexCount
* minimum vertex Count of the contours
* @param maxVertexCount
* maximum vertex Count
* @param minRatio
* minimum ratio of width to height
* @param maxRatio
* maximum ratio of width to height
*/
private void filterContours (List<MatOfPoint> inputContours,
double minArea,
double minPerimeter, double minWidth, double maxWidth,
double minHeight, double maxHeight, double[] solidity,
double maxVertexCount, double minVertexCount, double minRatio,
double maxRatio, List<MatOfPoint> output)
{
final MatOfInt hull = new MatOfInt();
output.clear();
// operation
for (int i = 0; i < inputContours.size(); i++)
{
final MatOfPoint contour = inputContours.get(i);
final Rect bb = Imgproc.boundingRect(contour);
if (bb.width < minWidth || bb.width > maxWidth)
continue;
if (bb.height < minHeight || bb.height > maxHeight)
continue;
final double area = Imgproc.contourArea(contour);
if (area < minArea)
continue;
if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()),
true) < minPerimeter)
continue;
Imgproc.convexHull(contour, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[]
{contour.get(index, 0)[0], contour.get(index, 0)[1]};
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
if (solid < solidity[0] || solid > solidity[1])
continue;
if (contour.rows() < minVertexCount
|| contour.rows() > maxVertexCount)
continue;
final double ratio = bb.width / (double) bb.height;
if (ratio < minRatio || ratio > maxRatio)
continue;
output.add(contour);
}
}
示例13: detect
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Detect the target in the image.
*
* @param frame The image to detect targetDetection in.
* @return The list of possible targetDetection ordered by confidence from greatest to least.
*/
@Override
public List<Target> detect(Mat frame) {
Mat filtered = filter.filter(frame);
ContourFinder contourFinder = new StandardContourFinder();
List<MatOfPoint> contours = contourFinder.findContours(filtered);
filtered.release();
contours = contourFilter.filterContours(contours);
List<Target> detections = new ArrayList<>();
for (MatOfPoint contour : contours) {
Rect boundary = Imgproc.boundingRect(contour);
double aspectRatio = (boundary.width / (double) boundary.height);
double aspectScore = Scorer.score(aspectRatio, targetSpecs.getWidth() / targetSpecs.getHeight());
double areaRatio = Imgproc.contourArea(contour) / (double) boundary.area();
double areaScore = Scorer.score(areaRatio,
targetSpecs.getArea() / (targetSpecs.getHeight() * targetSpecs.getWidth()));
double confidence = Math.round((aspectScore + areaScore) / 2) / 100.0;
Moments moments = Imgproc.moments(contour);
Point centerOfMass = new Point(moments.m10 / moments.m00, moments.m01 / moments.m00, 0);
Target target = new Target(confidence, boundary.width - 1, boundary.height - 1,
new Point(boundary.x, boundary.y, 0), centerOfMass, frame.size());
detections.add(target);
}
detections.sort((a, b) -> {
if (b.getIsTargetProbability() > a.getIsTargetProbability()) {
return 1;
} else if (a.getIsTargetProbability() > b.getIsTargetProbability()) {
return -1;
}
return 0;
});
return detections;
}
示例14: filterContours
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Filters out contours that do not meet certain criteria.
*
* @param inputContours
* is the input list of contours
* @param output
* is the the output list of contours
* @param minArea
* is the minimum area of a contour that will be kept
* @param minPerimeter
* is the minimum perimeter of a contour that will be kept
* @param minWidth
* minimum width of a contour
* @param maxWidth
* maximum width
* @param minHeight
* minimum height
* @param maxHeight
* maximimum height
* @param Solidity
* the minimum and maximum solidity of a contour
* @param minVertexCount
* minimum vertex Count of the contours
* @param maxVertexCount
* maximum vertex Count
* @param minRatio
* minimum ratio of width to height
* @param maxRatio
* maximum ratio of width to height
*/
private void filterContours(List<MatOfPoint> inputContours, double minArea, double minPerimeter, double minWidth,
double maxWidth, double minHeight, double maxHeight, double[] solidity, double maxVertexCount,
double minVertexCount, double minRatio, double maxRatio, List<MatOfPoint> output) {
final MatOfInt hull = new MatOfInt();
output.clear();
// operation
for (int i = 0; i < inputContours.size(); i++) {
final MatOfPoint contour = inputContours.get(i);
final Rect bb = Imgproc.boundingRect(contour);
if (bb.width < minWidth || bb.width > maxWidth) {
continue;
}
if (bb.height < minHeight || bb.height > maxHeight) {
continue;
}
final double area = Imgproc.contourArea(contour);
if (area < minArea) {
continue;
}
if (Imgproc.arcLength(new MatOfPoint2f(contour.toArray()), true) < minPerimeter) {
continue;
}
Imgproc.convexHull(contour, hull);
MatOfPoint mopHull = new MatOfPoint();
mopHull.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[] { contour.get(index, 0)[0], contour.get(index, 0)[1] };
mopHull.put(j, 0, point);
}
final double solid = 100 * area / Imgproc.contourArea(mopHull);
if (solid < solidity[0] || solid > solidity[1]) {
continue;
}
if (contour.rows() < minVertexCount || contour.rows() > maxVertexCount) {
continue;
}
final double ratio = bb.width / (double) bb.height;
if (ratio < minRatio || ratio > maxRatio) {
continue;
}
output.add(contour);
}
}
示例15: area
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Get the area of the contour
* <p/>
* A highly precise method that integrates the contour with respect to its arc length.
*
* @return Area of the contour
*/
public double area() {
return Imgproc.contourArea(mat);
}