org.opencv.core.Rect类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(283)

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

Rect介绍

[英]template class CV_EXPORTS Rect_ ``

// C++ code:

public:

typedef _Tp value_type;

//! various constructors

Rect_();

Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);

Rect_(const Rect_& r);

Rect_(const CvRect& r);

Rect_(const Point_& org, const Size_& sz);

Rect_(const Point_& pt1, const Point_& pt2);

Rect_& operator = (const Rect_& r);

//! the top-left corner

Point_ tl() const;

//! the bottom-right corner

Point_ br() const;

//! size (width, height) of the rectangle

Size_ size() const;

//! area (width*height) of the rectangle

_Tp area() const;

//! conversion to another data type

template operator Rect_() const;

//! conversion to the old-style CvRect

operator CvRect() const;

//! checks whether the rectangle contains the point

bool contains(const Point_& pt) const;

_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle

};

Template class for 2D rectangles, described by the following parameters:

  • Coordinates of the top-left corner. This is a default interpretation of Rect_.x and Rect_.y in OpenCV. Though, in your algorithms you may count x and y from the bottom-left corner.
  • Rectangle width and height.

OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not. For example, the method Rect_.contains returns true if

x

Virtually every loop over an imageROI in OpenCV (where ROI is specified by Rect_) is implemented as: ``

// C++ code:

for(int y = roi.y; y < roi.y + rect.height; y++)

for(int x = roi.x; x < roi.x + rect.width; x++)

//...

In addition to the class members, the following operations on rectangles are implemented:

  • rect = rect +- point (shifting a rectangle by a certain offset)
  • rect = rect +- size (expanding or shrinking a rectangle by a certain amount)
  • rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
  • rect = rect1 & rect2 (rectangle intersection)
  • rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3)
  • rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
  • rect == rect1, rect != rect1 (rectangle comparison)

This is an example how the partial ordering on rectangles can be established (rect1subseteq rect2): ``

// C++ code:

template inline bool

operator & r1, const Rect_& r2)

return (r1 & r2) == r1;

For your convenience, the Rect_<> alias is available:

typedef Rect_ Rect;
[中]模板类CV_导出Rect_//C++代码: 公众: typedef_Tp value_type; //! 各种建设者 Rect_u2;(); 矩形(_Tp_x,_Tp_y,_Tp_宽度,_Tp_高度); Rect_u(const Rect_u&r); Rect_uu(const CvRect&r); 矩形(常数点和组织,常数大小和大小); Rect_u3;(常数点_3;和pt1,常数点_3;和pt2); Rect_u&运算符=(const Rect_u&r); //! 左上角 点tl()常数; //! 右下角 点br()常数; //! 矩形的大小(宽度、高度) Size_uusize()常量; //! 矩形的面积(宽度*高度) _Tp面积()常数; //! 转换为另一种数据类型 模板运算符Rect_uz()const; //! 转换为旧式CvRect 运算符CvRect()常量; //! 检查矩形是否包含该点 布尔包含(常数点u&pt)常数; _Tp x,y,宽度,高度;//<左上角,以及矩形的宽度和高度 }; 二维矩形的模板类,由以下参数描述: *左上角的坐标。这是OpenCV中`Rect_.x`和`Rect_.y`的默认解释。不过,在您的算法中,您可以从左下角开始计算`x`和`y`。 *矩形宽度和高度。 OpenCV通常假定矩形的顶部和左侧边界是包含的,而右侧和底部边界不是包含的。例如,如果 *x* 实际上,OpenCV中imageROI上的每个循环(ROI由`Rect_`指定)都实现为:
//C代码:
for(int y=roi.y;y<roi.y+rect.height;y

对于(intx=roi.x;x<roi.x+rect.width;x++)
//...
除类成员外,还对矩形执行以下操作:
*rect=rect+-point(将矩形移动一定的偏移量)
*rect=rect+-size(按一定量扩展或缩小矩形)
*rect += point, rect -= point, rect += size, rect -= size(增援行动)
*rect = rect1 & rect2(矩形交叉)
*rect = rect1 | rect2(包含rect2rect3的最小面积矩形)
rect &= rect1, rect |= rect1(以及相应的增强操作)
rect == rect1, rect != rect1(矩形比较)
这是一个如何在矩形上建立偏序的示例(rect1
substeq
rect2):``
//C++代码:
模板内联布尔
运算符&r1,常量Rect_U2;&r2)
返回值(r1和r2)=r1;
为方便起见,Rect_<>别名可供选择:
typedef Rect_urect;

