在 opencv (c#) 中从相机中提取文本

Extracting Text from Camera in opencv (c#)

我写了一些代码来从相机中查找文本。实际上,我首先编写了用于在图像中查找文本的代码,我成功了,但我无法从相机中找到文本。 先感谢您。 另外我在类库中写了这段代码所以我不能很好地测试它。

public string GetImageText(string imgPath)
    {


        pathImage = imgPath;

        //CvCapture cap = new CvCapture(0);
       CvCapture cap;
       //CvCapture cap = new CvCapture(imgPath);
       cap = CvCapture.FromFile(imgPath);

       cap= CvCapture.FromCamera(CaptureDevice.Any);

      //  img = new IplImage(imgPath, LoadMode.Color);
      // cap =CvCapture.FromFile(imgPath);

      // IplImage frame = new IplImage();
       // imgPath=cap.QueryFrame(frame);
        frame = cap.QueryFrame();
       // IplImage frame = new IplImage();
       // BitmapConverter.ToBitmap(frame);
      //frame = new IplImage(imgPath, LoadMode.Color);


        if (frame != null)
        {
            IplImage img1 = new IplImage(frame.Size, BitDepth.U8, 1);
            IplConvKernel element = Cv.CreateStructuringElementEx(21, 3, 10, 2, ElementShape.Rect, null);
            aimg = new IplImage(frame.Size, BitDepth.U8, 1);
            IplImage temp = aimg.Clone();
            IplImage dest = aimg.Clone();
            frame.CvtColor(aimg, ColorConversion.RgbaToGray);
            bimg = aimg.Clone();
            Cv.Smooth(aimg, aimg, SmoothType.Gaussian);
            Cv.MorphologyEx(aimg, temp, dest, element, MorphologyOperation.TopHat, 1);

            Cv.Threshold(dest, aimg, 128, 255, ThresholdType.Binary | ThresholdType.Otsu);
            Cv.Smooth(aimg, dest, SmoothType.Median);
            Cv.Dilate(dest, dest, element, 2);

            Cv.ReleaseImage(temp);
            Cv.ReleaseImage(dest);
            IplImage labelImage = new IplImage(frame.Size, CvBlobLib.DepthLabel, 1);
            labelImage = new IplImage(frame.Size, BitDepth.U8, 1);
            frame = new IplImage(frame.Size, BitDepth.U8, 1);

            blob = new CvBlobs();
            text.Clear();
            CvBlobLib.Label(labelImage, blob);

            CvBlobLib.FilterByArea(blob, 6, 10);
            IplImage imgtemp = frame.Clone();
            foreach (var item in blob)
            {

                item.Value.SetImageRoiToBlob(bimg);

                double ratio = (double)item.Value.Rect.Width / item.Value.Rect.Height;
                double angle = (double)item.Value.Angle();
                if (ratio > 3.5 && ratio < 5.4 && angle > -15 && angle < 15)

                {

                    IplImage texttemp = new IplImage(new CvSize(140, 27), bimg.Depth, bimg.NChannels);

                 // texttemp.Flip( null , FlipMode.X );


                    Cv.Resize(bimg, texttemp);

                    text.Add(texttemp);
                    frame.Rectangle(item.Value.Rect, new CvScalar(0, 0, 255), 2, LineType.Link4);
                 //   frame.Flip(null, FlipMode.X);
              //   frame.Rectangle(item.Value.Rect, new CvScalar(0, 0, 255), 2, LineType.Link4)=RotateFlipType.Rotate180FlipNone()
                   // RotateFlipType.Rotate180FlipX(frame.Rectangle(item.Value.Rect, new CvScalar(0, 0, 255), 2, LineType.Link4));
                  // flipImage=frame.Rectangle(item.Value.Rect, new CvScalar(0, 0, 255), 2, LineType.Link4);
                }
            }

            textList.Clear();

        }
        return pathImage;
    }

我没有发表评论所需的声誉,所以这必须以答案的形式出现。在您完成所有处理以查找文本后,pathImage 似乎没有获得任何值 - 它保留了 imgPath 的值,因此您只需 return 与输入的值相同。