2014-11-01 142 views
0

我想旋转图像。基于http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html,但它不起作用,我没有得到任何错误。 可能是什么问题?如果有人给我一些建议,我将不胜感激。 下面是代码:使用openCV旋转图像

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()); 
     } 

}

回答

0

有旋转图像更简单的方法。 的代码是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)); 
} 
+0

好的,但我没有角度,所以它不会为我工作。 – michael 2014-11-01 20:35:29

0

我检查了好奇和不得不做一些错误的,因为它也不起作用。(我得到的不变图像)。有没有人有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());