1

我試圖在我的Windows Phone應用程序中應用Erosion和Dilation算法:)。在Windows手機中的侵蝕和膨脹???

但就是很難做到:(我研究了一些例子,我發現這個http://channel9.msdn.com/coding4fun/articles/FaceLight--Silverlight-4-Real-Time-Face-Detection

FaceLight的Silverlight。

是一個很好的例子,已經侵蝕和擴張算法,但我的應用程序有一些差異,FaceLight採取從相機拍攝照片,我想我的應用程序從庫加載一些圖像並應用算法。

我遵循這個步驟 1.-從畫廊加載圖像。

PhotoChooserTask photo = new PhotoChooserTask(); 
     photo.Completed += (s, ee) => 
     { 



      BitmapImage image = new BitmapImage(); 

      image.SetSource(ee.ChosenPhoto); 

      image.CreateOptions = BitmapCreateOptions.None; 

      this.imgoriginal.Source = image; 

      GrayTimer.Start(); 



     }; 

     photo.Show(); 

2.-從原始圖像中獲取灰度圖像。

WriteableBitmap grayimg = Operations.doGray(imgoriginal); // get the grayscale image 

     BitmapImage imggray = new BitmapImage(); 

     stream = new MemoryStream(grayimg.ToByteArray()); // get the array of the image 

     this.Dispatcher.BeginInvoke(() => 
     { 
      grayimg.SaveJpeg(stream, 320, 240, 0, 90); // resize and set quality 
      imggray.SetSource(stream); 
      grayImage.Source = imggray;// set the grayscale image in the control. 
     }); 

這種方法DoGray()

public static WriteableBitmap doGray(Image image) 
    { 
     WriteableBitmap bitmap = new WriteableBitmap(image, null); 
     for (int y = 0; y < bitmap.PixelHeight; y++) 
     { 
      for (int x = 0; x < bitmap.PixelWidth; x++) 
      { 
       int pixelLocation = bitmap.PixelWidth * y + x; 
       int pixel = bitmap.Pixels[pixelLocation]; 
       byte[] pixelbytes = BitConverter.GetBytes(pixel); 
       byte bwPixel = (byte)(.299 * pixelbytes[2] + .587 * pixelbytes[1] + .114 * pixelbytes[0]); 
       pixelbytes[0] = bwPixel; 
       pixelbytes[1] = bwPixel; 
       pixelbytes[2] = bwPixel; 
       bitmap.Pixels[pixelLocation] = BitConverter.ToInt32(pixelbytes, 0); 
      } 
     } 

     return bitmap; 
    } 

一切工作正常。

Original Image

GrayScale Image

現在我申請從FaceLight腐蝕運算。

WriteableBitmap g; 

     BitmapImage imggray = new BitmapImage(); 

     imggray =(BitmapImage) grayImage.Source; // get the gray image from the Control. 


     g = new WriteableBitmap(imggray); 

     // i apply 3 times for try to see something :S 
     er = Operations.Erode(g); 
     for (int i = 1; i <= 2; i++) 
     { 
      Stream str = new MemoryStream(er.ToByteArray()); 
      BitmapImage r = new BitmapImage(); 
      WriteableBitmap n; 
      er.SaveJpeg(str, 320, 240,0, 100); 
      r.SetSource(str); 
      n = new WriteableBitmap(r); 
      er = Operations.Erode(n); 

     } 

     this.Dispatcher.BeginInvoke(() => 
     { 
      stream = new MemoryStream(er.ToByteArray()); 
      BitmapImage result = new BitmapImage(); 
      er.SaveJpeg(stream, 320, 240, 0, 100); 
      result.SetSource(stream); 
      imgerode.Source = result; 
     }); 

這裏是腐蝕運算

// Process method of the Erode5x5Filter class 
    public static WriteableBitmap Erode(WriteableBitmap input) 
    { 
     var p = input.Pixels; 
     var w = input.PixelWidth; 
     var h = input.PixelHeight; 
     var result = new WriteableBitmap(w, h); 
     var rp = result.Pixels; 
     var empty = 0; // = 0 
     int c, cm; 
     int i = 0; 

     // Erode every pixel 
     for (int y = 0; y < h; y++) 
     { 
      for (int x = 0; x < w; x++, i++) 
      { 
       // Middle pixel 
       cm = p[y * w + x]; 
       if (cm == empty) { continue; } 

       // Row 0 
       // Left pixel 
       if (x - 2 > 0 && y - 2 > 0) 
       { 
        c = p[(y - 2) * w + (x - 2)]; 
        if (c == empty) { continue; } 
       } 
       // Middle left pixel 
       if (x - 1 > 0 && y - 2 > 0) 
       { 
        c = p[(y - 2) * w + (x - 1)]; 
        if (c == empty) { continue; } 
       } 
       if (y - 2 > 0) 
       { 
        c = p[(y - 2) * w + x]; 
        if (c == empty) { continue; } 
       } 
       if (x + 1 < w && y - 2 > 0) 
       { 
        c = p[(y - 2) * w + (x + 1)]; 
        if (c == empty) { continue; } 
       } 
       if (x + 2 < w && y - 2 > 0) 
       { 
        c = p[(y - 2) * w + (x + 2)]; 
        if (c == empty) { continue; } 
       } 

       // Row 1 
       // Left pixel 
       if (x - 2 > 0 && y - 1 > 0) 
       { 
        c = p[(y - 1) * w + (x - 2)]; 
        if (c == empty) { continue; } 
       } 


       // ... 
       // ... Process the rest of the 24 neighboring pixels 
       // ... 


       // If all neighboring pixels are processed 
       // it's clear that the current pixel is not a boundary pixel. 
       rp[i] = cm; 
      } 
     } 

     return result; 
    } 

這裏是結果:沒什麼發生:(

Erosion image

與擴張算法,但結果相同的問題圖片是空白圖片 任何想法o建議?我需要幫助

回答

0

我趕緊看看你的腐蝕方法。你爲什麼不寫一個循環(實際上從-2到+2的兩個循環)來處理你正在處理的25個事件?

+0

我不認爲這是他的代碼 - 他說,從FaceLight這是一個庫 – smocking 2012-04-21 01:31:38

+0

就是你的右邊的代碼侵蝕算法是從FaceLight – 2012-04-21 01:54:11

+0

我自己的解決方案:我通過WCF使用AForge.NET框架,我從Windows Phone發送圖像到WCF和WCF返回結果圖像:)。 – 2012-05-04 19:40:09

0

儘管Erode函數看起來應該可以在灰度上工作,但是在調用侵蝕之前,您是否嘗試將圖像閾值化爲二進制掩碼(例如rp = p> 127?255:0)?

你在這裏的侵蝕函數的代碼也不會改變從0 i ...