代码示例

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

/**
 * create a sub image from this image
 *
 * @param x pixel column
 * @param y pixel row
 * @param w width
 * @param h height
 * @return the new image
 */
public Picture getSub(int x, int y, int w, int h) {
 Picture img = new Picture();
 if (isValid()) {
  img = new Picture(getContent().submat(new Rect(x, y, w, h)));
 }
 return img;
}

代码示例来源:origin: kongqw/OpenCVForAndroid

public Rect(double[] vals) {
  set(vals);
}

代码示例来源:origin: kongqw/OpenCVForAndroid

public boolean inside(Rect r) {
  return r.contains(this);
}

代码示例来源:origin: nroduit/Weasis

private void copyTileToResultImage(Mat tileOutput, Mat resultImage, Rect dstTile) {
  Rect srcTile = new Rect(mPadding, mPadding, mTileSize, mTileSize);
  Point br = dstTile.br();
  if (br.x >= resultImage.cols()) {
    dstTile.width -= br.x - resultImage.cols();
    srcTile.width -= br.x - resultImage.cols();
  }
  if (br.y >= resultImage.rows()) {
    dstTile.height -= br.y - resultImage.rows();
    srcTile.height -= br.y - resultImage.rows();
  }
  Mat tileView = tileOutput.submat(srcTile);
  Mat dstView = resultImage.submat(dstTile);
  assert (tileView.rows() == dstView.rows());
  assert (tileView.cols() == dstView.cols());
  tileView.copyTo(dstView);
}

代码示例来源:origin: raulh82vlc/Image-Detection-Samples

public static void drawEyeRectangle(Rect eyeArea, Mat matrixRgba) {
  Imgproc.rectangle(matrixRgba, eyeArea.tl(), eyeArea.br(),
      new Scalar(255, 0, 0, 255), 2);
}

代码示例来源:origin: nroduit/Weasis

private void copyTileToResultImage(Mat tileOutput, Mat resultImage, Rect srcTile, Rect dstTile) {
  Point br = dstTile.br();
  if (br.x >= resultImage.cols()) {
    dstTile.width -= br.x - resultImage.cols();
    srcTile.width -= br.x - resultImage.cols();
  }
  if (br.y >= resultImage.rows()) {
    dstTile.height -= br.y - resultImage.rows();
    srcTile.height -= br.y - resultImage.rows();
  }
  Mat tileView = tileOutput.submat(srcTile);
  Mat dstView = resultImage.submat(dstTile);
  assert (tileView.rows() == dstView.rows());
  assert (tileView.cols() == dstView.cols());
  tileView.copyTo(dstView);
}

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

private void drawBoundingBox(MatOfPoint currentContour) {
  Rect rectangle = Imgproc.boundingRect(currentContour);
  Imgproc.rectangle(image, rectangle.tl(), rectangle.br(), new Scalar(255,0,0),1);
}

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

public Mat getContent(Element elem) {
 if (SX.isNull(elem)) {
  return makeContent();
 } else {
  return makeContent().submat(new Rect(elem.x, elem.y, elem.w, elem.h));
 }
}

代码示例来源:origin: us.ihmc/IHMCPerception

public void updateCoordinates(Rect coordinates)
{
 this.facialBorder.set(new double[]{coordinates.x, coordinates.y, coordinates.width, coordinates.height});
}

代码示例来源:origin: farkam135/GoIV

public boolean inside(Rect r) {
  return r.contains(this);
}

