2017-09-25 4 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