我是在c#中使用圖像的新手。如何識別Jpeg圖像是雙色調
我做到了這一點:
private bool IsBitonal(string FilePath)
{
Bitmap bitmap = new Bitmap(FilePath);
return (bitmap.PixelFormat == PixelFormat.Format1bppIndexed)
}
這與.png文件的工作,但不能與JPEG格式文件的工作任何一個可以幫助我嗎?
有沒有解決方案找到圖像是否是黑色?
我用Loding圖形的東西,它也不適合我。
private bool IsBitonal(string filePath)
{
bool isBitonal = false;
try
{
Bitmap bitmap = new Bitmap(filePath);
Graphics graphics = Graphics.FromImage(bitmap);
}
catch (Exception ex)
{
isBitonal = true;
}
return isBitonal;
}
是啊,我得到了解決辦法從Tomaz回答
這是我在C#中的答案:
public bool IsBitonal(Bitmap YourCurrentBitmap)
{
Color c;
long Eadges = 0;
long Others = 0;
for (int i = 0; i < YourCurrentBitmap.Width; i++)
{
for (int j = 0; j < YourCurrentBitmap.Height; j++)
{
c = YourCurrentBitmap.GetPixel(i, j);
if (!(c.R == c.G && c.G == c.B)) return false;
if (c.R <= 16||c.R >= 255-16)
Eadges++;
else
Others++;
}
}
double proportion = Eadges/(double)Others;
// here is estimation based on you requirement you can change
return proportion > 10;;
}
你能告訴我如何做估計。我不知道直方圖和如何找到分佈的邊緣值 – Civa
@Tomasz使用直方圖確實是一個很好的解決方法。請注意,GetPixels()和Scan0.ToPointer()之間的中間解決方案不依賴不安全的代碼,同時保留良好的性能:http://stackoverflow.com/a/14834760/1314667 –
謝謝:) 。我做同樣的事情。但是這個對我來說是充分利用「回報比例」> 10;「區分灰度和黑白。 – Civa