Fra*_*ank 5 c++ opencv image-segmentation vlfeat
我试图使用vl_slic_segment
存储在OpenCV Mat中的输入图像来使用VLFeat库的功能.我的代码正在编译和运行,但输出的超像素值没有意义.到目前为止,这是我的代码:
Mat bgrUChar = imread("/pathtowherever/image.jpg");
Mat bgrFloat;
bgrUChar.convertTo(bgrFloat, CV_32FC3, 1.0/255);
cv::Mat labFloat;
cvtColor(bgrFloat, labFloat, CV_BGR2Lab);
Mat labels(labFloat.size(), CV_32SC1);
vl_slic_segment(labels.ptr<vl_uint32>(),labFloat.ptr<const float>(),labFloat.cols,labFloat.rows,labFloat.channels(),30,0.1,25);
Run Code Online (Sandbox Code Playgroud)
我试过不将它转换为Lab颜色空间并设置不同的regionSize/regularrization,但输出总是非常小问题.我能够正确检索标签值,事情是每个标签通常散布在一个非连续的小区域.
我认为问题是我的输入数据格式错误但我无法弄清楚如何将其正确发送到该vl_slic_segment
功能.
先感谢您!
编辑
谢谢David,正如您帮助我理解的那样,vl_slic_segment希望数据按[LLLLLAAAAABBBBB]排序,而OpenCV则为LAB颜色空间排序其数据[LABLABLABLABLAB].
在我的学士论文过程中,我也必须使用 VLFeat 的 SLIC 实现。Lenna.png
您可以在 GitHub 上找到应用 VLFeat 的 SLIC 的简短示例:https: //github.com/davidstutz/vlfeat-slic-example。
也许,看一下main.cpp
可以帮助您弄清楚如何将 OpenCV 获得的图像转换为正确的格式:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
此外,请查看README.md
GitHub 存储库。下图显示了将正则化设置为 1 (100,1000) 并将区域大小设置为 30 (20,40) 的一些示例输出。
图 1:超像素分割,区域大小设置为 30,正则化设置为 1。
图 2:超像素分割,区域大小设置为 30,正则化设置为 100。
图 3:超像素分割,区域大小设置为 30,正则化设置为 1000。
图 4:超像素分割,区域大小设置为 20,正则化设置为 1000。
图 5:超像素分割,区域大小设置为 20,正则化设置为 1000。