在EmguCV中把直方圖相關資訊儲存成檔案,不像在OpenCV時有一些函式方法可以解決(Ex:CvOpenFileStorage、cvWrite...)。
EmguCV不好找 ,而且OpenCV相關個涵式在CvInvoke中也不齊全(Ex:cvWrite不存在),想使用C#的XmlSerializer,也會發現EmguCV的值方圖類別DenseHistogram不支援,好在後發現了一篇討論文找出了解決方法
前言
在EmguCV中把直方圖相關資訊儲存成檔案,不像在OpenCV時有一些函式方法可以解決(Ex:CvOpenFileStorage、cvWrite...)。
EmguCV不好找 ,而且OpenCV相關個涵式在CvInvoke中也不齊全(Ex:cvWrite不存在),想使用C#的XmlSerializer,也會發現EmguCV的值方圖類別DenseHistogram不支援,好在後發現了一篇討論文找出了解決方法
保存與讀取Histogram
在EmguCV中,計算與紀錄Histogram資料的方式主要都是透過DenseHistogram類別,但是EmguCV其實並沒有完整的所有OpenCV中的功能都包裝進來,有些可能也有Bug要待下一版處理
使用EmguCV 2.4.0時,不像在OpenCV時有完整可對應的函式方法可以解決
Ex:不法如透過CvOpenFileStorage與cvWrite和cvRead讀寫檔`,因為沒有cvWrite
後來自己透過extern並從openCV中的DLL找出對應匯入,使用cvWrite,卻又發現openCV中的方法:cvRead或cvReadByName會失敗…
最後發現DenseHistogram有支援序列化!
解決方式-使用BinaryFormatter序列化與反序列化
	透過二位元序列化方式把整個資料Save至檔案或Load到DenseHistogram
	記得加入
using System.Runtime.Serialization.Formatters.Binary;
	
	 
Save存入檔案
private void SaveHistToBinaryFile(DenseHistogram  histDense,string textFileName)
       {
           Stream stream;
           System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter;
           //寫檔
           try
           {
               // serialize histogram
               stream = File.Open(textFileName + ".bin", FileMode.Create);
               bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
               bformatter.Serialize(stream, histDense);
               stream.Close();
           }
           catch (IOException ex)
           {
               throw new InvalidOperationException(ex.Message);
           }
       }
Load讀取檔案
private DenseHistogram LoadHistBinaryFile(string loadHistFileName) {
           Stream stream;
           System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter;
           DenseHistogram histLoaded;
           try
           {
               if (File.Exists(loadHistFileName))
               {
                   stream = File.Open(loadHistFileName, FileMode.Open);
                   bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                   histLoaded = (DenseHistogram)bformatter.Deserialize(stream);
                   stream.Close();
                   return histLoaded;
               }
               return null;
           }
           catch (IOException ex)
           {
               throw new InvalidOperationException(ex.Message);
           }
       }
解決方式-使用SoapFormatter序列化與反序列化
記得加入
using System.Runtime.Serialization.Formatters.Soap;
Save存入檔案
private void SaveHistToSOAPFile(DenseHistogram histDense, string textFileName)
        {
            Stream stream;
            SoapFormatter soapformatter;
            //寫檔
            try
            {
                // serialize histogram
                stream = File.Open(textFileName + ".soap", FileMode.Create);
                soapformatter = new SoapFormatter();
                soapformatter.Serialize(stream, histDense);
                stream.Close();
            }
            catch (IOException ex)
            {
                throw new InvalidOperationException(ex.Message);
            }
        }
Load讀取檔案
private DenseHistogram LoadHistSOAPFile(string loadHistFileName)
        {
            Stream stream;
            SoapFormatter soapformatter;
            DenseHistogram histLoaded;
            try
            {
                if (File.Exists(loadHistFileName))
                {
                    stream = File.Open(loadHistFileName, FileMode.Open);
                    soapformatter = new SoapFormatter();
                    histLoaded = (DenseHistogram)soapformatter.Deserialize(stream);
                    stream.Close();
                    return histLoaded;
                }
                return null;
            }
            catch (IOException ex)
            {
                throw new InvalidOperationException(ex.Message);
            }
        }
至於使用XML序列化則會出現以下狀況
參考資料
文章中的敘述如有觀念不正確錯誤的部分,歡迎告知指正 謝謝 =)
另外要轉載請附上出處 感謝