我的opencv2.4.9已经配置好的。
这上面新建的cpp中粘贴如下代码(图像读写+ vlfeat中的超像素分割),可以测试你之前安装的的opencv和刚才安装的vlfeat有没有正确配置。
(记得在cpp所在路径下放置1.jpg和1.png两张图片)。
#include
#include
#include
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
image[j + mat.cols*i + mat.cols*mat.rows * 1] =mat.at
image[j + mat.cols*i + mat.cols*mat.rows * 2] =mat.at
}
}
//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;
floatregularization = 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];
}
}
intlabel = 0;
intlabelTop = -1;
intlabelBottom = -1;
intlabelLeft = -1;
intlabelRight = -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;
如果(j> 0){
labelLeft = labels [i] [j - 1];
}
labelRight = label;
如果(j
labelRight = labels [i] [j + 1];
}
如果(label!= labelTop || label!= labelBottom || label!= labelLeft || label!= labelRight){
mat.at
mat.at
mat.at
}
}
}
cv :: imwrite(“1.png”,mat);
// waitKey(6000);
return0;
}