如何发现图像中的所有像素都是灰度级,或者每个像素的R,G,B值相等 [英] How to find that all pixels in an image are grey scale or has R,G,B equal value for each individual pixel
问题描述
请不要引用此如何查看位图的颜色深度?或如何检查如果图片是灰度
Please dont quote this How can I check the color depth of a Bitmap? or How to check if a picture is in grayScale
因为图像可以是每像素24/32位,即使所有唯一颜色都是灰度或小于256并且每个像素可以是24或32位。
Because an image can be 24/32 bit per pixel even if all unique colors are grey scale or less than 256 and each pixel can be 24 or 32 bit.
如何找到图像中的像素 Bitmap.GetPixel(x,y)
具有相等的R,G,B值,以便我可以查看图像中的所有像素是否位于灰度范围内。因为灰度像素的R,G,B值相同。或者有没有更好的方法来查找图像是否为灰度?
How do I find that the pixel in an image Bitmap.GetPixel(x, y)
has equal R,G,B vales so that I can find out if all pixels in an image lie in grey scale range. Because the R,G,B value of a grey scale pixel is same. Or is there any better way to find if the image is in grey scale?
我正在编写一个代码来压缩16/24/32位图像的大小如果图像具有256种独特颜色,则将其更改为8位图像并保存。
I am writing a code to compress the size of a 16/24/32 bit image such that if the image has 256 unique colors then change it to 8 bit image and save it.
首先,我计算每个像素高于8的图像中的唯一颜色。
First I count unique colors in an image that has higher than 8 its per pixel.
如果唯一图像中的颜色小于或等于256然后
If the unique colors in the image are less than or equal to 256 then
- 如果所有独特颜色都在灰度范围内,则将其转换为灰度
- If all the unique colors are in greyscale range then convert it to greyscale
- Otherwise if any color is not greyscale then convert image to 8 BPP
uint UniqueColors(Bitmap Bitmap)
{
try
{
List<int> lstColors = new List<int>();
if (null == Bitmap)
return 0;
for (int iCount = 0; iCount < Bitmap.Height; iCount++)
for (int iCounter = 0; iCounter < Bitmap.Width; iCounter++)
if (!lstColors.Contains(Bitmap.GetPixel(iCounter, iCount).ToArgb()))
lstColors.Add(Bitmap.GetPixel(iCounter, iCount).ToArgb());
Bitmap.Dispose();
return Convert.ToUInt32(lstColors.Count);
}
catch (Exception)
{
Bitmap.Dispose();
return 0;
}
}
然后:
if (256 >= UniqueColors(new Bitmap(string ImagePath)))
{
if (Is_Greyscale(new Bitmap(ImagePath))
Convert_To_Greyscale(ImagePath);
else
Convert_To_8Bits(ImagePath);
}
现在我被困住如何找到图像中的每种独特颜色是否存在于灰度范围内。我的意思是每种独特的颜色具有相等的(R,G,B)值。 = G = B.如何在我的代码行中找到它
now I am stuck how do I find if each unique color in the image lies in the greyscae rage. By which I mean that each unique color has equal (R, G, B) values. Like R=G=B. How can I find it in my code line
Bitmap.GetPixel(iCounter, iCount).ToArgb()
推荐答案
Bitmap.GetPixel()
返回 颜色
结构,其字段为 R
, G
和 B
所以你可以随意比较它们。
Bitmap.GetPixel()
returns a Color
structure which has fields R
, G
and B
so you can just compare them however you want.
Do n注意使用 GetPixel()
非常慢,但是如果你不需要速度,它就会这样做。
Do note that using GetPixel()
is very slow, but if you don't need speed it will do.
这篇关于如何发现图像中的所有像素都是灰度级,或者每个像素的R,G,B值相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!