代码示例来源:origin: raulh82vlc/Image-Detection-Samples

public static void drawFaceShapes(Rect face, Mat matrixRGBA) {
  Point start = face.tl();
  int h = (int) start.y + (face.height / 2);
  int w = (int) start.x + (face.width / 2);
  Imgproc.rectangle(matrixRGBA, start, face.br(),
      FACE_RECT_COLOR, 3);
  Point center = new Point(w, h);
  Imgproc.circle(matrixRGBA, center, 10, new Scalar(255, 0, 0, 255), 3);
}

代码示例来源:origin: raulh82vlc/Image-Detection-Samples

@NonNull
private static Rect getEyeArea(int x, int y, int width, int height) {
  return new Rect(x,
      y,
      width,
      height);
}

代码示例来源:origin: farkam135/GoIV

public Rect(double[] vals) {
  set(vals);
}

代码示例来源:origin: tz28/Chinese-number-gestures-recognition

public boolean inside(Rect r) {
  return r.contains(this);
}

代码示例来源:origin: kongqw/OpenCVForAndroid

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
  // 子线程(非UI线程)
  mRgba = inputFrame.rgba();
  mGray = inputFrame.gray();
  for (ObjectDetector detector : mObjectDetects) {
    // 检测目标
    Rect[] object = detector.detectObject(mGray, mObject);
    for (Rect rect : object) {
      Imgproc.rectangle(mRgba, rect.tl(), rect.br(), detector.getRectColor(), 3);
    }
  }
  return mRgba;
}

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

Math.min(target.h + 2 * margin, mBase.height()));
rSub = new Rectangle(0, 0, mBase.cols(), mBase.rows()).intersection(rSub);
Rect rectSub = new Rect(rSub.x, rSub.y, rSub.width, rSub.height);
mResult = doFindMatch(target, mBase.submat(rectSub), null);
mMinMax = Core.minMaxLoc(mResult);

代码示例来源:origin: com.sikulix/sikulixapi

public Rect(double[] vals) {
  set(vals);
}

代码示例来源:origin: nu.pattern/opencv

public boolean inside(Rect r) {
  return r.contains(this);
}

代码示例来源:origin: kongqw/OpenCVForAndroid

public RotatedRect objectTracking(Mat mRgba) {
  rgba2Hsv(mRgba);
  updateHueImage();
  // 计算直方图的反投影。
  // Imgproc.calcBackProject(hueList, new MatOfInt(0), hist, prob, ranges, 255);
  Imgproc.calcBackProject(hueList, new MatOfInt(0), hist, prob, ranges, 1.0);
  // 计算两个数组的按位连接(dst = src1 & src2)计算两个数组或数组和标量的每个元素的逐位连接。
  Core.bitwise_and(prob, mask, prob, new Mat());
  // 追踪目标
  rotatedRect = Video.CamShift(prob, trackRect, new TermCriteria(TermCriteria.EPS, 10, 1));
  if (null != mOnCalcBackProjectListener) {
    mOnCalcBackProjectListener.onCalcBackProject(prob);
  }
  // 将本次最终到的目标作为下次追踪的对象
  trackRect = rotatedRect.boundingRect();
  Imgproc.rectangle(prob, trackRect.tl(), trackRect.br(), new Scalar(255, 255, 0, 255), 6);
  Log.i(TAG, "objectTracking: 宽度 : " + trackRect.width + "  高度 : " + trackRect.height + "  角度 : " + rotatedRect.angle);
  return rotatedRect;
}

代码示例来源:origin: kongqw/OpenCVForAndroid

public Rect boundingRect()
{
  Point pt[] = new Point[4];
  points(pt);
  Rect r = new Rect((int) Math.floor(Math.min(Math.min(Math.min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
      (int) Math.floor(Math.min(Math.min(Math.min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
      (int) Math.ceil(Math.max(Math.max(Math.max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
      (int) Math.ceil(Math.max(Math.max(Math.max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
  r.width -= r.x - 1;
  r.height -= r.y - 1;
  return r;
}

相关文章