Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv 检测并修复文本倾斜_Opencv_Image Processing - Fatal编程技术网

Opencv 检测并修复文本倾斜

Opencv 检测并修复文本倾斜,opencv,image-processing,Opencv,Image Processing,有没有一种方法(使用类似OpenCV的东西)来检测文本倾斜并通过旋转图像来纠正它?很像这样 如果你知道角度,旋转一个图像似乎很容易,但是对于我正在处理的图像,我不会…它需要被检测到 根据您的上述评论,以下是基于教程的代码,适用于上述图像 来源 旋转 Mat src=imread("text.png",0); Mat thr,dst; threshold(src,thr,200,255,THRESH_BINARY_INV); imshow("thr",thr); std::ve

有没有一种方法(使用类似OpenCV的东西)来检测文本倾斜并通过旋转图像来纠正它?很像这样


如果你知道角度,旋转一个图像似乎很容易,但是对于我正在处理的图像,我不会…它需要被检测到

根据您的上述评论,以下是基于教程的代码,适用于上述图像

来源

旋转

 Mat src=imread("text.png",0);
 Mat thr,dst;
 threshold(src,thr,200,255,THRESH_BINARY_INV);
 imshow("thr",thr);

  std::vector<cv::Point> points;
  cv::Mat_<uchar>::iterator it = thr.begin<uchar>();
  cv::Mat_<uchar>::iterator end = thr.end<uchar>();
  for (; it != end; ++it)
    if (*it)
      points.push_back(it.pos());

  cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
  cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, box.angle, 1);

  //cv::Mat rotated(src.size(),src.type(),Scalar(255,255,255));
  Mat rotated;
  cv::warpAffine(src, rotated, rot_mat, src.size(), cv::INTER_CUBIC);
 imshow("rotated",rotated);

Mat src=imread(“text.png”,0);
Mat-thr,dst;
阈值(src,thr,200255,阈值二进制变量);
imshow(“thr”,thr);
std::向量点;
cv::Mat_u2;::迭代器it=thr.begin();
cv::Mat_u3;::迭代器end=thr.end();
for(;it!=end;++it)
如果(*it)
点。向后推(it.pos());
cv::RotatedRect box=cv::MinareRect(cv::Mat(点));
cv::Mat rot_Mat=cv::getRotationMatrix2D(box.center,box.angle,1);
//cv::Mat旋转(src.size()、src.type()、Scalar(255255));
垫旋转;
cv::warpAffine(src,rotated,rot_mat,src.size(),cv::INTER_CUBIC);
imshow(“旋转”,旋转);

编辑:


另请参阅答案,可能会有所帮助。

我将提供javacv供您参考

package com.test13;

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

public class EdgeDetection {

    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main( String[] args ) throws Exception{      
        Mat src = Imgcodecs.imread("src//data//inclined_text.jpg");
        Mat src_gray = new Mat();
        Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
        Imgcodecs.imwrite("src//data//inclined_text_src_gray.jpg", src_gray);

        Mat output = new Mat();
        Core.bitwise_not(src_gray, output);
        Imgcodecs.imwrite("src//data//inclined_text_output.jpg", output);

        Mat points = Mat.zeros(output.size(),output.type());  
        Core.findNonZero(output, points);   

        MatOfPoint mpoints = new MatOfPoint(points);    
        MatOfPoint2f points2f = new MatOfPoint2f(mpoints.toArray());
        RotatedRect box = Imgproc.minAreaRect(points2f);

        Mat src_squares = src.clone();
        Mat rot_mat = Imgproc.getRotationMatrix2D(box.center, box.angle, 1);
        Mat rotated = new Mat(); 
        Imgproc.warpAffine(src_squares, rotated, rot_mat, src_squares.size(), Imgproc.INTER_CUBIC);
        Imgcodecs.imwrite("src//data//inclined_text_squares_rotated.jpg",rotated);    
    }
}

下面是用于确定倾斜的投影配置文件方法的Python实现。在获得二值图像后,想法是以不同角度旋转图像,并在每次迭代中生成像素直方图。为了确定倾斜角度,我们比较峰值之间的最大差异,并使用此倾斜角度旋转图像以校正倾斜


输入

结果

检测到的倾斜角度:-5


@哈里斯,这不是更多的文字行吗?我不觉得它会足够健壮来处理这个问题,它会像你上面提供的那样对图像起作用,就像,找到所有白色像素->找到定位点的旋转矩形->旋转等等…看到我为上面的图像得到的结果@Haris我试过,并在之后将其与帖子结合起来,但运气不太好这不起作用,对于某些图像,它会将它们旋转90度。
def define_score(arr,angle)
被调用
import cv2
import numpy as np
from scipy.ndimage import interpolation as inter

def correct_skew(image, delta=1, limit=5):
    def determine_score(arr, angle):
        data = inter.rotate(arr, angle, reshape=False, order=0)
        histogram = np.sum(data, axis=1)
        score = np.sum((histogram[1:] - histogram[:-1]) ** 2)
        return histogram, score

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] 

    scores = []
    angles = np.arange(-limit, limit + delta, delta)
    for angle in angles:
        histogram, score = determine_score(thresh, angle)
        scores.append(score)

    best_angle = angles[scores.index(max(scores))]

    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
    rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, \
              borderMode=cv2.BORDER_REPLICATE)

    return best_angle, rotated

if __name__ == '__main__':
    image = cv2.imread('1.png')
    angle, rotated = correct_skew(image)
    print(angle)
    cv2.imshow('rotated', rotated)
    cv2.imwrite('rotated.png', rotated)
    cv2.waitKey()