2017-09-25 9 views
-1

私は3-4のバーコードを含むいくつかの画像を持っています。私は位置に関係なくすべてのバーコードをマークしたいと思います。私は下のコードを使用してイメージ内のすべての矩形を取得しようとしているが、空に戻るか、バーコードをマークしないでください。何か不足していますか?どんな指針も大変ありがとうございます。画像Cのバーコードの座標を見つける#

I also tried to follow this tutorial EmguCVに移植しようとしましたが、特定の機能のパラメータが不足しているために何を渡すべきかわかりませんでした。コメントされた部分は、わからない部分です。方向を修正するように私を導いてください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Emgu.CV; 
using Emgu.CV.Structure; 
using System.IO; 
using static System.Net.Mime.MediaTypeNames; 
using Emgu.CV.CvEnum; 
using Emgu.CV.Util; 
using System.Windows.Forms; 

namespace ConsoleApplication4 
{ 
    class Program 
    { 

     //public static Mat mat = new Mat(); 
     // public static Mat kernel = new Mat(); 

     // private static Image<Bgr, byte> gradX = mat.ToImage<Bgr,byte>(); 
     // private static Image<Bgr, byte> gradY = mat.ToImage<Bgr, byte>(); 
     // private static Image<Bgr, byte> gradient = mat.ToImage<Bgr, byte>(); 
     // private static Image<Bgr, byte> blur = mat.ToImage<Bgr, byte>(); 
     // private static Image<Bgr, byte> thresh = mat.ToImage<Bgr, byte>(); 
     // private static Image<Bgr, byte> closed = mat.ToImage<Bgr, byte>(); 


     static void Main(string[] args) 
     { 

        Image<Bgr, byte> gambar = new Image<Bgr, byte>("source.jpg"); 
        Image<Bgr, byte> kotak = detectBarcode(gambar); 
        kotak.ToBitmap().Save("destination.jpg"); 

      Console.ReadKey(); 

     } 


     private static Image<Bgr, byte> detectBarcode(Image<Bgr, byte> image) 
     { 

      try 
      { 
       Image<Gray, byte> imageGrey = image.Convert<Gray, byte>(); 



       //CvInvoke.Sobel(imageGrey, gradX, DepthType.Cv32F, 1, 0, -1); 
       //CvInvoke.Sobel(imageGrey, gradY, DepthType.Cv32F, 0, 1, -1); 

       //CvInvoke.Subtract(gradX, gradY, gradient); 
       //CvInvoke.ConvertScaleAbs(gradient, gradient, 0, 0); 

       //CvInvoke.Blur(gradient, blur, new System.Drawing.Size(new System.Drawing.Point(9, 9)), new System.Drawing.Point(9, 9)); 
       //CvInvoke.Threshold(blur, thresh, 255, 255, ThresholdType.Binary); 

       //kernel = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new System.Drawing.Size(new System.Drawing.Point(9, 9)), new System.Drawing.Point(9, 9)); 

       //CvInvoke.MorphologyEx(thresh,closed,MorphOp.Close,kernel,); 

       //CvInvoke.Erode(closed,closed, new System.Drawing.Point(0, 0),4,BorderType.Default,); 
       //CvInvoke.Dilate(closed, closed, new System.Drawing.Point(0, 0), 4, BorderType.Default,); 

       List<RotatedRect> boxList = new List<RotatedRect>(); 

       UMat cannyEdges = new UMat(); 
       double cannyThreshold = 180.0; 
       double cannyThresholdLinking = 120.0; 
       CvInvoke.Canny(imageGrey, cannyEdges, cannyThreshold, cannyThresholdLinking); 


       using (VectorOfVectorOfPoint countours = new VectorOfVectorOfPoint()) 
       { 

        CvInvoke.FindContours(cannyEdges, countours, null, RetrType.List, 
        ChainApproxMethod.ChainApproxSimple); 
        int count = countours.Size; 
        for (int i = 0; i < count; i++) 
        { 
         using (VectorOfPoint kontur = countours[i]) 
         using (VectorOfPoint approxContour = new VectorOfPoint()) 
         { 
          CvInvoke.ApproxPolyDP(kontur, approxContour, CvInvoke.ArcLength(kontur, true) * 0.05, true); 
          if (CvInvoke.ContourArea(approxContour, false) > 250) //only consider contours with area greater than 250 
          { 
           if (approxContour.Size == 4) //rectangle 
           { 
            //determine if allthe angles in the contour are within [80,100] degree 
            bool isRectangle = true; 
            System.Drawing.Point[] pts = approxContour.ToArray(); 
            LineSegment2D[] edges = Emgu.CV.PointCollection.PolyLine(pts, true); 

            for (int j = 0; j < edges.Length; j++) 
            { 
             double angle = Math.Abs(
             edges[(j + i) % edges.Length].GetExteriorAngleDegree(edges[j])); 
             if (angle < 80 || angle > 100) 
             { 
              isRectangle = false; 
              break; 
             } 

            } 
            if (isRectangle) boxList.Add(CvInvoke.MinAreaRect(approxContour)); 
           } 
          } 
         } 
        } 
       } 

       Image<Bgr, byte> triRectImage = image.Copy(); 

       foreach (RotatedRect box in boxList) 
        triRectImage.Draw(box, new Bgr(0, 0, 0), 5); 
       return triRectImage; 

      } 
      catch (Exception e) { 

       Console.WriteLine(e.StackTrace); 

       return null; 
      } 



     } 
} 
} 
+0

コメントアウトしたすべてのコードがこの質問に役立つものを追加してくれると本当に思いますか? [mcve]の「最小」は理由があります。 –

答えて

0

I find myself referring you to, for example

パブリック静的ボイドソーベル(IInputArray SRC、IOutputArray DST、 DepthType ddepth、INT xorder、INT yorder、INT kSize = 3、ダブルスケール= 1、ダブルデルタ= 0、 BorderType borderType = BorderType.Reflect101)

パラメータの詳細なリストとその意味を以下に示します。あなたが実際にこのことを理解していない場合は、need to read the tutorialsを徹底的に提案します。そうしないと、Emgu CVのエキスパートがあなたのプログラムを書く方法を伝える必要があります。

私は気難しくはありませんが、少なくともあなたがしようとしているところに刺す必要があります。

+0

ありがとうKevin。 – Sri

関連する問題