2014-03-18 56 views
0

我花了很多時間下載並嘗試使用各種VB6實例 來獲取jpg圖像文件夾c:\ test的YCbCr的均值和協方差,但是,我只是僅在Csharp中獲取此代碼。如何讓它在VB 6.0中工作?VB6中圖像文件夾的均值和協方差

int num_samples = 0; 
foreach (string f in Directory.GetFiles(txtTrainingFolder.Text, "*.jpg")) 
{ 
    using (Bitmap bmp = new Bitmap(f)) 
     Add(bmp); 
    num_samples++; 
} 

public void Add(Bitmap bmp) 
{ 
    for (int y = 0; y < bmp.Height; y++) 
     for (int x = 0; x < bmp.Width; x++) 
     { 
      Color c = bmp.GetPixel(x, y); 
      // Skip black pixels in training images 
      if (c.R < 10 && c.G < 10 && c.B < 10) 
       continue; 
      double cb, cr; 
      CbCr(c.R, c.G, c.B, out cb, out cr); 
      sum_cr += cr; 
      sum_cb += cb; 
      sum_rr += cr * cr; 
      sum_rb += cr * cb; 
      sum_bb += cb * cb; 
      n++; 
     } 
} 

public void Finish() 
{ 
    // Mean 
    mean_cr = sum_cr/n; 
    mean_cb = sum_cb/n; 
    // Covariance 
    cov00 = sum_bb/n - mean_cb * mean_cb; 
    cov01 = sum_rb/n - mean_cr * mean_cb; 
    cov11 = sum_rr/n - mean_cr * mean_cr; 
    // Inverse covariance 
    double det = cov00 * cov11 - cov01 * cov01; 
    inv00 = cov00/det; 
    inv01 = -cov01/det; 
    inv11 = cov11/det; 
} 

static void CbCr(byte r, byte g, byte b, out double cb, out double cr) 
{ 
    double d0 = r/255.0, d1 = g/255.0, d2 = b/255.0; 
    cb = (-(0.148 * r) - (0.291 * g) + (0.439 * b) + 128); 
    cr = ((0.439 * r) - (0.368 * g) - (0.071 * b) + 128); 
    //cb = -37.797 * d0 - 74.203 * d1 + 112 * d2 + 128; 
    //cr = 112 * d0 - 93.786 * d1 - 18.214 * d2 + 128; 
} 
+0

根據您的情況,您可以將C#編譯爲DLL類庫,使其暴露於COM [示例](http://msdn.microsoft.com/zh-cn/library/c3fd4a20.aspx),然後在你的VB6代碼中調用你的類的實例? –

+0

@Hrqls,感謝您的評論,我如何讓VB6讀取圖像文件夾並讀取evey JPG像素? – user3356096

回答

1

嘗試以下項目:

磨片n您對picturebox單擊然後查找目錄中的所有JPG文件,並處理它們

'1 form with 
' 1 picturebox: name=Picture1 
Option Explicit 

Private Sub Form_Resize() 
    Picture1.Move 0, 0, ScaleWidth, ScaleHeight 
End Sub 

Private Sub Picture1_Click() 
    ProcessFiles "c:\temp\", "*.jpg" 
End Sub 

Private Sub ProcessFiles(strPath As String, strMask As String) 
    Dim strFile As String 
    strFile = Dir$(strPath & strMask) 
    Do Until Len(strFile) = 0 
     'ShowFile strPath & strFile 
     LoadFile strPath & strFile 
     strFile = Dir$() 'find next file 
    Loop 
End Sub 

Private Sub ShowFile(strFile As String) 
    Caption = strFile 
    Picture1.Picture = LoadPicture(strFile) 
End Sub 

Private Sub LoadFile(strFile As String) 
    Dim intFile As Integer 
    Dim bytArray() As Byte 
    intFile = FreeFile 
    Open strFile For Binary As #intFile 
     bytArray = Input(LOF(intFile), #intFile) 
    Close #intFile 
End Sub 
  • ShowFile將顯示在picturebox
  • LoadFile文件將文件加載到字節
  • 數組
+0

這就是代碼的作用就像一個魅力!謝謝 :) – user3356096