gpt4 book ai didi

c++ - OpenCV VLFeat Slic 函数调用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:39:29 26 4
gpt4 key购买 nike

我正在尝试使用存储在 OpenCV Mat 中的输入图像来使用 VLFeat 库的 vl_slic_segment 函数。我的代码正在编译和运行,但输出的超像素值没有意义。到目前为止,这是我的代码:

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);

我尝试过不将其转换为 Lab 色彩空间并设置不同的区域大小/正则化,但输出总是非常不稳定。我能够正确检索标签值,问题是每个标签通常分散在一个不连续的小区域。

我认为问题是我的输入数据格式错误,但我不知道如何将其正确发送到 vl_slic_segment 函数。

提前致谢!

编辑

谢谢大卫,正如你帮助我理解的那样,vl_slic_segment 希望数据按 [LLLLLAAAAAABBBBB] 排序,而 OpenCV 将其数据排序为 [LABLABLABLABLAB] LAB 颜色空间。

最佳答案

在我的学士论文过程中,我还必须使用 VLFeat 的 SLIC 实现。您可以在 GitHub 上的 Lenna.png 上找到应用 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;
}

此外,请查看 GitHub 存储库中的 README.md。下图显示了将正则化设置为 1 (100,1000) 并将区域大小设置为 30 (20,40) 的一些示例输出。

Superpixel segmentation with region size set to 30 and regularization set to 1.

图 1:区域大小设置为 30 且正则化设置为 1 的超像素分割。

Superpixel segmentation with region size set to 30 and regularization set to 100.

图 2:区域大小设置为 30 且正则化设置为 100 的超像素分割。

Superpixel segmentation with region size set to 30 and regularization set to 1000.

图 3:区域大小设置为 30 且正则化设置为 1000 的超像素分割。

Superpixel segmentation with region size set to 20 and regularization set to 1000.

图 4:区域大小设置为 20 且正则化设置为 1000 的超像素分割。

Superpixel segmentation with region size set to 20 and regularization set to 1000.

图 5:区域大小设置为 20 且正则化设置为 1000 的超像素分割。

关于c++ - OpenCV VLFeat Slic 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24403743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com