2017-02-15 55 views
2

OpenCV 3.2 我用平均文件「mean.binaryproto」下載了Caffe模型。我想用平均值減去輸入圖像並使用它。 如何將「mean_file_proto」讀入OpenCV Mat以從Caffe Model中使用均值中減去圖像?如何將「mean_file_proto」讀入OpenCV Mat以從Caffe Model中使用均值中減去圖像?

編輯: 我可以使用OpenCV而不使用Caffe嗎?我在Windows 10 64位的Visual Studio 2015中使用OpenCV 3.2。我從網絡攝像頭獲取圖像,並且我想通過使用OpenCV :: dnn獲取結果來減去平均值。

回答

2

平均文件是平面的,可能是1或3個通道。比例是[0,255]。

caffe::BlobProto mean_proto; 
caffe::ReadProtoFromBinaryFile(filename, &mean_proto); // check return code 
caffe::Blob<float> mean_blob; 
mean_blob.FromProto(mean_proto); 
float *data = mean_blob.mutable_cpu_data(); 

現在,您可以創建CV_32FC1類型的cv::Mat,每個通道。

cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data); 
data += mean_blob.height() * mean_blob.width(); 

最後,填充這些頻道的載體,並使用cv::merge()將它們組合成一個單一的cv::Mat,你可以從另一個cv::Mat包含你輸入圖像中減去。如果你的圖像已經被標準化[0,1],你還需要重新調整平均值。

+0

謝謝。我可以使用OpenCV而不使用Caffe嗎?我在Windows 10 64位的Visual Studio 2015中使用OpenCV 3.2。我從網絡攝像頭獲取圖像,並且我想通過使用OpenCV :: dnn獲取結果來減去平均值。在此先感謝 –

+1

那麼,'ReadProtoFromBinaryFile()'基本上是Google Protobuf庫的一個包裝。請參閱https://github.com/BVLC/caffe/blob/master/src/caffe/util/io.cpp。我對這個庫沒有直接的經驗,但它似乎涉及一個解析器生成器,它提供了對數據格式的描述。如果你不想與Caffe鏈接,那麼你需要在https://github.com/BVLC/caffe/blob/master/src/caffe/proto/caffe.proto上運行protobuf的protoc工具,以生成'caffe :: ReadProtoFromBinaryFile()'中使用的一些函數。請參閱https://github.com/BVLC/caffe/issues/22 –

+1

@ video-analysis-deep-learning您還可以利用Caffe的Python語言綁定,並使用它編寫轉換工具從這些文件中提取平均圖像。 –

相關問題