您的位置:首页 > Web前端

VLFeat + VS2013+opencv 配置

2016-01-18 18:01 357 查看

这个可以和opencv配置一样,只需要配置一次,以后就再也不用配置了,一劳永逸~~~~

vlfeat图像库包含SIFT,MSER,KDtree,快速shift,K-means等各种图像处理中常用的算法。最近想看看里面的东西…….顺带把它配置起来……..

说明:

1.系统环境:win 7 64位专业版,VS2013旗舰版2.4.9

2.opencv 配置很简单,随便参考一篇博文就行了,这里就不叙述了……

1.下载vlfeat

项目主页是:http://www.vlfeat.org/

也可以下载我上传的http://download.csdn.net/detail/lilai619/9114675

2.安装

只需要解压、改名为vlfeat、放到自己指定的目录就行了。

以我的为例子:D:\Software\Tools—>D:\Software\Tools\vlfeat



3.配置

1. 添加系统环境变量:

右击我的电脑——属性——高级系统设置——环境变量——系统环境变量——path



2.在VS2013中新建一个cpp:

右击源文件——添加cpp



3.视图——属性管理器——右击Microsoft.cpp.win32.user



4.在 VC++目录——包含目录

添加 D:\Software\Tools\vlfeat



5.在 链接器——常规——附加库目录

添加 D:\Software\Tools\vlfeat\bin\win32



6.在 链接器——输入——附加依赖库

添加 vl.lib



4.测试

我的opencv2.4.9是已经配置好的。

这上面新建的cpp中粘贴如下代码(图像读写+vlfeat中的超像素分割),可以测试你之前安装的的 opencv 和 刚才安装的 vlfeat 有没有正确配置。

(记得在cpp所在路径下放置1.jpg和1.png两张图片)。

#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
extern "C" {
#include "vl/generic.h"
#include "vl/slic.h"
}
using namespace cv;

int main(int argc, const char * argv[]) {
//// insert code here...
std::cout << "Hello, World!\n";
VL_PRINT("hello, VLFeat!\n");
// 读入一张图片(游戏原画)
Mat img = imread("1.jpg");
// 创建一个名为 "游戏原画"窗口
// 下面3句用于测试opencv

namedWindow("游戏原画");
imshow("游戏原画", img);
waitKey(3000);

// 下面用于测试vlfeat
cv::Mat mat = cv::imread("1.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];
}
}

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::imwrite("1.png", mat);
//waitKey(6000);
return 0;
}


注意:如果提示缺少vl.dll 不能运行的话

将 D:\Software\Tools\vlfeat\bin\win32路径下的vl.dll拷贝到项目生成的debug文件夹再编译就OK了.

效果图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: