Java 使用openCV旋转图像

Java 使用openCV旋转图像,java,opencv,Java,Opencv,我正在尝试旋转图像。基于,但它不工作,我没有得到任何错误。 有什么问题吗?如果有人给我一些建议,我将不胜感激。 代码如下: public static void main( String[] args ) { Mat warp_dst; Point [] srcTri = new Point[3]; Point [] dstTri = new Point[3]; MatOfPoint2f srcPoints; MatOfPoint2f dstPo

我正在尝试旋转图像。基于,但它不工作,我没有得到任何错误。 有什么问题吗?如果有人给我一些建议,我将不胜感激。 代码如下:

public static void main( String[] args )
{
 Mat warp_dst;     
   Point [] srcTri = new Point[3];
   Point [] dstTri = new Point[3];     
   MatOfPoint2f  srcPoints;
   MatOfPoint2f  dstPoints;    
   Mat warpMat;

  try {
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      File input = new File("C:\\Users\\Samsung\\Desktop\\res\\drawable-nodpi\\test.jpg");
      BufferedImage image = ImageIO.read(input);    

      byte[] data = ((DataBufferByte) image.getRaster().
      getDataBuffer()).getData();
      Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
      mat.put(0, 0, data);

      Mat mat1 = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
      Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);

      byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
      mat1.get(0, 0, data1);
      BufferedImage image1=new BufferedImage(mat1.cols(),mat1.rows()
      ,BufferedImage.TYPE_BYTE_GRAY);
      image1.getRaster().setDataElements(0,0,mat1.cols(),mat1.rows(),data1);


      warp_dst = Mat.zeros(mat1.rows(), mat1.cols(), mat1.type());

      srcTri[0] = new Point(0,0);
      srcTri[1] = new Point(mat1.cols() -1 , 0 );
      srcTri[2] = new Point(0, mat1.rows()- 1);

      dstTri[0] = new Point( mat1.cols()*0.0, mat1.rows()*0.33);
      dstTri[1] = new Point( mat1.cols()*0.85, mat1.rows()*0.25);
      dstTri[2] = new Point( mat1.cols()*0.15, mat1.rows()*0.7);

      srcPoints = new MatOfPoint2f(srcTri);
      dstPoints = new MatOfPoint2f(dstTri);

      warpMat = Imgproc.getAffineTransform(srcPoints, dstPoints);

      Imgproc.warpAffine(mat1, warp_dst, warpMat, warp_dst.size());


      byte[] data2 = new byte[warp_dst.rows()*warp_dst.cols()*(int)(warp_dst.elemSize())];
      mat1.get(0, 0, data2);
      BufferedImage image2=new BufferedImage(warp_dst.cols(),warp_dst.rows()
      ,BufferedImage.TYPE_BYTE_GRAY);
      image2.getRaster().setDataElements(0,0,warp_dst.cols(),warp_dst.rows(),data1);

      File ouptut = new File("C:\\Users\\Samsung\\Desktop\\res\\drawable-nodpi\\grayscale5.jpg");
      ImageIO.write(image2, "jpg", ouptut);
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }

}旋转图像有一种更简单的方法。 代码是C语言的,但是您可以使用相同的方法

void rotate(cv::Mat& src, double angle, cv::Mat& dst)
{
    int len = std::max(src.cols, src.rows);
    cv::Point2f pt(len/2., len/2.);
    cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0);

    cv::warpAffine(src, dst, r, cv::Size(len, len));
}

出于好奇,我检查了一下,结果出了点差错,因为它也不起作用。。(我得到了不变的图像)。有没有人在Java和Android中有旋转图像的工作示例? (在C中没有问题.)


好的,但我没有角度,所以它对我不起作用。
  warp_dst = Mat.zeros(image.getHeight(),image.getWidth(), image.getType());
      Point pt = new Point(mat1.cols()/2, mat1.rows()/2);                         
      Mat M = Imgproc.getRotationMatrix2D(pt, 30, 1.0);     
      Imgproc.warpAffine( mat1, warp_dst, M, mat1.size());