org.opencv.imgproc.Imgproc.warpAffine()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(205)

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

Imgproc.warpAffine介绍

[英]Applies an affine transformation to an image.

The function warpAffine transforms the source image using the specified matrix:

dst(x,y) = src(M _11 x + M _12 y + M _13, M _21 x + M _22 y + M _23)

when the flag WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with "invertAffineTransform" and then put in the formula above instead of M. The function cannot operate in-place.

Note: cvGetQuadrangleSubPix is similar to cvWarpAffine, but the outliers are extrapolated using replication border mode.
[中]对图像应用仿射变换。
函数warpAffine使用指定的矩阵变换源图像:
dst(x,y)=src(M_11 x+M_12 y+M_13,M_21 x+M_22 y+M_23)
当设置标志WARP_INVERSE_MAP时。否则,首先使用“invertAffineTransform”反转转换,然后将其放入上面的公式中,而不是M。该功能无法在适当位置运行。
注意:cvGetQuadrangleSubPixcvWarpAffine类似,但异常值是使用复制边界模式推断的。

代码示例

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

public static ImageCV warpAffine(Mat source, Mat matrix, Size boxSize, Integer interpolation) {
  if (matrix == null) {
    return (ImageCV) source;
  }
  // System.out.println(matrix.dump());
  Mat srcImg = Objects.requireNonNull(source);
  ImageCV dstImg = new ImageCV();
  if (interpolation == null) {
    interpolation = Imgproc.INTER_LINEAR;
  }
  Imgproc.warpAffine(srcImg, dstImg, matrix, boxSize, interpolation);
  return dstImg;
}

代码示例来源:origin: openpnp/openpnp

private static Mat offset(Mat mat, int offsetX, int offsetY) {
  if (offsetX == 0D && offsetY == 0D) {
    return mat;
  }
  Mat mapMatrix = new Mat(2, 3, CvType.CV_32F) {
    {
      put(0, 0, 1, 0, offsetX);
      put(1, 0, 0, 1, offsetY);
    }
  };
  Mat dst = mat.clone();
  Imgproc.warpAffine(mat, dst, mapMatrix, mat.size(), Imgproc.INTER_LINEAR);
  mat.release();
  mapMatrix.release();
  return dst;
}

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

public static ImageCV getRotatedImage(Mat source, double angle, double centerx, double centery) {
  if (isEqualToZero(angle)) {
    return ImageCV.toImageCV(source);
  }
  Mat srcImg = Objects.requireNonNull(source);
  Point ptCenter = new Point(centerx, centery);
  Mat rot = Imgproc.getRotationMatrix2D(ptCenter, -angle, 1.0);
  ImageCV dstImg = new ImageCV();
  // determine bounding rectangle
  Rect bbox = new RotatedRect(ptCenter, srcImg.size(), -angle).boundingRect();
  // double[] matrix = new double[rot.cols() * rot.rows()];
  // // adjust transformation matrix
  // rot.get(0, 0, matrix);
  // matrix[2] += bbox.width / 2.0 - centerx;
  // matrix[rot.cols() + 2] += bbox.height / 2.0 - centery;
  // rot.put(0, 0, matrix);
  Imgproc.warpAffine(srcImg, dstImg, rot, bbox.size());
  return dstImg;
}

代码示例来源:origin: openpnp/openpnp

Imgproc.warpAffine(image, image, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);

代码示例来源:origin: openpnp/openpnp

static Mat rotateRect(Mat mat, RotatedRect rect, double degrees) {
  // get the affine mattrix
  Mat mapMatrix = Imgproc.getRotationMatrix2D(rect.center, degrees, 1.0);
  // adjust rect angle to coincide with the rotation. This modifies the model
  rect.angle -= degrees;
  // find the new bbox for the now rotated rect
  Rect bbox = rect.boundingRect();
  // adjust transformation matrix
  double[] cx = mapMatrix.get(0, 2);
  double[] cy = mapMatrix.get(1, 2);
  cx[0] += bbox.width / 2D - rect.center.x;
  cy[0] += bbox.height / 2D - rect.center.y;
  mapMatrix.put(0, 2, cx);
  mapMatrix.put(1, 2, cy);
  // rotate and crop
  Imgproc.warpAffine(mat, mat, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);
  mapMatrix.release();
  // adjust the model to the new center
  bbox = rect.boundingRect();
  rect.center.x = bbox.width / 2.0;
  rect.center.y = bbox.height / 2.0;
  return mat;
}

代码示例来源:origin: openpnp/openpnp

private static Mat rotate(Mat mat, double rotation) {
  if (rotation == 0D) {
    return mat;
  }
  // See:
  // http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c
  Point center = new Point(mat.width() / 2D, mat.height() / 2D);
  Mat mapMatrix = Imgproc.getRotationMatrix2D(center, rotation, 1.0);
  // determine bounding rectangle
  Rect bbox = new RotatedRect(center, mat.size(), rotation).boundingRect();
  // adjust transformation matrix
  double[] cx = mapMatrix.get(0, 2);
  double[] cy = mapMatrix.get(1, 2);
  cx[0] += bbox.width / 2D - center.x;
  cy[0] += bbox.height / 2D - center.y;
  mapMatrix.put(0, 2, cx);
  mapMatrix.put(1, 2, cy);
  Mat dst = new Mat(bbox.width, bbox.height, mat.type());
  Imgproc.warpAffine(mat, dst, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);
  mat.release();
  mapMatrix.release();
  return dst;
}

代码示例来源:origin: openpnp/openpnp

Imgproc.warpAffine(mat, dst, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);
mat.release();

相关文章

Imgproc类方法