2012-10-28 112 views
1

我在託管代碼中遇到訪問衝突異常。直方圖指針不爲空,一切似乎都沒問題。接到http://www.emgu.com/forum/viewtopic.php?f=8&t=59訪問衝突異常emguCV cvQueryHistValue

// initializing data 
var random = new Random(); 
var array = new double[1000]; 
for (int i = 0; i < 1000; i++) 
{ 
    array[i] = random.NextDouble(); 
} 
var arrayPtr = GetDataPtr(array); 

//initializing ranges array 
double[] rangesArray = { 0, 1 }; 
var rangesArrayPtr = GetRangesArrayPtr(rangesArray); 
//creating and querying histogram 
var histogramStructure = CvInvoke.cvCreateHist(1, new[] {20}, HIST_TYPE.CV_HIST_ARRAY, rangesArrayPtr, true); 
var histogram = CvInvoke.cvMakeHistHeaderForArray(1, new[] { 20 }, histogramStructure, arrayPtr, rangesArrayPtr, 1); 
CvInvoke.cvNormalizeHist(histogram, 1.0); 
CvInvoke.cvQueryHistValue_1D(histogram, 0); // getting exception here 

幫助方法

private static IntPtr[] GetRangesArrayPtr(double[] array) 
{ 
    var ranges = new IntPtr[1]; 
    ranges[0] = Marshal.AllocHGlobal(array.Length * sizeof(double)); 
    Marshal.Copy(array, 0, ranges[0], array.Length); 
    return ranges; 
} 
private static IntPtr GetDataPtr(double[] array) 
{ 
    var ranges = new IntPtr(); 
    ranges = Marshal.AllocHGlobal(array.Length * sizeof(double)); 
    Marshal.Copy(array, 0, ranges,array.Length); 
    return ranges; 
} 

回答

0

例如,創建IntPtr的年代,我在最近的一個項目相同的問題,並通過複製直方圖值到一個新的陣列來解決它。

Double[] histtemp = new double[255]; 

Histogram.MatND.ManagedArray.CopyTo(histtemp,0); 

現在您可以訪問histt​​emp中的直方圖值。我希望它能幫助未來的觀衆。