Opencv SLIC代码

时间:2014-12-23 11:01:54

标签: c++ opencv

    #include <opencv/cv.h>
    #include <opencv/highgui.h>
    #include <stdio.h>
    #include <math.h>  
    #include <vector>
    #include <float.h>
    using namespace std;
    using namespace cv;
    #include "slic.h"
    int main(int argc, char *argv[]) {
/* Load the image and convert to Lab colour space. */

    for(int i=1;i<=1863;i++)
  {
        stringstream ss;           
    ss<<i;                      
    string si = "/home/me/Desktop/dataset/" + ss.str() + ".jpg";
    Mat image;
    image=imread(si,1);
IplImage *lab_image;
IplImage *image2;
lab_image=cvCreateImage(cvSize(image.cols,image.rows),8,3);
image2=cvCreateImage(cvSize(image.cols,image.rows),8,3);
IplImage ipltemp=image; 
cvCopy(&ipltemp,lab_image);
cvCopy(&ipltemp,image2);
cvCvtColor(&image, lab_image, CV_BGR2Lab);


/* Yield the number of superpixels and weight-factors from the user. */
int w = image.cols, h = image.rows;
int nr_superpixels = atoi(argv[1]);
int nc = atoi(argv[2]);
double step = sqrt((w * h) / (double) nr_superpixels);


/* Perform the SLIC superpixel algorithm. */
Slic slic;
slic.generate_superpixels(lab_image, step, nc);
slic.create_connectivity(lab_image);


/* Display the contours and show the result. */
slic.display_contours(image2, CV_RGB(255,0,0));
Mat image3(image2);
imshow("result", image3);
cvWaitKey(0);
string ssave1 = "/home/me/Desktop/new_result/" + ss.str()+ ".jpg";
imwrite(ssave1,image3);
//cvSaveImage("/home/me/Desktop/img.jpg", image);
}
}

我收到了这个错误: 在抛出'cv :: Exception'的实例后终止调用   what():/ tmp/buildd/ros-hydro-opencv2-2.4.9-2precise-20140819-1808/modules/core/src/matrix.cpp:698:错误:( - 5)函数中的未知数组类型cvarrToMat < / p>

在抛出'cv :: Exception'的实例后终止调用   what():/ tmp/buildd/ros-hydro-opencv2-2.4.9-2precise-20140819-1808/modules/core/src/matrix.cpp:698:错误:( - 5)函数cvarrToMat中的未知数组类型 我是新来的cpp编码,有人可以告诉我哪里错了?

1 个答案:

答案 0 :(得分:1)

  1. 使用opencv时,请注意包含IplImages的任何内容。 必须过时且无法维护
  2. 获取https://github.com/berak/SLIC-Superpixels.git
  3. 更改您的代码:
  4. -

    #include <stdio.h>
    #include <math.h>  
    #include <vector>
    using namespace std;
    
    #include <opencv/opencv.hpp>
    using namespace cv;
    #include "slic.h"
    
    int main(int argc, char *argv[]) {
    
        /* Yield the number of superpixels and weight-factors from the user. */
        int nr_superpixels = atoi(argv[1]);
        int nc = atoi(argv[2]);
    
    
        for(int i=1;i<=1863;i++)
        {
            /* Load the image and convert to Lab colour space. */
            stringstream ss;           
            ss<<i;                      
            string si = "/home/me/Desktop/dataset/" + ss.str() + ".jpg";
            Mat image = imread(si,1);
            Mat lab_image;
            cvtColor(image, lab_image, COLOR_BGR2Lab);
    
            double step = sqrt((image.total()) / (double) nr_superpixels);
    
            /* Perform the SLIC superpixel algorithm. */
            Slic slic;
            slic.generate_superpixels(lab_image, step, nc);
            slic.create_connectivity(lab_image);
    
    
            /* Display the contours and show the result. */
            slic.display_contours(image2, Vec3b(0,0,255));
            imshow("result", image2);
            waitKey(0);
            string ssave1 = "/home/me/Desktop/new_result/" + ss.str()+ ".jpg";
            imwrite(ssave1,image2);
        }
    }