如何使用OpenCV旋转图像

时间:2019-03-12 13:48:14

标签: java opencv image-processing ocr

我正在尝试旋转图像。下面的代码可以成功旋转图像,但是会切掉图像的角,并且会以错误的方向旋转。可能是什么问题呢?如果有人给我一些建议,我将不胜感激。这是代码:

public void findAngle(){
    FloatBuffer deskew_angle = FloatBuffer.allocate(1);
    IntBuffer orientation = IntBuffer.allocate(1);
    IntBuffer writing_direction = IntBuffer.allocate(1);
    IntBuffer textline_order = IntBuffer.allocate(1);
    final TessAPI api = TessAPI.INSTANCE;

    final TessBaseAPI handle;
    handle = api.TessBaseAPICreate();
    api.TessBaseAPIInit3(handle, "C:\\Program Files\\Tesseract-OCR\\tessdata", "eng");
    api.TessBaseAPISetVariable(handle, "tessedit_char_whitelist",
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?!.,:-");
    File tiff = new File("C:\\Users\\A\\Desktop\\1.jpg");
    BufferedImage image = null;
    try {
        image = ImageIO.read(new FileInputStream(tiff));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } // require jai-imageio lib to read TIFF
    ByteBuffer buf = ImageIOHelper.convertImageData(image);
    int bpp = image.getColorModel().getPixelSize();
    int bytespp = bpp / 8;
    int bytespl = (int) Math.ceil(image.getWidth() * bpp / 8.0);
    api.TessBaseAPISetPageSegMode(handle, TessAPI.TessPageSegMode.PSM_AUTO_OSD);
    api.TessBaseAPISetImage(handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
    TessPageIterator pi = api.TessBaseAPIAnalyseLayout(handle);
    api.TessPageIteratorOrientation(pi, orientation, writing_direction, textline_order, deskew_angle);

    float angle = deskew_angle.get(0);
    System.out.println("Angle : "+angle);

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    String resultString = "";

    Mat frame = Imgcodecs.imread(lFile, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    Point center = new Point(350,350);
    if(angle == 270 || angle == -90) {
        Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1);
        Imgproc.warpAffine(frame, frame, rotImage, new Size(frame.cols(),frame.cols()));
    }
    else if (angle == 180 || angle == -180 ){
        Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1);
        Imgproc.warpAffine(frame, frame, rotImage, new Size(frame.cols(),frame.cols()));
    }
    else if(angle == 90 || angle == -270) {
        Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1);
        Imgproc.warpAffine(frame, frame, rotImage, new Size(frame.cols(),frame.cols()));
    }
    else if(angle == 360 || angle == 0 || angle == -360) {
        Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 0.65);
        Imgproc.warpAffine(frame, frame, rotImage, new Size(frame.cols(),frame.cols()));
    }

    System.out.println(String.format(
            "Orientation: %s\nWritingDirection: %s\nTextlineOrder: %s\nDeskew angle: %.4f\n",
            Utils.getConstantName(orientation.get(), TessOrientation.class),
            Utils.getConstantName(writing_direction.get(), TessWritingDirection.class),
            Utils.getConstantName(textline_order.get(), TessTextlineOrder.class),
            deskew_angle.get(0)));
    api.TessBaseAPIClear(handle);
    api.TessBaseAPIEnd(handle);
    api.TessBaseAPIDelete(handle);
    Imgcodecs.imwrite("C:\\Users\\A\\Desktop\\a.jpg", img);
    }

旋转图像的背景是黑色,但我希望它是透明或白色的。以下是代码https://stackoverflow.com/a/45559068/9994984,我正在尝试所有步骤,但无法正常工作。预先感谢。

0 个答案:

没有答案