c# - 我需要使用 opencvsharp 识别图像中的文本

标签 c# c++ opencv

我试图识别图像中的文本。

实际上,我正在尝试识别图像中的文本位置,然后将其转换为文本。

我发现了一些用 C++ 编写的代码,我正在尝试将其转换为 C#。 你能帮帮我吗?

Extracting text OpenCV

std::vector<cv::Rect> detectLetters(cv::Mat img)
{       std::vector<cv::Rect> boundRect;[enter image description here][1]
    cv::Mat img_gray, img_sobel, img_threshold, element;
    cvtColor(img, img_gray, CV_BGR2GRAY);
    cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
    cv::threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
    element = getStructuringElement(cv::MORPH_RECT, cv::Size(10, 15) );
    cv::morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
    std::vector< std::vector< cv::Point> > contours;
    cv::findContours(img_threshold, contours, 0, 1); 
    std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
    for( int i = 0; i < contours.size(); i++ )
        if (contours[i].size()>80)
        { 
            cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 17, true );
            cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
            if (appRect.width>appRect.height) 
                boundRect.push_back(appRect);
        }
        return boundRect;
}

我尝试将其转换为 C#,但没有成功

private List<Rectangle> detectLetters(IntPtr img)
{
    //cvtColor(img, img_gray, CV_BGR2GRAY);
    List<Rectangle> boundRect = new List<Rectangle>();

    //cv::Mat img_gray, img_sobel, img_threshold, element;
    IntPtr 
        img_gray = IntPtr.Zero, 
        img_sobel= IntPtr.Zero, 
        img_threshold= IntPtr.Zero,
        img_tmp = IntPtr.Zero,
        element= IntPtr.Zero;

    //cvtColor(img, img_gray, CV_BGR2GRAY);
    CvInvoke.cvCvtColor(img, img_gray,COLOR_CONVERSION.CV_BGR2GRAY); //CV_BGR2GRAY);

    //cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
    CvInvoke.cvSobel(img_gray, img_sobel, 0, 1, 1);//, 3, 1, 0, cv.BORDER_DEFAULT);

    //cv.threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
    CvInvoke.cvThreshold(img_sobel, img_threshold, 0, 255, THRESH.CV_THRESH_BINARY|THRESH.CV_THRESH_OTSU);

    //element = getStructuringElement(cv.MORPH_RECT, cv.Size(10, 15));
    element = CvInvoke.cvCreateStructuringElementEx(1,1,10,15,CV_ELEMENT_SHAPE.CV_SHAPE_RECT,element);// GetStructuringElement(

    //cv.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
    CvInvoke.cvMorphologyEx(img_threshold,img_threshold,img_tmp,element,CV_MORPH_OP.CV_MOP_CLOSE,1);

    //List<List<cv.Point>> contours = new List<List<cv.Point>>();
    var contours = new List<IntPtr>();

    //cv.findContours(img_threshold, contours, 0, 1);
    CvInvoke.cvFindContours(img_threshold, element,ref ((IntPtr)contours[0]), 1, RETR_TYPE.CV_RETR_EXTERNAL, CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,new Point(0,0));


    //std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
    var contours_poly = new List<List<Point>>(contours.Count);

    //for( int i = 0; i < contours.size(); i++ )
    for (int i = 0; i < contours.Count; i++)
    {
        //if (contours[i].size()>80)
        if (contours[i].ToInt32() > 80)
        {
            //cv.approxPolyDP(Emgu.CV.Matrix<>(contours[i]), contours_poly[i], 17, true);
            CvInvoke.cvApproxPoly(contours[i], 17,contours_poly[i],APPROX_POLY_TYPE.CV_POLY_APPROX_DP, 1,1);
            //cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
            Rectangle appRect = new Rectangle(CvInvoke.cvBoundingRect(contours_poly[i],false));
            //if (appRect.width>appRect.height) 
            if (appRect.width > appRect.height)
            {
                //boundRect.push_back(appRect);
                boundRect.Add(appRect);
            }
        }
    }
    //return boundRect;
    return boundRect;
}

最佳答案

根据您要查找的文本大小,您可能需要尝试使用元素大小和 ApproxPolyDP 的变量,但这段代码非常接近原始代码,但使用的是 OpenCvSharp 行话。

static List<Rect> RunTextRecog(string inFile)
{
    List<Rect> boundRect = new List<Rect>();
    using (Mat img = new Mat(inFile))
    using (Mat img_gray = new Mat())
    using (Mat img_sobel = new Mat())
    using (Mat img_threshold = new Mat())
    {
        Cv2.CvtColor(img, img_gray, ColorConversionCodes.BGR2GRAY);
        Cv2.Sobel(img_gray, img_sobel, MatType.CV_8U, 1, 0, 3, 1, 0, BorderTypes.Default);
        Cv2.Threshold(img_sobel, img_threshold, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
        using (Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 15)))
        {
            Cv2.MorphologyEx(img_threshold, img_threshold, MorphTypes.Close, element);
            Point[][] edgesArray = img_threshold.Clone().FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxNone);
            foreach (Point[] edges in edgesArray)
            {
                Point[] normalizedEdges = Cv2.ApproxPolyDP(edges, 17, true);
                Rect appRect = Cv2.BoundingRect(normalizedEdges);
                boundRect.Add(appRect);
            }
        }
    }
    return boundRect;
}

关于c# - 我需要使用 opencvsharp 识别图像中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366083/

相关文章:

c++ - std::enable_if 有条件地编译一个成员函数

c++ - 使用 boost 将几何体切割成碎片

c++ - 旋转后字典序最小的字符串

c++ - 将 opencv 的 cvScalar 与 Ycbcr 值一起使用

Python OpenCV : Play UDP video streaming while sending keep alive messages

c# - 差异 b/n 对象、引用、指针

C# IIS 以编程方式创建站点缺少方法

c# - 第二次打开时如何将参数发送到已运行的应用程序?

c++ - 来自 OpenCV 的 VTK 相机姿势来自 solvePnP 的估计姿势

c# - 应用忙但没有响应 : how to check what it's doing?