简体   繁体   English

从图像中的每个像素获取色相

[英]Getting Hue from every pixel in an Image

I am trying to get the Hue or 'H' of every pixel of an image, then changing the colour of the pixel that has a specific Hue. 我试图获取图像每个像素的色相或“ H”,然后更改具有特定色相的像素的颜色。 I am trying to change all pixels that have a hue >= 210 and <=260, which is the different hues of blue. 我正在尝试更改所有色相> = 210和<= 260的像素,这是蓝色的不同色相。

Here is my code: 这是我的代码:

// 'i' is the image
// 'b' is the bitmap of the image
float y;
for (int a = 0; a < i.Height; a++) 
{ 
    for (int c = 0; c < i.Width; c++) 
    { 
        y = b.GetPixel(c, a).GetHue(); 
        if (y >= 210 && y <= 260) 
            { 
                b.SetPixel(c, a, Color.Black); 
            } 
    } 
}

The only problem is that it doesn't pickup any blue colours, which are from 210 - 260. I am pretty sure that I am doing this correctly, but I must not be since it isnt working. 唯一的问题是它不会拾取210-260之间的任何蓝色。我敢肯定我做得正确,但是由于它不起作用,所以我绝对不能这样做。

Please post here if you can solve this problem, Thanks! 如果可以解决此问题,请在这里发布,谢谢!

Edit: I put a breakpoint on SetPixel, and it gets called many, many times so now I will check if I am saving the picture right. 编辑:我在SetPixel上放置一个断点,它被调用了很多很多次,所以现在我将检查是否正确保存了图片。

Edit 2: I figured it out! 编辑2:我想通了! I wasn't saving the picture. 我没有保存图片。

Use, LockBits method, Luke! 使用LockBits方法,卢克!

Or, i think, it`s better be done with Graphics context. 或者,我认为,最好使用Graphics上下文来完成。

It kind of worked for me. 这为我工作。 In the code below I have a Windows Form with PictureBox called imgViwer. 在下面的代码中,我有一个带有PictureBox的Windows窗体,名为imgViwer。 Then clicking the a button I execute the code: 然后单击a按钮,我执行代码:

private void HueFilter()
{
  float y;
  Bitmap i = (Bitmap)imgViwer.Image;

  for (int a = 0; a < i.Height; a++)
  {
      for (int c = 0; c < i.Width; c++)
      {                   
          y = i.GetPixel(c, a).GetHue();
          if (y >= 210 && y <= 260)
          {
              i.SetPixel(c, a, Color.Black);
          }
      }
  }

  imgViwer.Image = i;
}

In your case you have an output image called b that should be assigned back to the PictureBox to the refresh. 在您的情况下,您有一个称为b的输出图像,应将其分配回PictureBox进行刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM