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

时间:2016-05-23 13:03:43

标签: c# opencv image-processing video-processing opencvsharp

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

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;
    }

1 个答案:

答案 0 :(得分:1)

我没有评论所需的声誉,所以这必须以答案的形式出现。在完成所有处理以查找文本之后,看起来pathImage没有获得任何值 - 它保留了imgPath的值,因此您只需返回相同的值。