2012-06-28 42 views
3

我想將一個圖像的alpha通道與其他圖像進行比較。我的比較方法是這樣的:我可以將圖像處理與PLINQ並行嗎?

public static unsafe double Similiarity (Bitmap a, Bitmap b) 
{ 
    BitmapData aData = a.LockBits (
     new Rectangle (0, 0, a.Width, a.Height), 
     System.Drawing.Imaging.ImageLockMode.ReadOnly, a.PixelFormat); 
    BitmapData bData = b.LockBits (
     new Rectangle (0, 0, b.Width, b.Height), 
     System.Drawing.Imaging.ImageLockMode.ReadOnly, b.PixelFormat); 
    int PixelSize = 4; 

    double sum = 0; 
    for (int y=0; y<aData.Height; y++) { 
     byte* aRow = (byte *)aData.Scan0 + (y * aData.Stride); 
     byte* bRow = (byte *)bData.Scan0 + (y * bData.Stride); 
     for (int x=0; x<aData.Width; x++) { 
      byte aWeight = aRow [x * PixelSize + 3]; 
      byte bWeight = bRow [x * PixelSize + 3]; 
      sum += Math.Abs (aWeight - bWeight); 
     } 
    } 
    a.UnlockBits (aData); 
    b.UnlockBits (bData); 

    return 1 - ((sum/255)/(a.Width * a.Height)); 
} 

我認爲加速計算是通過使用PLINQ最簡單的方法:

var list = from Bitmap img in imageList.AsParallel where (Similiarity (referenceImage, img) > 0.5) select img; 

但在執行中有gdiplus一個例外:

System.InvalidOperationException: The operation is invalid [GDI+ status: Win32Error] 
    at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] in <filename unknown>:0 
    at System.Drawing.Bitmap.LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format, System.Drawing.Imaging.BitmapData bitmapData) [0x00000] in <filename unknown>:0 

我知道gdiplus必須在不同的進程中執行,但我認爲PLINQ正在這樣做。我的假設有什麼問題?

+0

PLINQ不使用不同的工藝來執行。它使用線程。 – svick

回答

3

我會建議重量計算從這樣的相似性比較分開:

public static unsafe byte[,] GetWeight(Bitmap a) 
{ 
    BitmapData aData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadOnly, a.PixelFormat); 
    const int pixelSize = 4; 

    byte[,] weight = new byte[aData.Width, aData.Height]; 
    for (int y = 0; y < aData.Height; y++) 
    { 
     byte* aRow = (byte*)aData.Scan0 + (y * aData.Stride); 
     for (int x = 0; x < aData.Width; x++) 
     { 
      byte aWeight = aRow[x * pixelSize + 3]; 
      weight[x, y] = aWeight; 
     } 
    } 
    a.UnlockBits(aData); 
    return weight; 
} 

public static double GetSimilarity(byte[,] weightsA, byte[,] weightsB) 
{ 
    double sum = 0; 
    int height = weightsA.GetLength(1); 
    int width = weightsA.GetLength(0); 
    for (int y = 0; y < height; y++) 
    { 
     for (int x = 0; x < width; x++) 
     { 
      byte aWeight = weightsA[x,y]; 
      byte bWeight = weightsB[x, y]; 
      sum += Math.Abs(aWeight - bWeight); 
     } 
    } 

    return 1 - ((sum/255)/(width * height)); 
} 

調用本身則是這樣的:肯定

var referenceWeigth = GetWeight(referenceImage);  
var list = 
     imageList 
      .AsParallel() 
      .Select(image => new {@Image = image, @Weight = GetWeight(image)}) 
      .Where(imageAndWeight => GetSimilarity(imageAndWeight.Weight, referenceWeigth) > 0.5) 
      .Select(imageAndWeight => imageAndWeight.Image); 
+1

感謝您的建議代碼。它按預期工作,並增強了我對LINQ的理解。 – Rodja

1

我認爲這個問題可能是你試圖同時在不同的線程中鎖定相同的位。一個可能的解決方案(正如你只讀)應該是首先從每個位圖提取位,並只使用這些數組並行比較它們。

1

我看到的問題是,雖然img取自一個列表,referenceImage是多個線程中使用的相同實例,每個線程都試圖鎖定它。

考慮鎖定外部的參考圖像,並傳遞BitmapData。

相關問題