在照片上的像素搜索者

时间:2014-01-15 12:20:33

标签: c#

我有一些照片,我想做一些有趣的事情。

例如,我有一个混合了很多颜色的pohto,我希望能够将照片解剖成像素并检查每个像素的颜色,这样我就可以记录确切的颜色在照片中使用最多。

是否有可以使用的内置库,它能够剖析这样的照片并遍历每个像素?

2 个答案:

答案 0 :(得分:1)

您可以使用Image.FromFile加载图片。如果它们很小,您可以在循环中调用Bitmap.GetPixel。如果它们很大,超过几百万像素,我猜它会很慢,那么你应该使用Bitmap.LockBits代替。所有链接都有样品。您可以使用Dictionary集合来构建直方图,只需要小心,因为类似的像素将具有略微不同的R,G和B值,因此您的集合可能会变得非常庞大。

答案 1 :(得分:0)

将图像加载到BitMap中,然后使用返回Color对象的GetPixel method处理位图

  private void GetPixel_Example(PaintEventArgs e)
    {

    // Create a Bitmap object from an image file.
    Bitmap myBitmap = new Bitmap("Grapes.jpg");
    int width = myBitmap.Width;
    int height = myBitmap.Height;


    // Get the color of a pixel within myBitmap.

    for (int widthIndex = 0;widthIndex < width;widthIndex++) 
    {
        for (int heightIndex = 0;heightIndex < height;heightIndex ++) 
        {
              Color pixelColor = myBitmap.GetPixel(widthIndex, heightIndex);
              // add to dictionary
        }
    }
    // Fill a rectangle with pixelColor.
    SolidBrush pixelBrush = new SolidBrush(pixelColor);
    e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
    }

您可以使用字典存储颜色和计数