首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何找到一个特定的颜色,并询问它的位置,宽度和高度。

我如何找到一个特定的颜色,并询问它的位置,宽度和高度。
EN

Stack Overflow用户
提问于 2018-09-14 14:23:31
回答 1查看 49关注 0票数 0

嘿伙计们.

例如,我是否有可能在Photoshop中拥有一幅图片,并在其中制作了一个黑色的表面,以便Unity能够找到该特定的黑色表面,并要求它的位置、宽度和高度。

我想让我的应用程序自动。我的意思是,如果人们造了一个360天穹顶,里面有几个黑色区域,我可以用代码说,统一必须找到那些黑色区域,然后在那里放一部电影。

有谁能帮帮我呢?

EN

回答 1

Stack Overflow用户

发布于 2018-09-14 14:40:28

如果我理解得对,你想得到特定颜色、宽度和高度的位置。我以前做过这件事,这是我的代码,希望能帮到你。

代码语言:javascript
复制
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);
      }


    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52333947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档