VLfeat库---研习
官網:http://www.vlfeat.org/index.html
下載后解壓
不用管BioID-FaceDatabase-V1.2.zip,那是我下載的數據集
加壓后直接打開 vlfeat.sln ,我的是VS2013 發現打不開,ok,我們打開vl
這里是vlfeat的源代碼
打開VS2013,新建一個x64的工程,
將vl文件夾,整個文件夾復制到新建的工程下
將vl源代碼中所有頭文件.h,將全部? ?右鍵添加現有項,
這里別忘了,源代碼中有一個,
也一定要加上,
接著源文件.c,將全部? ?右鍵添加現有項,
這里還是別忘了,源代碼中有三個,
也一定要加上,
解釋一下
https://stackoverflow.com/questions/1877196/tc-th-files-for-c-program
他們使用這些作為模板并且文件不是直接編譯的,而是在設置影響最終結果的#defines之后在相應的.c或.h文件中包含#included。
一個例子是mathop_sse2.c中發生的事情。它們包括相同的mathop_sse2.tc兩次,但第一次將FLT定義為VL_TYPE_DOUBLE,第二次定義為VL_TYPE_FLOAT。這樣他們就可以避免為不同類型復制完全相同的代碼。
=============================
用我們的話理解,就是這個頭文件,一般不會被包含,當有#defines預定義時候,或者,添加某些預處理器時候,才可能使用到
?
完成后,工程樣貌
?
編譯會出錯,四中類型錯誤
第一種
?error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
解決:
要么? 設置,C++,預處理器,預處理器定義,加入? _CRT_SECURE_NO_WARNINGS
要么 哪個文件出現了,在那個文件的頭,加入警告忽略? ?#pragma warning( disable : 4996)
要么? 哪個文件出現了,在那個文件的頭,加入預定義???#define _CRT_SECURE_NO_WARNINGS
要么?在VS中新建項目的時候去掉“安全開發生命周期(SDL)檢查”即可將錯誤轉變成警告,使得使用不安全版本也不影響編譯和運行,如下圖所示。
第二種
error C2491,不允許dllimport函數的定義:
解決:
將宏VL_BUILD_DLL加入到預處理器定義中;
第三種
三個類似的錯誤,fatal error C1189: #error :?"Compiling with SSE2 enabled, …”:
解決:
分別將三個宏 __SSE2__、_SSE2_、__AVX__加入到預處理器定義中;
第四種
六個類似的錯誤,error C4146?一元負運算符應用于無符號類型,結果仍為無符號類型:
解決:
將C/C++--> 常規--> SDL檢查改為否(/sdl-)。
?
?
?
?
最后展示一下預處理器定義
?
這里還有一點問題,將我們新建的這個工程,改為動態庫.dll
然后添加輸出庫的名字
然后添加輸出調試庫pdb的名字
這些都是我們在Debug下完成的,切換到Release
同樣上面的步驟,預處理器那些,輸出名稱改為
..\VLfeat_64.dll
調試庫信息加上,但不生成,因為Release下不太需要這個,所以改為了否
然后,分別在Debug和Release點擊生成,此時就可以看到
VLfeat庫了
?
?
最后自己整理一下,新建文件夾,里面有三個子文件夾,bin,lib,include
各自放入
?
?
最后說幾句,如果你要跑vlfeat的例子,mser這個例子中,有個錯誤,
C++: malloc : error: invalid conversion from ‘void*’ to ‘uint8_t*’
vl_int8 buffer; buffer = malloc(numBytes);應該改為
vl_int8 buffer; buffer = (vl_int8 *)malloc(numBytes);?
?
最后最后,關于vlfeat的c接口的使用,其實就是與opencv對接,用opencv的讀取和顯示功能,其他,全部是vlfeat
比如
// OpenCV can be used to read images. #include <opencv2/opencv.hpp>// The VLFeat header files need to be declared external. extern "C" {#include "vl/generic.h"#include "vl/slic.h" }int main() {// Read the Lenna image. The matrix 'mat' will have 3 8 bit channels// corresponding to BGR color space.cv::Mat mat = cv::imread("Lenna.png", CV_LOAD_IMAGE_COLOR);// Convert image to one-dimensional array.float* image = new float[mat.rows*mat.cols*mat.channels()];for (int i = 0; i < mat.rows; ++i) {for (int j = 0; j < mat.cols; ++j) {// Assuming three channels ...image[j + mat.cols*i + mat.cols*mat.rows*0] = mat.at<cv::Vec3b>(i, j)[0];image[j + mat.cols*i + mat.cols*mat.rows*1] = mat.at<cv::Vec3b>(i, j)[1];image[j + mat.cols*i + mat.cols*mat.rows*2] = mat.at<cv::Vec3b>(i, j)[2];}}// The algorithm will store the final segmentation in a one-dimensional array.vl_uint32* segmentation = new vl_uint32[mat.rows*mat.cols];vl_size height = mat.rows;vl_size width = mat.cols;vl_size channels = mat.channels();// The region size defines the number of superpixels obtained.// Regularization describes a trade-off between the color term and the// spatial term.vl_size region = 30; float regularization = 1000.;vl_size minRegion = 10;vl_slic_segment(segmentation, image, width, height, channels, region, regularization, minRegion);// Convert segmentation.int** labels = new int*[mat.rows];for (int i = 0; i < mat.rows; ++i) {labels[i] = new int[mat.cols];for (int j = 0; j < mat.cols; ++j) {labels[i][j] = (int) segmentation[j + mat.cols*i];}}// Compute a contour image: this actually colors every border pixel// red such that we get relatively thick contours.int label = 0;int labelTop = -1;int labelBottom = -1;int labelLeft = -1;int labelRight = -1;for (int i = 0; i < mat.rows; i++) {for (int j = 0; j < mat.cols; j++) {label = labels[i][j];labelTop = label;if (i > 0) {labelTop = labels[i - 1][j];}labelBottom = label;if (i < mat.rows - 1) {labelBottom = labels[i + 1][j];}labelLeft = label;if (j > 0) {labelLeft = labels[i][j - 1];}labelRight = label;if (j < mat.cols - 1) {labelRight = labels[i][j + 1];}if (label != labelTop || label != labelBottom || label!= labelLeft || label != labelRight) {mat.at<cv::Vec3b>(i, j)[0] = 0;mat.at<cv::Vec3b>(i, j)[1] = 0;mat.at<cv::Vec3b>(i, j)[2] = 255;}}}// Save the contour image.cv::imwrite("Lenna_contours.png", mat);return 0; }?
哈哈哈哈哈哈,最后最后最后,再廢話一句,別嫌我嘮叨,為什么要pdb,如果沒有pdb,我們Debug,只能停在函數那里
?
如果你加上了pdb,程序跑起來,dll和lib,會自動加載pdb,Debug調試的時候,可以跳進函數內部,比如單步調試
?
?
函數內部信息,一覽無余,這樣,你可以快速的學習VLfeat的每一個函數
?
?
當然,你不要pdb也能進入,是因為,你在本機上編譯了這個dll。lib,pdb,只要你別動工程的位置,還是能進,我們說的是,假如,你把這個生成的庫,拿到另外的電腦上,還能進入源代碼,就必須一定一定一定一定一定一定,需要pdb文件了,而且,他和對應的dll,盡量放在一個路徑,名字相同,但是,不同模式下編譯的,名字要有別,以便Debug和Release分開!!!
?
我的修改版
// OpenCV can be used to read images. #include <opencv2/opencv.hpp>// The VLFeat header files need to be declared external. extern "C" { //告訴編譯器,這部分代碼按C語言的格式進行編譯,而不是C++的 #include "vl/generic.h" #include "vl/slic.h" }int main() {// Read the Lenna image. The matrix 'mat' will have 3 8 bit channels// corresponding to BGR color space.cv::Mat mat = cv::imread("img1.ppm", CV_LOAD_IMAGE_COLOR);// BGR// Convert image to one-dimensional array.// 將圖像轉換為一維數組。float* image = new float[mat.rows*mat.cols*mat.channels()];for (int i = 0; i < mat.rows; ++i) {for (int j = 0; j < mat.cols; ++j) {// Assuming three channels ...image[j + mat.cols*i + mat.cols*mat.rows * 0] = mat.at<cv::Vec3b>(i, j)[0];image[j + mat.cols*i + mat.cols*mat.rows * 1] = mat.at<cv::Vec3b>(i, j)[1];image[j + mat.cols*i + mat.cols*mat.rows * 2] = mat.at<cv::Vec3b>(i, j)[2];}}// The algorithm will store the final segmentation in a one-dimensional array.// 算法將最終分段存儲在一維數組中vl_uint32* segmentation = new vl_uint32[mat.rows*mat.cols];vl_size height = mat.rows;vl_size width = mat.cols;vl_size channels = mat.channels();// The region size defines the number of superpixels obtained.// Regularization describes a trade-off between the color term and the// spatial term.//區域大小定義了獲得的超像素的數量。//正則化描述了顏色術語和空間術語之間的權衡。vl_size region = 30;float regularization = 1000.;vl_size minRegion = 10;vl_slic_segment(segmentation, image, width, height, channels, region, regularization, minRegion);// Convert segmentation. 按行走,一行一行的遍歷,把segmentation 劃分成 labels[i][j] 圖像定位格式// 轉換 segmentationint maxlabels= 0;int** labels = new int*[mat.rows]; //行for (int i = 0; i < mat.rows; ++i) {labels[i] = new int[mat.cols]; // 列for (int j = 0; j < mat.cols; ++j) {labels[i][j] = (int)segmentation[j + mat.cols*i];if (labels[i][j]> maxlabels){maxlabels = labels[i][j];}}}/* opencv與二維數組相互轉換 */// labels[m][n] 二維數組-m代表行,n代表列cv::Mat opencv_labels = cv::Mat::ones(mat.rows, mat.cols, CV_32S);cv::Mat new_opencv_labels;new_opencv_labels.create(opencv_labels.rows, opencv_labels.cols, CV_64F);int *ptmp = NULL;double *new_ptmp = NULL;int opencv_tmp = 0;for (int m = 0; m < opencv_labels.rows; m++){ptmp = opencv_labels.ptr<int>(m);//指針指向img2的第i行new_ptmp = new_opencv_labels.ptr<double>(m);for (int n = 0; n < opencv_labels.cols; n++){//ptr[i][j] = mat.at<uchar>(i, j);//img的矩陣數據傳給二維數組ptr[][]opencv_tmp = labels[m][n];ptmp[n] = opencv_tmp;//二維數組數據傳給img2的第i行第j列new_ptmp[n] = ((double)opencv_tmp / (double)maxlabels) * (double)255;}}cv::Mat uchar_img;uchar_img.create(opencv_labels.rows, opencv_labels.cols, CV_8UC1);new_opencv_labels.convertTo(uchar_img, CV_8UC1); // or CV_32F works (too)cv::imshow("Lenna", uchar_img); // 感受一下分割// Compute a contour image: this actually colors every border pixel// red such that we get relatively thick contours.// 計算輪廓圖像:這實際上會為每個邊框像素著色// 紅色使得我們得到相對較厚的輪廓。int label = 0;int labelTop = -1;int labelBottom = -1;int labelLeft = -1;int labelRight = -1;for (int i = 0; i < mat.rows; i++) {for (int j = 0; j < mat.cols; j++) {label = labels[i][j];labelTop = label;if (i > 0) {labelTop = labels[i - 1][j];}labelBottom = label;if (i < mat.rows - 1) {labelBottom = labels[i + 1][j];}labelLeft = label;if (j > 0) {labelLeft = labels[i][j - 1];}labelRight = label;if (j < mat.cols - 1) {labelRight = labels[i][j + 1];}if (label != labelTop || label != labelBottom || label != labelLeft || label != labelRight) {mat.at<cv::Vec3b>(i, j)[0] = 0;mat.at<cv::Vec3b>(i, j)[1] = 0;mat.at<cv::Vec3b>(i, j)[2] = 255;}}}cv::imshow("Lenna_contours", mat);// Save the contour image.cv::imwrite("Lenna_contours.png", mat);cv::waitKey();return 0; }總結
以上是生活随笔為你收集整理的VLfeat库---研习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 加州大学信息科学院长:数据科学课程不只是
- 下一篇: 大数据汇聚全球精英