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

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

本文整理了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

  1. public static ImageCV warpAffine(Mat source, Mat matrix, Size boxSize, Integer interpolation) {
  2. if (matrix == null) {
  3. return (ImageCV) source;
  4. }
  5. // System.out.println(matrix.dump());
  6. Mat srcImg = Objects.requireNonNull(source);
  7. ImageCV dstImg = new ImageCV();
  8. if (interpolation == null) {
  9. interpolation = Imgproc.INTER_LINEAR;
  10. }
  11. Imgproc.warpAffine(srcImg, dstImg, matrix, boxSize, interpolation);
  12. return dstImg;
  13. }

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

  1. private static Mat offset(Mat mat, int offsetX, int offsetY) {
  2. if (offsetX == 0D && offsetY == 0D) {
  3. return mat;
  4. }
  5. Mat mapMatrix = new Mat(2, 3, CvType.CV_32F) {
  6. {
  7. put(0, 0, 1, 0, offsetX);
  8. put(1, 0, 0, 1, offsetY);
  9. }
  10. };
  11. Mat dst = mat.clone();
  12. Imgproc.warpAffine(mat, dst, mapMatrix, mat.size(), Imgproc.INTER_LINEAR);
  13. mat.release();
  14. mapMatrix.release();
  15. return dst;
  16. }

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

  1. public static ImageCV getRotatedImage(Mat source, double angle, double centerx, double centery) {
  2. if (isEqualToZero(angle)) {
  3. return ImageCV.toImageCV(source);
  4. }
  5. Mat srcImg = Objects.requireNonNull(source);
  6. Point ptCenter = new Point(centerx, centery);
  7. Mat rot = Imgproc.getRotationMatrix2D(ptCenter, -angle, 1.0);
  8. ImageCV dstImg = new ImageCV();
  9. // determine bounding rectangle
  10. Rect bbox = new RotatedRect(ptCenter, srcImg.size(), -angle).boundingRect();
  11. // double[] matrix = new double[rot.cols() * rot.rows()];
  12. // // adjust transformation matrix
  13. // rot.get(0, 0, matrix);
  14. // matrix[2] += bbox.width / 2.0 - centerx;
  15. // matrix[rot.cols() + 2] += bbox.height / 2.0 - centery;
  16. // rot.put(0, 0, matrix);
  17. Imgproc.warpAffine(srcImg, dstImg, rot, bbox.size());
  18. return dstImg;
  19. }

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

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

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

  1. static Mat rotateRect(Mat mat, RotatedRect rect, double degrees) {
  2. // get the affine mattrix
  3. Mat mapMatrix = Imgproc.getRotationMatrix2D(rect.center, degrees, 1.0);
  4. // adjust rect angle to coincide with the rotation. This modifies the model
  5. rect.angle -= degrees;
  6. // find the new bbox for the now rotated rect
  7. Rect bbox = rect.boundingRect();
  8. // adjust transformation matrix
  9. double[] cx = mapMatrix.get(0, 2);
  10. double[] cy = mapMatrix.get(1, 2);
  11. cx[0] += bbox.width / 2D - rect.center.x;
  12. cy[0] += bbox.height / 2D - rect.center.y;
  13. mapMatrix.put(0, 2, cx);
  14. mapMatrix.put(1, 2, cy);
  15. // rotate and crop
  16. Imgproc.warpAffine(mat, mat, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);
  17. mapMatrix.release();
  18. // adjust the model to the new center
  19. bbox = rect.boundingRect();
  20. rect.center.x = bbox.width / 2.0;
  21. rect.center.y = bbox.height / 2.0;
  22. return mat;
  23. }

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

  1. private static Mat rotate(Mat mat, double rotation) {
  2. if (rotation == 0D) {
  3. return mat;
  4. }
  5. // See:
  6. // http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c
  7. Point center = new Point(mat.width() / 2D, mat.height() / 2D);
  8. Mat mapMatrix = Imgproc.getRotationMatrix2D(center, rotation, 1.0);
  9. // determine bounding rectangle
  10. Rect bbox = new RotatedRect(center, mat.size(), rotation).boundingRect();
  11. // adjust transformation matrix
  12. double[] cx = mapMatrix.get(0, 2);
  13. double[] cy = mapMatrix.get(1, 2);
  14. cx[0] += bbox.width / 2D - center.x;
  15. cy[0] += bbox.height / 2D - center.y;
  16. mapMatrix.put(0, 2, cx);
  17. mapMatrix.put(1, 2, cy);
  18. Mat dst = new Mat(bbox.width, bbox.height, mat.type());
  19. Imgproc.warpAffine(mat, dst, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR);
  20. mat.release();
  21. mapMatrix.release();
  22. return dst;
  23. }

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

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

相关文章

Imgproc类方法