2013-04-16 14 views
0

我是在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;; 
    } 

回答

2

JPEG圖像根據定義用顏色定義。即使是保存在JPEG中的雙色調圖像,由於壓縮假象,重讀也不會是雙色調。

你所能做的就是做出一些估計,例如,創建一個直方圖並手動檢查(有一些錯誤),如果值沿邊緣分佈或不。

編輯:

這應該是一個計算直方圖的快速方法。如果你更喜歡一個(很慢),但更簡單的方法,你可以改爲使用getPixel()爲圖片中的每個像素。

基於: http://aforge.googlecode.com/svn/tags/AForge-1.5.0/Sources/Imaging/ImageStatisticsHSL.cs

private int[] getHistogram(BitmapData imageData) 
    { 
     int width = imageData.Width; 
     int height = imageData.Height; 
     int offset = imageData.Stride - width * 3; 

     int[] l = new int[256]; 

     unsafe 
     { 
      byte * p = (byte *) imageData.Scan0.ToPointer(); 

      // for each line 
      for (int y = 0; y < height; y++) 
      { 
       // for each pixel 
       for (int x = 0; x < width; x++, p += 3) 
       { 
        int bright = p[RGB.R] + p[RGB.G] + p[RGB.B]; 
        bright /= 3; 

        l[bright]++; 
       } 
       p += offset; 
      } 
     } 

     return l; 
    } 

我不能在此刻測試,但它應該很容易讓你瞭解的想法。你總是可以像使用AForge.NJET一樣使用liblary來生成直方圖。

一旦你有了直方圖,你應該檢查是否所有的值都位於兩點附近。

int[] hist = getHistogram(imageData); 

hist[n]是多個具有亮度的像素的「n」個(在範圍從0到255)

對於理想的圖像中,所有像素將是白色或黑色,因此HIST [0]和hist [255]將被設置,並且所有其他值將爲零。 相反,所有值將分佈在0和255附近。計算其他像素的黑白像素比例。

int eadges = 0; // black, white or very close to black/white 
int others = 0; 

for(int i =0; i<hist.length; i++) 
{ 
    if(i < 16 || i > 255-16) eadges += hist[i]; 
    else others += hist[i];  
} 

double proportion = eadges/(double)others; 
return proportion > 10; 

16和10只是一個例子 - 您可以選擇其他數字以獲得不同的檢測精度。

如果您願意,您應該能夠輕鬆找到更多關於直方圖和其他計算方法的信息。首先查看直方圖的Google圖像結果。

+0

你能告訴我如何做估計。我不知道直方圖和如何找到分佈的邊緣值 – Civa

+2

@Tomasz使用直方圖確實是一個很好的解決方法。請注意,GetPixels()和Scan0.ToPointer()之間的中間解決方案不依賴不安全的代碼,同時保留良好的性能:http://stackoverflow.com/a/14834760/1314667 –

+0

謝謝:) 。我做同樣的事情。但是這個對我來說是充分利用「回報比例」> 10;「區分灰度和黑白。 – Civa

1

相反,PNG圖像,JPEG圖像不能黑白,由於路它編碼顏色信息。你可以在這裏閱讀更多關於它:http://en.wikipedia.org/wiki/JPEG

我想你可以計算圖片中不同顏色的數量,以確定它是否是雙色調,但是JPEG製品(即邊緣顏色流血)會給出不正確的結果。

+0

如果我不計算顏色灰度圖像存在也有較少的顏色。你可以區分顏色如何? – Civa

0

簡單的方法:嘗試將圖像轉換爲PNG,然後運行代碼。

編輯:加載爲圖形應該做的伎倆,是不是?

+3

將圖像保存爲PNG並不能保證它將使用最佳位深度,是嗎?此外,由於壓縮僞影,JPEG圖像幾乎不可能只有2種顏色。 –

+0

我也做了加載圖形的東西,它也沒有給出正確的結果 – Civa