2011-10-22 21 views
0

對不起,我不知道如何設置一個主題,可以表達我需要什麼幫助。一種列出字節數組像素值的方法

我有一個字節數組,位圖中每個像素的值。它是從左到右的一維數組。它佔用每行並將其添加到數組索引的末尾。

我想分割一個位圖爲225(= 15 * 15)件。每塊磚具有例如尺寸34×34,並且陣列的長度爲260×100(= 225×34×34)。所以就像你現在看到的那樣,我們需要15塊寬度和高度的磚塊。

幾個月前我從0-14開始使用兩個循環。我寫了自己的長代碼來獲得所有這些34x34磚塊。但是我沒有使用任何存儲所有值的數組。

現在我有一個一維數組,因爲帶位鎖的元帥副本和位圖數據是將所有像素的值快速複製到數組的最佳方法。

而是我站在面對面與問題如何得到那麼34元一排低,另外一個知道在35水平將有自己的初始值的另一塊磚..

PS。編輯我的帖子,如果事情不好。

很少有人會說「首先製作任何測試代碼」。我嘗試過,但我得到的只是垃圾,我真的不知道該怎麼做。


此方法用於將圖像裁剪爲包含磚塊的較小圖像。但我不希望存儲磚的小圖像。我需要存儲在字節數組中的值。

下,有一個證明。

private void OCropImage(int ii, int jj, int p, int p2) 


    { 
     ////We took letter and save value to binnary, then we search in dictionary by value 
     this.rect = new Rectangle(); 
     this.newBitmap = new Bitmap(this.bitmap); 
     for (ii = 0; ii < p; ii++) 
     { 
      for (jj = 0; jj < p2; jj++) 
      { 
       ////New bitmap 
       this.newBitmap = new Bitmap(this.bitmap); 

       ////Set rectangle working area with letters 
       this.rect = new Rectangle(jj * this.miniszerokosc, ii * this.miniwysokosc, this.miniszerokosc, this.miniwysokosc); 
       ////Cut single rectangle with letter 
       this.newBitmap = this.newBitmap.Clone(this.rect, this.newBitmap.PixelFormat); 
       ////Add frame to rectangle to delet bad noise 
       this.OAddFrameToCropImage(this.newBitmap, this.rect.Width, this.rect.Height); 
       this.frm1.SetIm3 = (System.Drawing.Image)this.newBitmap; 

       ////Create image with letter which constains less background 
       this.newBitmap = this.newBitmap.Clone(this.GetAreaLetter(this.newBitmap), this.newBitmap.PixelFormat); 
       ////Count pixels in bitmap 
       this.workingArea = this.GetBinnary(this.newBitmap); 

       var keysWithMatchingValues = this.alphabetLetters.Where(x => x.Value == this.workingArea).Select(x => x.Key); 
       foreach (var key in keysWithMatchingValues) 
       { 
        this.chesswords += key.ToString(); 
       } 
      } 

      this.chesswords += Environment.NewLine; 
      var ordered = this.alphabetLetters.OrderBy(x => x.Value); 
     } 
    } 

PS2。對不起我的英文,如果需要的話請糾正。

+0

你真正的問題是什麼? – misha

回答

0

如果我得到你的權利,那麼,如果你有這樣

p00|p01|p02|... 
---+---+------- 
p10|p11|p12|... 
---+---+------- 
p20|p21|p22|... 
---+---+---+--- 
...|...|...|... 

圖像存儲在陣列中的左到右行掃描是這樣的:

p00,p01,...,p0n, p10,p11,...,p1n, p20,p21, ... 

如果我正確地理解了你,你想要做的是,從圖像中取一個給定的矩形(來自具有一定寬度和高度的特定x和y)。下面是代碼來做到這一點,有解釋:

byte[] crop_area (byte[] source_image, int image_width, int image_height, 
int start_x, int start_y, int result_width, int result_height) 
{ 
    byte[] result = new byte[result_width * result_height]; 
    int endX = x + result_width; 
    int endY = y + result_height; 

    int pos = 0; 

    for (int y = startY; y < endY; y++) 
     for (int x = startX; x < endX; x++) 
     { 
      /* To get to the pixel in the row I (starting from I=1), we need 
      * to skip I-1 rows. Since our y indexes start from row 0 (not 1), 
      * then we don't need to subtract 1. 
      * 
      * So, the offset of the pixel at (x,y) is: 
      * 
      *  y * image_width  +  x 
      * |-----------------------| |-----------------| 
      * Skip pixels of y rows Offset inside row 
      */ 
      result[pos] = source_image[y * image_width + x]; 

      /* Advance to the next pixel in the result image */ 
      pos++; 
     } 
    return result; 
} 

然後,取該塊中的行I和J列在(I,J = 0,...,14)做:

crop_area (source_image, image_width, image_height, J*image_width/15, I*image_height/15, image_width/15, image_height/15) 
+0

我認爲這將是答案,但我會檢查出tommorow :)感謝您的幫助! :) – deadfish

+0

是的,這就是(說什麼更多?) – deadfish

+0

嘿,但我有字節數組,它存儲的值bitmapdata.sride * bitmap.height,什麼是假設操作呢?圖像是10x10不會給出結果100:| – deadfish