SLIC c++ segmentation

走远了吗. 提交于 2019-12-24 04:24:08

问题


Im trying to segment an image using SLIC in OpenCV. Im trying to use the following function:

void vl_slic_segment    (   vl_uint32 *     segmentation,
float const *   image,
vl_size     width,
vl_size     height,
vl_size     numChannels,
vl_size     regionSize,
float   regularization,
vl_size     minRegionSize 
)

the #include is fine and the linking to libraries is fine. I just need to know how can I pass the image to this function. the image parameter in this function is of type float const * and I dont know how to convert the image to this type.

Here is how i am loading the image into the code:

IplImage *image = cvLoadImage("train.tif", 1);

and here is the whole code:

extern "C" {
  #include </home/me/Downloads/vlfeat-0.9.17/vl/slic.h>
}
#include <stdio.h>
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include<opencv/highgui.h>

using namespace std;
using namespace cv;

int main () {
    IplImage *image = cvLoadImage("train.tif", 1);

   vl_uint32 * seg;

   vl_slic_segment(seg,(const float *)image,image->width,image->height,image->nChannels,15,0.1,1);

  waitKey(0);
}

and also i dont know if i am using the vl_uint32 * seg correctly. Please if someone has an example or sample code to do this segmentation.

Thanks !!


回答1:


You need to allocate storage for seg correctly. If you are going to use the C++ API as in berak's answer, (which I also recommend) you could create a Mat to hold the label data to make later access easier and automatically manage memory:

cv::Mat labels(floatimg.size(), CV_32SC1); // Mat doesn't support 32-bit unsigned directly, but this should work fine just to hold data.
vl_slic_segment(labels.ptr<vl_uint32>(),floatimg.ptr<float>(),floatimg.cols,floatimg.rows,floatimg.channels(),15,0.1,1);

If for some reason you don't want to do that, you would allocate a chunk of raw memory like this (not recommended):

vl_uint32* seg = new vl_uint32[floatimg.total()]; // don't forget to delete[]

Or if you decide to continue with the C API, you would use malloc (really not recommended):

vl_uint32* seg = (vl_uint32*)malloc(img->height * img->width); // don't forget to free()



回答2:


don't pass the whole image, only the pixels ! and please use the c++ api, not the old c one.

Mat img = imread("train.tif", 1); // 0 for grayscale
Mat floatimg;
img.convertTo(CV_32FC3,floatimg); // CV_32FC1 for grayscale

vl_slic_segment(seg,(const float *)(floatimg.data),floatimg.cols,floatimg.rows,floatimg.channels(),15,0.1,1);


来源:https://stackoverflow.com/questions/18468565/slic-c-segmentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!