【问题标题】:How to find number of pixels from specific color in an image?如何从图像中的特定颜色中找到像素数?
【发布时间】:2011-09-19 10:56:01
【问题描述】:

所以我正在尝试制作一个程序,可以找到它有多少特定颜色的像素。这些图像是用相机拍摄的照片,之后它们上的某些区域已在 Photoshop 上标记,我需要找到这些像素的确切数量。但我有几个问题。 我正在使用 getPixel(x,y) 但我正在与我想要的 Color.FromArgb(red, green, blue) 进行比较,但是......我的第一个问题是颜色略有不同,例如我想要找出颜色 RGB 116,110,40 但是当你在 Photoshop 上用这种颜色绘制时,一些像素会得到一点不同的颜色,比如 RGB 115,108,38(和其他类似的颜色),我也想包括这个。所以我终于想出了这段代码(但似乎 id 现在可以正常工作了):

public Form1()
    {
        InitializeComponent();
    }

    Bitmap image1;
    int count=0;
    int red, green, blue;
    int redt, greent, bluet;
    double reshenie;

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            red = int.Parse(textBox1.Text);
            green = int.Parse(textBox2.Text);
            blue = int.Parse(textBox3.Text);

                           // Retrieve the image.
            image1 = new Bitmap(@"C:\bg-img.jpg", true);
             double widht, height, pixel ;
            int x, y;
            MessageBox.Show(pixel.ToString());

            // Loop through the images pixels            
            for (x = 0; x < image1.Width; x++)
            {
                for (y = 0; y < image1.Height; y++)
                {
                    Color pixelColor = image1.GetPixel(x, y);
                    redt = pixelColor.R;
                    greent = pixelColor.G;
                    bluet = pixelColor.B;


                    if ((red+10>=redt) && (red-10>=redt))//i used +-10 in attempt to resolve the problem that i have writed about the close colours
                    {

                        if ((green + 10 >= greent) && (green - 10 >= greent))
                        {
                            if ((blue + 10 >= bluet) && (blue - 10 >= bluet))
                            {
                                count += 1;

                            }
                        }
                    }
                }
            }

            pictureBox1.Image = image1;

            MessageBox.Show("Imashe " + count.ToString());
            count = 0;

        }
        catch (ArgumentException)
        {
            MessageBox.Show("There was an error." +
                "Check the path to the image file.");

        }

    }

问题是我没有得到我期望的结果。例如,当我必须得到 1000 像素时,我得到的或多或少,我找不到我的错误在哪里。因此,如果有人可以让我知道我做错了什么。感谢您提前提供的所有帮助。

【问题讨论】:

  • 我在我的博客上发布了一个示例,它将返回指定颜色图像中每个像素的位置。 jarloo.com/find-all-pixels

标签: c# colors


【解决方案1】:

试试这个循环:

int epsilon = 10;

for (x = 0; x < image1.Width; ++x)
{
    for (y = 0; y < image1.Height; ++y)
    {
        Color pixelColor = image1.GetPixel(x, y);
        redt = pixelColor.R;
        greent = pixelColor.G;
        bluet = pixelColor.B;

        if (Math.Abs(redt   - red)   <= epsilon &&
            Math.Abs(greent - green) <= epsilon &&
            Math.Abs(bluet  - blue)  <= epsilon)
        {
            ++ count;
        }
    }
}

其中epsilon 是每个通道的像素颜色和目标颜色之间的最大差异。

【讨论】:

    【解决方案2】:

    来自您的代码:

    if ((green + 10 >= greent) && (green - 10 >= greent))
    

    如果(a - 10 &gt;= b),那么肯定是(a + 10 &gt;= b)。看看你能不能理解为什么。

    我想你可能是想说

    if ((green - 10 <= greent) && (greent <= green + 10))
    

    像这样对条件进行排序有助于提高可读性,因为greent 必须 green - 10green + 10 之间,并且物理上也位于这些表达式之间.

    【讨论】:

    • 仅作记录:我重新阅读了您的回复和您的陈述如果 (a + 10 >= b),那么肯定是 (a - 10 >= b)。看看你能不能理解为什么。 错了。示例:a = 0, b = 5: 0 + 10 is &gt;= 5, 0 - 10 is not &gt;= 5 我想你的意思是:如果(a - 10 &gt;= b) 那么肯定是(a + 10 &gt;= b)
    【解决方案3】:

    我认为你的颜色比较不对。您将&lt;=&gt;= 混合在一起,试图在颜色范围内。试试这个:

    if ((red+10 >= redt) && (red-10 <= redt)) //i used +-10 in attempt to resolve the problem that i have writed about the close colours
    {
        if ((green + 10 >= greent) && (green - 10 <= greent))
        {
            if ((blue + 10 >= bluet) && (blue - 10 <= bluet))
            {
               count += 1;
            }
        }
    }
    

    【讨论】:

    • 非常感谢你们的帮助!!!我开始觉得我快疯了,哈哈……它昨天还在工作,现在经过几次更改它就停止了……再次感谢您的帮助!!!
    • 太好了,我们可以提供帮助。请记住选择一个答案并将其作为您接受的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多