2016-05-23 37 views
0

我編寫了一些用於從相機中查找文本的代碼。實際上,我首先編寫了用於在圖像中查找文本的代碼,並且我成功了,但無法從相機中找到文本。 預先感謝您。 另外我在classlibrary中寫了這段代碼,所以我不能很好地測試它。在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; 
    } 

回答

1

我沒有評論所需的聲望,所以這將不得不以回答的形式。它看起來像你做所有的處理找到文本之後pathImage沒有得到任何價值 - 它讓imgPath的價值,所以你只返回你把相同的值

+0

感謝您的評論Pathımage得到一個值,但我竟忘了在這裏除了添加這些代碼,當我把我的調試代碼會全部處理:(如果你想我可以在這裏 – stackopen

+0

添加這些代碼也許看看下面的鏈接之前添加更多的代碼在這裏: HTTP:/ /stackoverflow.com/questions/34366083/i-need-recognize-text-in-image-using-opencvsharp –

+1

太感謝你了,但我讀了我們的問題,代碼是不同的,但我會讀一遍 – stackopen