我想旋转图像。基于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());
}
}
好的,但我没有角度,所以它不会为我工作。 – michael 2014-11-01 20:35:29