Opencv SLIC code

Opencv SLIC code

本文关键字:code SLIC Opencv      更新时间:2023-10-16
    #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::异常"实例后终止调用 what():/tmp/buildd/ros-hydro-opencv2-2.4.9-2precise-20140819-1808/modules/core/src/matrix.cpp:698:错误:(-5) 函数 cvarrToMat 中的数组类型未知

在抛出"cv::异常"实例后终止调用 what():/tmp/buildd/ros-hydro-opencv2-2.4.9-2precise-20140819-1808/modules/core/src/matrix.cpp:698:错误:(-5) 函数 cvarrToMat 中的数组类型未知我是 cpp 编码的新手,有人可以说出问题所在吗?

  1. 使用OpenCV时,要警惕任何包含IplImages的内容。 必须过时且未维护
  2. 获取 https://github.com/berak/SLIC-Superpixels.git
  3. 更改您的代码:

--

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