【问题标题】:How do I find a specific color and ask the position, width and height of it如何找到特定颜色并询问它的位置、宽度和高度
【发布时间】:2018-09-14 14:23:31
【问题描述】:

大家好。

例如,我是否有可能在 Photoshop 中制作一张图片并在其中制作黑色表面,以便 Unity 可以找到该特定黑色表面并从中请求其位置、宽度和高度。

我想让我的应用程序自动化。我的意思是,如果人们制作一个带有一些黑色区域的 360 度穹顶,我可以在代码中说 Unity 必须找到这些黑色区域,然后必须在那里放一部电影。

谁能帮帮我?

【问题讨论】:

    标签: c# colors 360-degrees


    【解决方案1】:

    如果我理解正确,您想获取特定颜色的位置以及宽度和高度。我以前用 aspose 和模板做过,这是我的代码,希望对你有帮助

    public static void CheckRotation(string imageFilePath, string templateFilePath, int tryCount)
        {
          if (tryCount > 2)
            throw new Exception("the Image Cant be Detected");
    
          int leftTotal = 0;
          int leftBlackColor = 0;
    
          int rightTotal = 0;
          int rightBlackColor = 0;
    
          byte findLeftAndRight = 0;
    
          OmrTemplate template = OmrTemplate.Load(templateFilePath);
          OmrImage image = OmrImage.Load(imageFilePath);
          OmrEngine engine = new OmrEngine(template);
          image.AutoDetectResolution = true;
    
          OmrProcessingResult result = engine.ExtractData(new OmrImage[] { image });
          Hashtable OmrResult = result.PageData[0];
          ICollection key = OmrResult.Keys;
    
          OmrElementsCollection collection = template.Pages[0].Elements;
          foreach (Object obj in collection)
          {
            if (obj.GetType() == typeof(ChoiceBoxElement))
            {
              var elem = (ChoiceBoxElement)obj;
    
              if (elem.Name.ToString().ToUpper() == "LEFT" || elem.Name.ToString().ToUpper() == "RIGHT")
              {
                findLeftAndRight++;
    
                var xPosition = int.Parse(Math.Floor((elem.Position.X * 150) / 25.4).ToString()) + 1;
                var yPosition = int.Parse(Math.Floor((elem.Position.Y * 150) / 25.4).ToString()) + 1;
    
                Bitmap bitmap = new Bitmap(imageFilePath);
                BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(xPosition, yPosition, int.Parse(Math.Floor((elem.Size.Width * 150) / 25.4).ToString()), int.Parse(Math.Floor((elem.Size.Height * 150) / 25.4).ToString())),
                ImageLockMode.ReadOnly, bitmap.PixelFormat);  // make sure you check the pixel format as you will be looking directly at memory
    
                unsafe
                {
                  // example assumes 24bpp image.  You need to verify your pixel depth
                  // loop by row for better data locality
                  for (int y = 0; y < data.Height; ++y)
                  {
                    byte* pRow = (byte*)data.Scan0 + y * data.Stride;
                    for (int x = 0; x < data.Width; ++x)
                    {
                      // windows stores images in BGR pixel order
                      byte r = pRow[2];
                      byte g = pRow[1];
                      byte b = pRow[0];
    
                      if (elem.Name.ToString().ToUpper() == "LEFT")
                        leftTotal++;
                      else if (elem.Name.ToString().ToUpper() == "RIGHT")
                        rightTotal++;
    
                      if (
                          (r == 0 && g == 0 && b == 0) ||
                          ((r < 100 && g < 100) || (r < 100 && b < 100) || (b < 100 && g < 100)) ||
                          (r < 120 && g < 120 && b < 120 && Math.Abs(r - g) < 10 && Math.Abs(r - b) < 10 && Math.Abs(b - g) < 10)
                        )
                      {
                        if (elem.Name.ToString().ToUpper() == "LEFT")
                          leftBlackColor++;
                        else if (elem.Name.ToString().ToUpper() == "RIGHT")
                          rightBlackColor++;
                      }
    
    
                      // next pixel in the row
                      pRow += 3;
                    }
                  }
                }
    
                bitmap.UnlockBits(data);
              }
            }
          }
    
          if (findLeftAndRight != 2)
            throw new Exception("The Left and Right Mark Didnt Exists!!");
    
          var leftPercentage = (leftBlackColor / leftTotal) * 100;
          var rightPercentage = (rightBlackColor / rightTotal) * 100;
    
          if (leftPercentage > 80 && rightPercentage > 80) //It means The Paper is Currect Rotation
            return;
          else
          {
            RotateImage(imageFilePath, 180);
            CheckRotation(imageFilePath, templateFilePath, ++tryCount);
          }
    
    
        }
    

    【讨论】:

    • 你有我的变量吗?我现在收到大量错误
    • 你有aspose dll吗?首先你必须得到这些dll。假设站点:aspose.com/…
    • 你能给我一个具体的链接,我可以直接下载吗?
    • 你能分步告诉我我必须做什么。我是新手,抱歉=D
    猜你喜欢
    • 2011-09-09
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多