Home > database >  OpenCv rotate text image
OpenCv rotate text image

Time:12-10

help me rotate text image, i want the text to be smooth all the examples that are written in python. My code.

   var src = Cv2.ImRead("NzGFw.png");
        var gray = new Mat();
        Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
        var output = new Mat();
        Cv2.BitwiseNot(gray, output);
        Mat points = Mat.Zeros(output.Size(), output.Type());
        Cv2.FindNonZero(output, points);
        var box = Cv2.MinAreaRect(points);
        Mat squares = src.Clone();
        Mat rot = Cv2.GetRotationMatrix2D(box.Center,  box.Angle, 1);
        Mat rotated = new Mat();
        Cv2.WarpAffine(squares, rotated, rot, squares.Size(), InterpolationFlags.Cubic);
        Cv2.ImWrite("inclined_text_squares_rotated.jpg", rotated);[![enter image description here][1]][1]

enter image description here

enter image description here

CodePudding user response:

    public static float CalcAngle(Mat image)
    {
        double angle = 0;
        var img = new Mat();
        var size = image.Size();
        Cv2.CvtColor(image, img, ColorConversionCodes.BGR2GRAY);
        Cv2.BitwiseNot(img, img);
        var lines = Cv2.HoughLinesP(img, 1, Math.PI / 180, 100, size.Width / 2f, 10);

        foreach (var line in lines)
            img.Line(line.P1, line.P2, Scalar.White, 1, LineTypes.AntiAlias, 0);

        for (var i = 0; i < lines.Length; i  )
            angle  = Math.Atan2(lines[i].P2.Y - lines[i].P1.Y, lines[i].P2.X - lines[i].P1.X);

        angle = angle / lines.Length;
        return (float)((angle * 180) / Math.PI);
    }
  • Related