如何使用Java扭曲角度校正

时间:2017-01-12 08:58:07

标签: java opencv image-processing

我阅读了一个教程,以便对此site进行倾斜角度修正。但我不明白如何将这些代码转换为 Java

std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)              
  if (*it)                           //what is the meaning of this code (1)
    points.push_back(it.pos());      //what is the meaning of this code (2)

请帮我理解这段代码。

2 个答案:

答案 0 :(得分:1)

对于OpenCV 3.2.0以下是https://felix.abecassis.me/2011/10/opencv-bounding-box-skew-angle/中来自C ++的Java中deskew的完整翻译(稍加修改):

public Mat deskew(Mat src, double angle) {
    Point center = new Point(src.width()/2, src.height()/2);
    Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1.0);
    //1.0 means 100 % scale
    Size size = new Size(src.width(), src.height());
    Imgproc.warpAffine(src, src, rotImage, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS);
    return src;
}

public void computeSkew( String inFile ) {
    //Load this image in grayscale
    Mat img = Imgcodecs.imread( inFile, Imgcodecs.IMREAD_GRAYSCALE );

    //Binarize it
    //Use adaptive threshold if necessary
    //Imgproc.adaptiveThreshold(img, img, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 40);
    Imgproc.threshold( img, img, 200, 255, THRESH_BINARY );

    //Invert the colors (because objects are represented as white pixels, and the background is represented by black pixels)
    Core.bitwise_not( img, img );
    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));

    //We can now perform our erosion, we must declare our rectangle-shaped structuring element and call the erode function
    Imgproc.erode(img, img, element);

    //Find all white pixels
    Mat wLocMat = Mat.zeros(img.size(),img.type());
    Core.findNonZero(img, wLocMat);

    //Create an empty Mat and pass it to the function
    MatOfPoint matOfPoint = new MatOfPoint( wLocMat );

    //Translate MatOfPoint to MatOfPoint2f in order to user at a next step
    MatOfPoint2f mat2f = new MatOfPoint2f();
    matOfPoint.convertTo(mat2f, CvType.CV_32FC2);

    //Get rotated rect of white pixels
    RotatedRect rotatedRect = Imgproc.minAreaRect( mat2f );

    Point[] vertices = new Point[4];
    rotatedRect.points(vertices);
    List<MatOfPoint> boxContours = new ArrayList<>();
    boxContours.add(new MatOfPoint(vertices));
    Imgproc.drawContours( img, boxContours, 0, new Scalar(128, 128, 128), -1);

    double resultAngle = rotatedRect.angle;
    if (rotatedRect.size.width > rotatedRect.size.height)
    {
        rotatedRect.angle += 90.f;
    }

    //Or
    //rotatedRect.angle = rotatedRect.angle < -45 ? rotatedRect.angle + 90.f : rotatedRect.angle;

    Mat result = deskew( Imgcodecs.imread( inFile ), rotatedRect.angle );
    Imgcodecs.imwrite( outputFile, result );

}

答案 1 :(得分:0)

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ValidateDocumentAlignment {

    public ValidateDocumentAlignment() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public boolean isDocumentTiltAngleWithinThresholdLimit(File scannedDoc, int thresholdAngle) {
        int kernelSize = 3;
        int cannyLowerThreshold = 25;
        int cannyUpperThreshold = 50;

        Mat image = new Mat();
        Mat blur = new Mat();
        Mat edged = new Mat();
        Mat dilate = new Mat();
        Mat erode = new Mat();
        int maxValIdx = 0;
        double area = 0;
        List<MatOfPoint> contours = new ArrayList<>();

        Mat sourceImage = Imgcodecs.imread(scannedDoc.getPath(), Imgcodecs.IMREAD_GRAYSCALE);

        Imgproc.adaptiveThreshold(sourceImage, sourceImage, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);
        Core.bitwise_not(sourceImage, sourceImage);
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(kernelSize, kernelSize));
        Imgproc.morphologyEx(sourceImage, image, Imgproc.MORPH_CLOSE, kernel);
        Imgproc.GaussianBlur(image, blur, new Size(7, 7), 0);
        Imgproc.Canny(blur, edged, cannyLowerThreshold, cannyUpperThreshold);
        Imgproc.dilate(edged, dilate, kernel, new Point(-1, -1), 6);
        Imgproc.erode(dilate, erode, kernel, new Point(-1, -1), 3);
        Imgproc.findContours(erode, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
            Rect rect = Imgproc.boundingRect(contours.get(contourIdx));
            if ((rect.height * rect.width) > area) {
                area = rect.height * rect.width;
                maxValIdx = contourIdx;
            }
        }
        RotatedRect rotatedRect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(maxValIdx).toArray()));
        double skewAngle = rotatedRect.angle;
        int acuteAngle = (int) (skewAngle % 90);
        boolean isProperlyAligned = true;
        if (Math.abs(acuteAngle) > thresholdAngle && Math.abs(acuteAngle) < (90 - thresholdAngle)) {
            isProperlyAligned = false;
        }
        return isProperlyAligned;
    }
}