1. Correct soil picture by hough line

public static void getCorrect1(Mat image) { Mat clone=image.clone(); Mat src=image.clone(); int width = image.width(); int height = image.height(); int pointCount = width * height; Mat points=image.reshape(3, pointCount); points.convertTo(points, CvType.CV_32F); Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0); HighGui.imshow("GaussianBlur", clone); Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY); HighGui.imshow("GRY", clone); Int lowThresh=20; // In general, maxVal is recommended to be 2 to 3 times as high as minVal. Imgproc.Canny(clone, clone,lowThresh, lowThresh*3,3); HighGui.imshow("Canny", clone); Mat storage = new Mat(); /** * HoughLines(Mat image, Mat lines, double rho, double theta, int threshold, double srn, double stn, double min_theta, The hough line transform detects the output vector of the line, denoted by (ρ,θ) * rho's distance accuracy in pixels (the unit radius of the progressive size in line search) * theta The Angle precision in radians (the Angle unit of the progressive dimension when searching a line) * threshold Threshold parameter of the accumulated plane (the value that a line must reach in the accumulated plane when it is recognized) * SRN For multi-scale Hough transform, this is the divisor distance of the third parameter progressive dimension. Rho/SRN * min_theta * max_theta */ double sum = 0; double angle=0; Imgproc.HoughLines(Clone, Storage, 1, math.pi / 180.0, 200, 0, 0); for (int x = 0; x < storage.rows(); x++) { double[] vec = storage.get(x, 0); double rho = vec[0]; double theta = vec[1]; Point pt1 = new Point(); Point pt2 = new Point(); double a = Math.cos(theta); double b = Math.sin(theta); double x0 = a * rho; double y0 = b * rho; pt1.x = Math.round(x0 + 1000 * (-b)); pt1.y = Math.round(y0 + 1000 * (a)); pt2.x = Math.round(x0 - 1000 * (-b)); pt2.y = Math.round(y0 - 1000 * (a)); sum += theta; Imgproc.line(clone, pt1, pt2, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_4, 0); } HighGui.imshow("houghLines", clone); double average = sum / storage.rows(); Angle = average/ math. PI * 180-90; System.out.println("average:"+angle); Point center=new Point(); center.x=image.cols()/2; center.y=image.rows()/2; / / Mat rotation matrix operator matrix. = Imgproc getRotationMatrix2D (center, Angle, 1); Imgproc.warpAffine(src, src, matrix,src.size(), 1, 0, new Scalar(0, 0, 0)); HighGui.imshow("rotation", src); }Copy the code

2. Check the picture of corrected soil by contour

public static Mat getCorrect2(Mat image) { Mat clone=image.clone(); Mat src=image.clone(); int width = image.width(); int height = image.height(); int pointCount = width * height; Mat points=image.reshape(3, pointCount); points.convertTo(points, CvType.CV_32F); Imgproc.GaussianBlur(clone, clone, new Size(3, 3), 0, 0); HighGui.imshow("GaussianBlur", clone); Imgproc.cvtColor(clone, clone,Imgproc.COLOR_BGR2GRAY); HighGui.imshow("GRY", clone); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours(Clone, contours, hierarchy, imgproc. RETR_EXTERNAL, imgproc. CHAIN_APPROX_NONE, new Point(0, 0)); Double area = imgproc.boundingRect (contours.get(0)).area(); double area = imgproc.boundingRect (contours.get(0)).area(); int index = 0; For (int I = 0; i < contours.size(); i++) { double tempArea = Imgproc.boundingRect(contours.get(i)).area(); if (tempArea > area) { area = tempArea; index = i; } } MatOfPoint2f matOfPoint2f = new MatOfPoint2f(contours.get(index).toArray()); RotatedRect rect = Imgproc.minAreaRect(matOfPoint2f); [] rectPoint = new Point[4]; rect.points(rectpoint); double line1 = Math.sqrt((rectpoint[1].y - rectpoint[0].y)*(rectpoint[1].y - rectpoint[0].y) + (rectpoint[1].x - rectpoint[0].x)*(rectpoint[1].x - rectpoint[0].x)); double line2 = Math.sqrt((rectpoint[3].y - rectpoint[0].y)*(rectpoint[3].y - rectpoint[0].y) + (rectpoint[3].x - rectpoint[0].x)*(rectpoint[3].x - rectpoint[0].x)); double angle = rect.angle; if (line1 > line2) { angle = 90 + angle; } Point center = rect.center; Mat CorrectImg = new Mat(clone.size(), clone.type()); clone.copyTo(CorrectImg); / / Mat rotation matrix operator matrix. = Imgproc getRotationMatrix2D (center, Angle, 0.8); Imgproc.warpAffine(src, src, matrix, CorrectImg.size(), 1, 0, new Scalar(0, 0, 0)); HighGui.imshow("rotation", src); return src; }Copy the code

3. Application scenarios of the two algorithms

The correction algorithm based on contour extraction is more suitable for vehicle license plate, ID card, RMB, book, invoice and other rectangular shapes with obvious boundaries.

The correction algorithm based on line detection is more suitable for text correction.

reference

www.jianshu.com/p/9eb9d6f6f… www.cnblogs.com/skyfsm/p/69…