获取PictureBox Image中特定颜色的所有阴影的像素 [英] Get pixels of all shades of particular color in PictureBox Image

查看:118
本文介绍了获取PictureBox Image中特定颜色的所有阴影的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,


我在图片框中有一个图像,并且想要获得该图像或图像中所有蓝色阴影的像素(或包含该图像的所有区域).例如:我有一个带有图像和两个按钮的图片框,一个用于蓝色,一个用于绿色,当我单击蓝色时,我希望该图片中只有蓝色阴影是可见的,而其余部分则不可见.与绿色按钮相同.


请朋友帮我.

预先感谢!

Dear friends,,


I have an image in a picture box, and I want to get Pixels(or all the area that contains) of all shades of blue color in that picture or image. For Example: I have a picture box with an image and two buttons, one for blue and on for green, I want when I click on blue then only blue shades in that picture should be visible and the rest is not visible. Same for the green button.


Please friends help me.

Thanks in advance!

推荐答案

我不确定在谈论颜色阴影时您的意思是否与我相同: HSV [
I''m not sure if you mean the same thing as I when talking of a shade of a color: See here please[^].
Could it be that you really want to calculate the hue part of HSV[^] and all colors that fall into a certain hue range are considered to be in a class?

Regards,

Manfred


我做了函数和枚举.为了方便起见,我创建了一个用于图层选择的枚举"WhichLayer"
I have made function and enum. For convinience I have created an enum for layer selection, "WhichLayer"
public enum WhichLayer
    {
        Red=0,
        Green=1,
        Blue=2
    }



然后,功能如下:



Then, the functions as follows:

private Bitmap GetSpecificLayer(Bitmap image, WhichLayer layer)
{
    Rectangle rect=new Rectangle(0, 0, image.Width, image.Height);
    BitmapData dataIn = image.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

    Bitmap result = new Bitmap(rect.Width,rect.Height,PixelFormat.Format8bppIndexed);
    // Keep the original resolotion
    result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    // Set Grayscale palette
    SetGrayscalePalette(result);
    BitmapData dataOut = result.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

    int strideIn = dataIn.Stride;
    int strideOut = dataOut.Stride;
    int lineOffsetIn = dataIn.Stride - rect.Width * 3;
    int lineOffsetOut = dataOut.Stride - rect.Width;

    // Get layer index
    int ind=0;
    switch(layer)
    {
        case WhichLayer.Blue: ind=0; break;
        case WhichLayer.Green:ind=1; break;
        case WhichLayer.Red: ind = 2;break;
    }
    unsafe
    {
        byte* pIn = (byte*)(void*)dataIn.Scan0;
        byte* pOut = (byte*)(void*)dataOut.Scan0;

        for (int y = 0; y < image.Height; y++)
        {
            for (int x = 0; x < image.Width; x++)
            {
                pOut[0] = pIn[ind];
                pIn += 3;       // skip RGB24=3; RGB32=4...
                pOut++;
            }
            pIn += lineOffsetIn;
            pOut += lineOffsetOut;
        }
    }
    image.UnlockBits(dataIn);
    result.UnlockBits(dataOut);

    return result;
}


private static void SetGrayscalePalette(Bitmap b)
{
    ColorPalette pal = b.Palette;
    for (int i = 0; i < 256; i++)
        pal.Entries[i] = Color.FromArgb(255, i, i, i);
    b.Palette = pal;
}



以上仅适用于RGB图像(24位).如果需要所有RGB彩色图像,则可以分别对每种格式进行处理.或者您可以使用第三方工具.您可以在这里找到一个:
http://www.artuxsoft. com [^ ]



The above is only for RGB images (24-bit). If you need for all RGB color images you may do separately for each format. Or you may use third party tools. You may find one here:http://www.artuxsoft.com[^]


这篇关于获取PictureBox Image中特定颜色的所有阴影的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