Esempio n. 1
0
/**
* Draw OpenCV matrix using QLabel
*/
void ImageApp::doSLIC()
{
    processedImage = originalImage; //浅拷贝
    //processedImage = originalImage.clone(); //深拷贝
    float* image = new float[processedImage.rows*processedImage.cols*processedImage.channels()];
    for (int i = 0; i < processedImage.rows; ++i) {
        for (int j = 0; j < processedImage.cols; ++j) {
            // Assuming three channels ...
            image[j + processedImage.cols*i + processedImage.cols*processedImage.rows*0] = processedImage.at<cv::Vec3b>(i, j)[0];
            image[j + processedImage.cols*i + processedImage.cols*processedImage.rows*1] = processedImage.at<cv::Vec3b>(i, j)[1];
            image[j + processedImage.cols*i + processedImage.cols*processedImage.rows*2] = processedImage.at<cv::Vec3b>(i, j)[2];
        }
    }

    // The algorithm will store the final segmentation in a one-dimensional array.
    vl_uint32* segmentation = new vl_uint32[processedImage.rows*processedImage.cols];
    vl_size height = processedImage.rows;
    vl_size width = processedImage.cols;
    vl_size channels = processedImage.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 = 10000.;
    vl_size minRegion = 10;

    vl_slic_segment(segmentation, image, width, height, channels, region, regularization, minRegion);

    // Convert segmentation.
    int** labels = new int*[processedImage.rows];
    for (int i = 0; i < processedImage.rows; ++i) {
        labels[i] = new int[processedImage.cols];

        for (int j = 0; j < processedImage.cols; ++j) {
            labels[i][j] = (int) segmentation[j + processedImage.cols*i];
        }
    }

    int label = 0;
    int labelTop = -1;
    int labelBottom = -1;
    int labelLeft = -1;
    int labelRight = -1;

    for (int i = 0; i < processedImage.rows; i++) {
        for (int j = 0; j < processedImage.cols; j++) {

            label = labels[i][j];

            labelTop = label;
            if (i > 0) {
                labelTop = labels[i - 1][j];
            }

            labelBottom = label;
            if (i < processedImage.rows - 1) {
                labelBottom = labels[i + 1][j];
            }

            labelLeft = label;
            if (j > 0) {
                labelLeft = labels[i][j - 1];
            }

            labelRight = label;
            if (j < processedImage.cols - 1) {
                labelRight = labels[i][j + 1];
            }

            if (label != labelTop || label != labelBottom || label!= labelLeft || label != labelRight) {
                processedImage.at<cv::Vec3b>(i, j)[0] = 0;
                processedImage.at<cv::Vec3b>(i, j)[1] = 0;
                processedImage.at<cv::Vec3b>(i, j)[2] = 255;
            }
        }
    }
    showImage(processedImage);
}
Esempio n. 2
0
void
mexFunction(int nout, mxArray *out[],
            int nin, const mxArray *in[])
{
  enum {IN_IMAGE, IN_REGIONSIZE, IN_REGULARIZER, IN_END} ;
  enum {OUT_SEGMENTATION = 0} ;

  int            verbose = 0 ;
  int            opt ;
  int            next = IN_END ;
  mxArray const *optarg ;

  float const * image ;
  vl_size width ;
  vl_size height ;
  vl_size numChannels ;
  vl_size regionSize ;
  double regularizer ;
  vl_uint32 * segmentation ;
  int minRegionSize = -1 ;

  VL_USE_MATLAB_ENV ;

  /* -----------------------------------------------------------------
   *                                               Check the arguments
   * -------------------------------------------------------------- */

  if (nin < 3) {
    vlmxError(vlmxErrInvalidArgument,
              "At least three arguments are required.") ;
  } else if (nout > 1) {
    vlmxError(vlmxErrInvalidArgument,
              "Too many output arguments.");
  }

  image = mxGetData(IN(IMAGE)) ;
  if (!mxIsNumeric(IN(IMAGE)) || mxIsComplex(IN(IMAGE))) {
    vlmxError(vlmxErrInvalidArgument, "IMAGE is not a real matrix.") ;
  }
  if (mxGetClassID(IN(IMAGE)) != mxSINGLE_CLASS) {
    vlmxError(vlmxErrInvalidArgument, "IMAGE is not of class SINGLE.") ;
  }
  if (mxGetNumberOfDimensions(IN(IMAGE)) > 3) {
    vlmxError(vlmxErrInvalidArgument, "IMAGE has more than three dimensions.") ;
  }

  width = mxGetDimensions(IN(IMAGE))[1] ;
  height = mxGetDimensions(IN(IMAGE))[0] ;
  if (mxGetNumberOfDimensions(IN(IMAGE)) == 2) {
    numChannels = 1 ;
  } else {
    numChannels = mxGetDimensions(IN(IMAGE))[2] ;
  }

  if (!vlmxIsPlainScalar(IN(REGIONSIZE))) {
    vlmxError(vlmxErrInvalidArgument, "REGIONSIZE is not a plain scalar.") ;
  }
  regionSize = mxGetScalar(IN(REGIONSIZE)) ;
  if (regionSize < 1) {
    vlmxError(vlmxErrInvalidArgument, "REGIONSIZE=%d is smaller than one.", regionSize) ;
  }

  if (!vlmxIsPlainScalar(IN(REGULARIZER))) {
    vlmxError(vlmxErrInvalidArgument, "REGULARIZER is not a plain scalar.") ;
  }
  regularizer = mxGetScalar(IN(REGULARIZER)) ;
  if (regularizer < 0) {
    vlmxError(vlmxErrInvalidArgument, "REGULARIZER=%g is smaller than one.", regularizer) ;
  }

  while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
    switch (opt) {
      case opt_verbose :
        ++ verbose ;
        break ;
      case opt_min_segment_size :
        if (!vlmxIsPlainScalar(optarg)) {
          vlmxError(vlmxErrInvalidArgument, "MINREGIONSIZE is not a plain scalar.") ;
        }
        minRegionSize = mxGetScalar(optarg) ;
        if (minRegionSize < 0) {
          vlmxError(vlmxErrInvalidArgument, "MINREGIONSIZE=%d is smaller than zero.", minRegionSize) ;
        }
        break ;
    }
  }

  if (minRegionSize < 0) {
    minRegionSize = (regionSize * regionSize) / (6*6) ;
  }

  if (verbose) {
    mexPrintf("vl_slic: image = [%d x %d x %d]\n",
              width, height, numChannels) ;
    mexPrintf("vl_slic: regionSize = %d\n", regionSize) ;
    mexPrintf("vl_slic: regularizer = %g\n", regularizer) ;
    mexPrintf("vl_slic: minRegionSize = %d\n", minRegionSize) ;
  }

  /* -----------------------------------------------------------------
   *                                                           Do work
   * -------------------------------------------------------------- */

  OUT(SEGMENTATION) = mxCreateNumericMatrix((mwSize)height, (mwSize)width, mxUINT32_CLASS, mxREAL) ;
  segmentation = mxGetData(OUT(SEGMENTATION)) ;

  vl_slic_segment(segmentation,
                  image, height, width, numChannels, /* the image is transposed */
                  regionSize, regularizer, minRegionSize) ;
}