-2

I'm trying to rotate image. Code below can rotate an image successfully but it cuts off corners of the image and it rotates in wrong direction. What could be the problem? I would be grateful if someone give me some advice. Here is the code :

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

And rotated image's background is black but I want it to be transparent or white. Following is the code https://stackoverflow.com/a/45559068/9994984 I'm trying to all steps but not working correctly. Thanks in advance.

  • Alternative to rotate image check below link, https://stackoverflow.com/questions/46657423/rotated-image-coordinates-after-scipy-ndimage-interpolation-rotate – Ethan Hunt 007 Mar 12 '19 at 13:59

1 Answers1

-1

Cuz you don't change the image's size when you rotate it; 1.if you don't want to change the image's size,you shoud increase the window's size,because when you rotate image,the window's width and height is not enough; 2.if you dont want to change the window's size,so you should reduce the image's size.

liwei ni
  • 1
  • 1