我正在尝试旋转图像。基于http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html,但它不起作用,我没有得到任何错误。 可能是什么问题呢 ?如果有人给我一些建议,我将不胜感激。 这是代码:
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 :(得分: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));
}
答案 1 :(得分: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());