本文整理汇总了Java中org.opencv.imgproc.Imgproc.moments方法的典型用法代码示例。如果您正苦于以下问题:Java Imgproc.moments方法的具体用法?Java Imgproc.moments怎么用?Java Imgproc.moments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.opencv.imgproc.Imgproc
的用法示例。
在下文中一共展示了Imgproc.moments方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBeaconConfig
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static int getBeaconConfig(Image img, VuforiaTrackableDefaultListener beacon, CameraCalibration camCal) {
OpenGLMatrix pose = beacon.getRawPose();
if (pose != null && img != null && img.getPixels() != null) {
Matrix34F rawPose = new Matrix34F();
float[] poseData = Arrays.copyOfRange(pose.transposed().getData(), 0, 12);
rawPose.setData(poseData);
float[][] corners = new float[4][2];
corners[0] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, 276, 0)).getData(); //upper left of beacon
corners[1] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, 276, 0)).getData(); //upper right of beacon
corners[2] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, -92, 0)).getData(); //lower right of beacon
corners[3] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, -92, 0)).getData(); //lower left of beacon
//getting camera image...
Bitmap bm = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.RGB_565);
bm.copyPixelsFromBuffer(img.getPixels());
//turning the corner pixel coordinates into a proper bounding box
Mat crop = bitmapToMat(bm, CvType.CV_8UC3);
float x = Math.min(Math.min(corners[1][0], corners[3][0]), Math.min(corners[0][0], corners[2][0]));
float y = Math.min(Math.min(corners[1][1], corners[3][1]), Math.min(corners[0][1], corners[2][1]));
float width = Math.max(Math.abs(corners[0][0] - corners[2][0]), Math.abs(corners[1][0] - corners[3][0]));
float height = Math.max(Math.abs(corners[0][1] - corners[2][1]), Math.abs(corners[1][1] - corners[3][1]));
//make sure our bounding box doesn't go outside of the image
//OpenCV doesn't like that...
x = Math.max(x, 0);
y = Math.max(y, 0);
width = (x + width > crop.cols())? crop.cols() - x : width;
height = (y + height > crop.rows())? crop.rows() - y : height;
//cropping bounding box out of camera image
final Mat cropped = new Mat(crop, new Rect((int) x, (int) y, (int) width, (int) height));
//filtering out non-beacon-blue colours in HSV colour space
Imgproc.cvtColor(cropped, cropped, Imgproc.COLOR_RGB2HSV_FULL);
//get filtered mask
//if pixel is within acceptable blue-beacon-colour range, it's changed to white.
//Otherwise, it's turned to black
Mat mask = new Mat();
Core.inRange(cropped, BEACON_BLUE_LOW, BEACON_BLUE_HIGH, mask);
Moments mmnts = Imgproc.moments(mask, true);
//calculating centroid of the resulting binary mask via image moments
Log.i("CentroidX", "" + ((mmnts.get_m10() / mmnts.get_m00())));
Log.i("CentroidY", "" + ((mmnts.get_m01() / mmnts.get_m00())));
//checking if blue either takes up the majority of the image (which means the beacon is all blue)
//or if there's barely any blue in the image (which means the beacon is all red or off)
// if (mmnts.get_m00() / mask.total() > 0.8) {
// return VortexUtils.BEACON_ALL_BLUE;
// } else if (mmnts.get_m00() / mask.total() < 0.1) {
// return VortexUtils.BEACON_NO_BLUE;
// }//elseif
//Note: for some reason, we end up with a image that is rotated 90 degrees
//if centroid is in the bottom half of the image, the blue beacon is on the left
//if the centroid is in the top half, the blue beacon is on the right
if ((mmnts.get_m01() / mmnts.get_m00()) < cropped.rows() / 2) {
return BEACON_RED_BLUE;
} else {
return BEACON_BLUE_RED;
}
}
return NOT_VISIBLE;
}
示例2: 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;
}