Example #1
0
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]){
    double* input_image = (double*)(mxGetData(prhs[0]));
    const mwSize* image_size = mxGetDimensions(prhs[0]);
    size_t height = image_size[0];
    size_t width = image_size[1];
    size_t dims = mxGetNumberOfDimensions(prhs[0]);

    double sigma = mxGetScalar(prhs[1]);
    double min_size = mxGetScalar(prhs[2]);
    double sp_num = mxGetScalar(prhs[3]);

    image<rgb>* new_image = new image<rgb>(width, height);
    for(size_t i = 0;i < height;i++){
        for(size_t j = 0;j < width;j++){
            rgb color;
            color.r = input_image[i + height * (j + width * 0)];
            color.g = input_image[i + height * (j + width * 1)];
            color.b = input_image[i + height * (j + width * 2)];
            new_image->access[i][j] = color;
        }
    }

    int num_ccs; 
    // image<int>* pixel_label = new image<int>(width,height);    
    plhs[0] = mxCreateDoubleMatrix(height,width,mxREAL);
    double* pixel_label = (double*)mxGetPr(plhs[0]);
    segment_image(new_image, sigma, sp_num, min_size, &num_ccs, pixel_label);
}
Example #2
0
  cv::Mat& segment(const cv::Mat& input, float sigma, float k, int min_size, int *numccs, std::vector<duckietown_msgs::Rect>& rects)
  {
    int w = input.cols;
    int h = input.rows;

    convert_mat2image(input, image_);

    image<rgb> *segmentedImage = segment_image(image_, sigma, k, min_size, numccs, region_rects_);

    convert_image2mat(segmentedImage, segimage_);
    cv::cvtColor(segimage_, segimage_gray_, CV_BGR2GRAY);
    cv::cvtColor(segimage_gray_, segimage_, CV_GRAY2BGR);
    
    rects.resize(region_rects_.size());
    for(int i=0; i<region_rects_.size(); i++)
    {
      ROS_DEBUG_STREAM("[" << i << "]" << region_rects_[i].x << " " << region_rects_[i].y << " " << region_rects_[i].w << " " << region_rects_[i].h);

      // draw green rectangles
      if(draw_rect_)
      {
        cv::rectangle(segimage_, 
          cv::Point(region_rects_[i].x, region_rects_[i].y), 
          cv::Point(region_rects_[i].x + region_rects_[i].w, region_rects_[i].y + region_rects_[i].h), 
          cv::Scalar(0, 255, 0), 1, 8);
      }
      rects[i].x = int32_t(region_rects_[i].x);
      rects[i].y = int32_t(region_rects_[i].y);
      rects[i].w = int32_t(region_rects_[i].w);
      rects[i].h = int32_t(region_rects_[i].h);
    }
    delete segmentedImage;
    return segimage_;
  }
Example #3
0
int main(int argc, char** argv)
{
	if(argc < 2){
		std::cerr << "arg1: imgname" << std::endl;
		return 1;
	}
	cv::Mat src_img = cv::imread(argv[1], 1);
	cv::Mat dst_img;
	int n;
	segment_image(src_img, 0.5, 500, 50, &n, dst_img);
	n = quantize<int>(dst_img, dst_img);
	
	cv::RNG rng(1);
	std::vector<cv::Vec3b> colors(n);
	for(auto& color : colors){
		color[0] = rng.uniform(0,255);
		color[1] = rng.uniform(0,255);
		color[2] = rng.uniform(0,255);
	}
	cv::Mat dsp_img(dst_img.size(), CV_8UC3);
	dst_img.forEach<int>([&](int idx, const int* p){
		dsp_img.at<cv::Vec3b>(p) = colors[idx];
	});
	
	cv::imshow("result", dsp_img);
	cv::waitKey(0);
	return 0;
}
Example #4
0
int SegmentImpl::process(const SegmentParameters &params,
                         Raster &raster)
{
  raster.scatterHistogram();
  raster.resetMarker();                     // create empty marker
                                            // must be called as the last one
                                            // before true segmentation!
  return segment_image(raster, params, _h); // process image
};
Example #5
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    float sigma, k;
    int min_size;

    //r = mxGetPr( prhs[0] );
    //g = mxGetPr( prhs[1] );
    //b = mxGetPr( prhs[3] );
    double *image = mxGetPr( prhs[0] );
    const mwSize *dims = mxGetDimensions( prhs[0] );

    sigma = mxGetScalar( prhs[1] );
    k = mxGetScalar( prhs[2] );
    min_size = mxGetScalar( prhs[3] );

    //mexPrintf( "sigma: %.3f, k: %.3f, min_size: %d\n", sigma, k, min_size );

    int height = dims[0];
    int width = dims[1];
    int c = dims[2];

    typedef unsigned char uchar;
    imageRGB *input = new imageRGB(width, height);
    for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
                {
                    int index = height * x + y;
                    imRef(input, x, y).r = static_cast<uchar>( image[index] );
                    imRef(input, x, y).g = static_cast<uchar>( image[width*height + index] );
                    imRef(input, x, y).b = static_cast<uchar>( image[width*height*2 + index] );
                }
        }

    int num_ccs;
    imageRGB *seg = segment_image(input, sigma, k, min_size, &num_ccs);
    // printf("sigma %.2f k %.2f min_size %d\n", sigma, k, min_size);
    // mexPrintf( "number of regions: %d\n", num_ccs);

    plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
    uchar *output = static_cast<uchar*>( mxGetData(plhs[0]) );

    for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
                {
                    int index = height*x + y;
                    output[index] = imRef(seg, x, y).r;
                    output[width * height + index] = imRef(seg, x, y).g;
                    output[2 * width * height + index] = imRef(seg, x, y).b;
                }
        }

    delete input;
    delete seg;
}
Example #6
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  if (nrhs != 4 || nlhs != 1) {
	mexErrMsgTxt("usage: segs = segment(img,sigma, k, min)\n");
  }
  
  if (mxGetClassID(prhs[0]) != mxUINT8_CLASS || mxGetNumberOfDimensions(prhs[0]) != 3) {
    mexErrMsgTxt("'img' should be a 3D array of type UINT8.");  
  }

  float sigma = mxGetScalar(prhs[1]);
  float k = mxGetScalar(prhs[2]);
  int   min_size = mxGetScalar(prhs[3]);
  
  const mwSize *d = mxGetDimensions(prhs[0]);
  int h = d[0];
  int w = d[1];
  unsigned char *ptr = (unsigned char*)mxGetPr(prhs[0]);
  	
  printf("%dx%d image.\n", w, h); 
  image<rgb> input(w, h, false);
  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
    	input.access[y][x].r = ptr[x * h + y            ];
    	input.access[y][x].g = ptr[x * h + y + w * h    ];
    	input.access[y][x].b = ptr[x * h + y + w * h * 2];    	
    }
  }
	
  printf("processing\n");
  int num_ccs; 
  image<rgb> *seg = segment_image(&input, sigma, k, min_size, &num_ccs); 
  printf("got %d components\n", num_ccs);
  printf("done! uff...thats hard work.\n");
  
  mwSize dim[3];
  dim[0] = seg->height();
  dim[1] = seg->width();
  dim[2] = 3;
  plhs[0] = mxCreateNumericArray(3, dim, mxUINT8_CLASS, mxREAL);
  ptr = (unsigned char*)mxGetPr(plhs[0]);
  for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {    
    	ptr[x * h + y            ] = seg->access[y][x].r;
    	ptr[x * h + y + w * h    ] = seg->access[y][x].g;
    	ptr[x * h + y + w * h * 2] = seg->access[y][x].b;    	
    }
  }
}
//--------------------------------------------------------------
ofPixels& ofxImageSegmentation::segment(ofPixels& image){

	if(!image.isAllocated()){
		ofLogError("ofxImageSegmentation::segment") << "input image must be allocated";
		return segmentedPixels;
	}
	
	if(!segmentedPixels.isAllocated() || 
		segmentedPixels.getWidth() != image.getWidth() ||
		segmentedPixels.getHeight() != image.getHeight() ||
		segmentedPixels.getImageType() != image.getImageType() )
	{
		segmentedPixels.allocate(image.getWidth(), image.getHeight(), OF_IMAGE_COLOR);
		segmentedMasks.clear();
	}

	image11<rgb> *input = loadPixels(image);	
	image11<rgb> *seg;
	image11<char> **masks;
	numSegments = segment_image(input, sigma, k, min, seg, masks); 
	memcpy(segmentedPixels.getPixels(),seg->data,segmentedPixels.getWidth()*segmentedPixels.getHeight()*segmentedPixels.getBytesPerPixel());
	
	//calculate segment masks
	if(numSegments > 0){
		while(segmentedMasks.size() < numSegments){
			segmentedMasks.push_back(ofPixels());
			segmentedMasks.back().allocate(image.getWidth(), image.getHeight(), OF_IMAGE_GRAYSCALE);
		}
		int bytesPerMask = segmentedMasks[0].getWidth()*segmentedMasks[0].getHeight()*segmentedMasks[0].getBytesPerPixel();
		for(int i = 0; i < numSegments; i++){
			memcpy(segmentedMasks[i].getPixels(),masks[i]->data,bytesPerMask);
		}
	}

	//This is really slow to do, find a way to preserve memory
	delete input;
	delete seg;
	for(int i = 0; i < numSegments; i++){
		delete masks[i];
	}
	delete [] masks;

	return segmentedPixels;
}
Example #8
0
cv::Mat Egbis::update(cv::Mat matHandle, double dblSigma, double dblK, int intMin, int* intSegments) {
	cv::Mat matOutput = cv::Mat();

	{
		image< rgb >* imageHandle = toNative(&matHandle);

		{
			int intDummy = 0;

			if (intSegments == NULL) {
				intSegments = &intDummy;
			}

			matOutput = segment_image(toNative(&matHandle), (float) (dblSigma), (double) (dblK), intMin, intSegments);
		}

		delete imageHandle;
	}

	return matOutput;
}
Example #9
0
int main(int argc, char **argv) {
  if (argc != 6) {
    fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);
    return 1;
  }
  
  float sigma = atof(argv[1]);
  float k = atof(argv[2]);
  int min_size = atoi(argv[3]);
	
  /* printf("loading input image.\n"); */
  image<rgb> *input = loadPPM(argv[4]);
	
  /* printf("processing\n"); */
  int num_ccs; 
  image<rgb> *seg = segment_image(input, sigma, k, min_size, &num_ccs); 
  savePPM(seg, argv[5]);

  /* printf("got %d components\n", num_ccs); */
  /* printf("done! uff...thats hard work.\n"); */

  return 0;
}
Example #10
0
void segment_set_neural_layer (NEURON_LAYER *nl_segment, NEURON_LAYER *nl_image, int center_x, int center_y)
{
	int width  = nl_segment->dimentions.x;
	int height = nl_segment->dimentions.y;
	char *reg_int = region_of_interest (nl_image, center_x, center_y, width, height);
	char *img = segment_image (0.5, 50, 20, reg_int, nl_segment->dimentions.x, nl_segment->dimentions.y);

	int x, y;
	for (y = 0; y < height; y++)
	{
		for (x = 0; x < width; x++) 
		{
			nl_segment->neuron_vector[y * nl_segment->dimentions.x + x].output.ival =
				PIXEL(img[3 * (y * nl_segment->dimentions.x + x) + 0],
				      img[3 * (y * nl_segment->dimentions.x + x) + 1],
				      img[3 * (y * nl_segment->dimentions.x + x) + 2]);
		}
	}
	
	all_outputs_update ();

	free(img);
	free(reg_int);
}
Example #11
0
int FelzenszwalbShell::execute() {
	multi_img::ptr input;
	imginput::ImgInput ii(config.input);

	input = ii.execute();
	if (input->empty()) {
		throw std::runtime_error
				("EdgeDetection::execute: imginput module failed to read image.");
	}
	input->rebuildPixels(false);
	std::pair<cv::Mat1i, seg_felzenszwalb::segmap> result =
		 segment_image(*input, config);

	if (config.verbosity > 0) {	// statistical output
		const segmap &segmap = result.second;
		cv::Mat1i sizes((int)segmap.size(), 1);
		cv::Mat1i::iterator sit = sizes.begin();
		segmap::const_iterator mit = segmap.begin();
		for (; mit != segmap.end(); ++sit, ++mit)
			*sit = (int)mit->size();
		cv::Scalar mean, stddev;
		cv::meanStdDev(sizes, mean, stddev);
		std::cout << "Found " << result.second.size() << " segments"
				  << " of avg. size " << mean[0]
				  << " (± " << stddev[0] << ")." << std::endl;
	}

	Labeling output;
	output.yellowcursor = false;
	output.shuffle = true;
	output.read(result.first, false); // TODO: we get consecutive index now!

	std::string output_name = config.output_file;
	cv::imwrite(output_name, output.bgr());
	return 0;
}
	int CSkinFeatureExtractor::segment(const IRgbImage &input, Segments &output) const
	{
		return segment_image(&input, output, 0.5, 1000, 100);
	}
Example #13
0
bool CJBig2File::MemoryToJBig2(unsigned char* pBufferBGRA ,int BufferSize, int nWidth, int nHeight, std::wstring sDstFileName)
{
	// check for valid input parameters

///////////////////////////////////////////////////////////
	if ( NULL == pBufferBGRA )	return false;

	int lBufferSize   = BufferSize;
	unsigned char *pSourceBuffer = pBufferBGRA;

	PIX  *pSource = pixCreate( nWidth, nHeight, 32 );
	if ( !pSource )	return false;

	for ( int nY = 0; nY < nHeight; nY++ )
	{
		for ( int nX = 0; nX < nWidth; nX++, pSourceBuffer += 3 )//todooo сделать 3 ? 4
		{
			pixSetRGBPixel( pSource, nX, nY, pSourceBuffer[ 2 ], pSourceBuffer[ 1 ], pSourceBuffer[ 0 ] );
		}
	}


	jbig2ctx *pContext = jbig2_init( m_dTreshold, 0.5, 0, 0, ! m_bPDFMode,  m_bRefine ? 10 : -1 );

	// Пока сделаем запись одной картинки в JBig2
	// TO DO: надо будет сделать запись нескольких картинок в 1 JBig2 файл

	// Убираем ColorMap
	PIX *pPixL = NULL;
	if ( NULL == ( pPixL = pixRemoveColormap( pSource, REMOVE_CMAP_BASED_ON_SRC ) ) ) 
	{
		pixDestroy( &pSource );
		jbig2_destroy( pContext );
		return false;
	}
	pixDestroy( &pSource );

	PIX *pPixT = NULL;
	if ( pPixL->d > 1 ) 
	{
		PIX *pGray = NULL;

		if ( pPixL->d > 8 ) 
		{
			pGray = pixConvertRGBToGrayFast( pPixL );
			if ( !pGray )
			{
				pixDestroy( &pSource );
				jbig2_destroy( pContext );
				return false;
			}
		} 
		else 
		{
			pGray = pixClone( pPixL );
		}

		if (  m_bUpscale2x ) 
		{
			pPixT = pixScaleGray2xLIThresh( pGray,  m_nBwTreshold );
		} 
		else if (  m_bUpscale4x ) 
		{
			pPixT = pixScaleGray4xLIThresh( pGray,  m_nBwTreshold );
		} 
		else 
		{
			pPixT = pixThresholdToBinary( pGray,  m_nBwTreshold );
		}

		pixDestroy( &pGray );
	} 
	else 
	{
		pPixT = pixClone( pPixL );
	}

	if ( m_sOutputTreshold.length() > 0 ) 
	{
		pixWrite( m_sOutputTreshold.c_str(), pPixT, IFF_BMP );
	}

	if (  m_bSegment && pPixL->d > 1 ) 
	{
		PIX *pGraphics = segment_image( pPixT, pPixL );
		if ( pGraphics ) 
		{
			char *sFilename;
			asprintf( &sFilename, "%s.%04d.%s", m_sBaseName.c_str(), 0, ".bmp" );
			pixWrite( sFilename, pGraphics, IFF_BMP );
			free( sFilename );
		} 
		if ( !pPixT ) 
		{
			// Ничего не делаем
			return true;
		}
	}

	pixDestroy( &pPixL );

	if ( !m_bSymbolMode ) 
	{
		int nLength = 0;
		uint8_t *pBuffer = jbig2_encode_generic( pPixT, !m_bPDFMode, 0, 0, m_bDuplicateLineRemoval, &nLength );

		bool bRes = true;
        NSFile::CFileBinary file;
        if (file.CreateFileW(sDstFileName ) == true )
        {
            file.WriteFile(pBuffer, nLength);
            file.CloseFile();
			bRes = true;
        }
		else
			bRes = false;

		pixDestroy( &pPixT );
		if ( pBuffer ) free( pBuffer );
		jbig2_destroy( pContext );

		return bRes;
	}

	int nNumPages = 1;
	jbig2_add_page( pContext, pPixT );
	pixDestroy( &pPixT );

	int nLength = 0;
	uint8_t *pBuffer = jbig2_pages_complete( pContext, &nLength );
	if ( !pBuffer )
	{
		jbig2_destroy( pContext );
		return false;
	}

	if ( m_bPDFMode ) 
	{
		std::wstring sFileName = sDstFileName;//m_sBaseName + _T(".sym");

        NSFile::CFileBinary file;
        if ( file.CreateFileW(sFileName) == false)
		{
			free( pBuffer );
			jbig2_destroy( pContext );
			return false;
		}
        file.WriteFile( pBuffer, nLength );
        file.CloseFile();
	}
	free( pBuffer );

	for ( int nIndex = 0; nIndex < nNumPages; ++nIndex ) 
	{
		pBuffer = jbig2_produce_page( pContext, nIndex, -1, -1, &nLength );
		if ( m_bPDFMode ) 
		{
            std::wstring sFileName = m_sBaseName + L".0000";

            NSFile::CFileBinary file;
            if ( file.CreateFileW(sFileName) ==false)
            {
				free( pBuffer );
				jbig2_destroy( pContext );
				return false;
			}
            file.WriteFile( pBuffer, nLength );
            file.CloseFile();
		} 
		free( pBuffer );
	}

	jbig2_destroy( pContext );

	return true;
}
Example #14
0
int main(int argc, char** argv) {
	if (argc != 11) {
		printf("%s c c_reg min sigma hie_num start_fr end_fr input output\n", argv[0]);
		printf("       c --> value for the threshold function in over-segmentation\n");
		printf("   c_reg --> value for the threshold function in hierarchical region segmentation\n");
		printf("     min --> enforced minimum supervoxel size\n");
		printf("   sigma --> variance of the Gaussian smoothing.\n");
		printf("   range --> number of frames as one subsequence (k in the paper)\n");
		printf(" hie_num --> desired number of hierarchy levels\n");
		printf(" start_fr --> starting frame index\n");
		printf(" end_fr --> end frame index\n");
		printf("   input --> input path of ppm video frames\n");
		printf("  output --> output path of segmentation results\n");
		return 1;
	}

	// Read Parameters
	float c = (float)atof(argv[1]);
	float c_reg = (float)atof(argv[2]);
	int min_size = atoi(argv[3]);
	float sigma = (float)atof(argv[4]);
	int range = atoi(argv[5]);
	int hie_num = atoi(argv[6]);
	int start_frame_num = atoi(argv[7]);
	int end_frame_num = atoi(argv[8]);
	char* input_path = argv[9];
	char* output_path = argv[10];
	if (c <= 0 || c_reg < 0 || min_size < 0 || sigma < 0 || hie_num < 0) {
		fprintf(stderr, "Unable to use the input parameters.");
		return 1;
	}

	//subarna: optical flow
	double alpha = 0.012;
	double ratio = 0.75;
	int minWidth = 20;
	int nOuterFPIterations = 7;
	int nInnerFPIterations = 1;
	int nSORIterations = 30;

	// count files in the input directory
	/*int frame_num = 0;
	struct dirent* pDirent;
	DIR* pDir;
	pDir = opendir(input_path);
	if (pDir != NULL) {
		while ((pDirent = readdir(pDir)) != NULL) {
			int len = strlen(pDirent->d_name);
			if (len >= 4) {
				if (strcmp(".ppm", &(pDirent->d_name[len - 4])) == 0)
					frame_num++;
			}
		}
	}
	//http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200%28v=vs.85%29.aspx
	if (frame_num == 0) {
		fprintf(stderr, "Unable to find video frames at %s", input_path);
		return 1;
	}
	printf("Total number of frames in fold is %d\n", frame_num);

	// check if the range is right
	if (range > frame_num)
		range = frame_num;
	if (range < 1)
		range = 1;
	*/
	
	// check if the range is right
	if (range > (end_frame_num - start_frame_num))
		range = (end_frame_num - start_frame_num);
	if (range < 1)
		range = 1;

# ifdef USE_OPTICAL_FLOW
	if (range < 2)
	{
		range = 2;
		printf("range value should at least be 2 if motion values are to be used\n");
		printf("making the range value 2");
		if ( (end_frame_num - start_frame_num) < 2 )
		{
			printf("\n Not enough frames ... exiting\n\n\n\n");
			exit(1);
		}
	}
# endif

	// make the output directory
	struct stat st;
	int status = 0;
	char savepath[1024];
  	sprintf(savepath, "%s",output_path);
	if (stat(savepath, &st) != 0) {
		/* Directory does not exist */
		if (mkdir(savepath/*, S_IRWXU*/) != 0) {
			status = -1;
		}
	}
	for (int i = 0; i <= hie_num; i++) {
  		sprintf(savepath, "%s/%02d",output_path,i);
		if (stat(savepath, &st) != 0) {
			/* Directory does not exist */
			if (_mkdir(savepath/*, S_IRWXU*/) != 0) { //subarna: _mkdir
				status = -1;
			}
		}
	}
	if (status == -1) {
		fprintf(stderr,"Unable to create the output directories at %s",output_path);
		return 1;
	}
 
	// Initialize Parameters
	int last_clip = (end_frame_num - start_frame_num + 1) % range;
	int num_clip = (end_frame_num - start_frame_num + 1) / range;
	char filepath[1024];
	universe** u = new universe*[num_clip + 1];

	image<rgb>** input_first = new image<rgb>*[range];
	image<rgb>** input_middle = new image<rgb>*[range + 1];
	image<rgb>** input_last = new image<rgb>*[last_clip + 1];

	//subarna
# ifdef LUV
	DImage** input_first_LUV = new DImage*[range];
	DImage** input_middle_LUV = new DImage*[range+1];
	DImage** input_last_LUV = new DImage*[last_clip+1];
# endif

	// Time Recorder
	time_t Start_t, End_t;
	int time_task;
	Start_t = time(NULL);

	int height, width;
	
	// clip 1
	//calculate pair-wise optical flow
	//initialize memory and first frame
	printf("processing subsequence -- 0\n");
	for (int j = 0; j < range; j++) {
		sprintf(filepath, "%s/%05d.ppm", input_path, j + 1+ start_frame_num);
		input_first[j] = loadPPM(filepath);
		
		printf("load --> %s\n", filepath);

		if (j == 0 )
		{
			height = input_first[0]->height();
			width = input_first[0]->width();
		}

# ifdef LUV
		input_first_LUV[j] = new DImage (width, height,3);
		int m = 0;
		//convert to LUV and then apply bilateral filter
		for (int s = 0; s < height*width; s++)
		{     
			double x1,y1,z1;
			double x2,y2,z2;
			ccRGBtoXYZ(input_first[j]->data[s].r, input_first[j]->data[s].g, input_first[j]->data[s].b, &x1, &y1, &z1);
			ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
			input_first_LUV[j]->pData[m] = x2, 
			input_first_LUV[j]->pData[m+1] = y2, 
			input_first_LUV[j]->pData[m+2] = z2;
			m = m+3;
		}
		// pass input_first_LUV
# endif

	}

#ifdef USE_OPTICAL_FLOW
	DImage** D_vx_motion_first = new DImage*[range];
	DImage** D_vx_motion_middle = new DImage*[range + 1];
	DImage** D_vx_motion_last = new DImage*[last_clip + 1];

	DImage** D_vy_motion_first = new DImage*[range];
	DImage** D_vy_motion_middle = new DImage*[range + 1];
	DImage** D_vy_motion_last = new DImage*[last_clip + 1];
# endif

# ifdef USE_OPTICAL_FLOW
	DImage** D_input_first = new DImage*[range];
	DImage** D_input_middle = new DImage*[range + 1];
	DImage** D_input_last = new DImage*[last_clip + 1];

	for (int i = 0; i < range; i++ )
	{
		D_input_first[i] = new DImage(width,height,3);
		D_vx_motion_first[i] = new DImage (width,height);
		D_vy_motion_first[i] = new DImage (width,height);
	}
	image<rgb>* motion_buffer = NULL;
	DImage* D_motion_buffer = new DImage(width,height,3);

	//initialize
	int m = 0;
	int m1 = 0;
	for(int k1= 0; k1 < width; k1++)
	{
		for (int k2 = 0; k2 < height; k2++)
		{
			D_input_first[0]->pData[m1] = input_first[0]->data[m].r;
			D_input_first[0]->pData[m1+1] = input_first[0]->data[m].g;
			D_input_first[0]->pData[m1+2] = input_first[0]->data[m].b;
			m++;
			m1=m1+3;
		}
	}
	D_input_first[0]->im2double();

	for (int j = 0; j < range-1; j++) {
		// initialization for consecutive frames
		m = 0;	
		m1 = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_first[j+1]->pData[m1] = input_first[j+1]->data[m].r;
				D_input_first[j+1]->pData[m1+1] = input_first[j+1]->data[m].g;
				D_input_first[j+1]->pData[m1+2] = input_first[j+1]->data[m].b;
				m++;
				m1=m1+3;
			}
		}
		D_input_first[j+1]->im2double();

		OpticalFlow::Coarse2FineTwoFrames(*D_input_first[j], *D_input_first[j+1], &D_vx_motion_first[j], &D_vy_motion_first[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
		
# ifdef DUMP_FLOW
		//debug: to be deleted
		image<rgb>* test_out = new image<rgb>(width, height);
		int m = 0;	
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
				//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
				//test_out->data[m].b = 0 ; //test_out->data[m].r;

				test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_first[j]->pData[m])));
				test_out->data[m].g = 50 + 30*((int)(fabs(D_vy_motion_first[j]->pData[m])));
				test_out->data[m].b = 0 ; //test_out->data[m].r;
				
				m++;
			}
		}
		sprintf(filepath,"%s/motion/%05d.ppm",output_path, j + 1+ start_frame_num);
		savePPM(test_out, filepath); //"out_test/test1.ppm"
		delete test_out;
# endif

	}
	delete input_first[0];
	sprintf(filepath, "%s/%05d.ppm", input_path, range +1 + start_frame_num);
	input_first[0] = loadPPM(filepath);
	m = 0;	
	m1 = 0;
	for(int k1= 0; k1 < width; k1++)
	{
		for (int k2 = 0; k2 < height; k2++)
		{
			D_motion_buffer->pData[m1] = input_first[0]->data[m].r;
			D_motion_buffer->pData[m1+1] = input_first[0]->data[m].g;
			D_motion_buffer->pData[m1+2] = input_first[0]->data[m].b;
			m++;
			m1=m1+3;
		}
	}
	// dummy place holder
	D_motion_buffer->im2double();
	OpticalFlow::Coarse2FineTwoFrames(*D_input_first[range-2], *D_motion_buffer, &D_vx_motion_first[range-1], &D_vy_motion_first[range-1], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
	
# if 0
	//copy content from D_motion_first[j-1] to D_motion_first[range-1]
	D_vx_motion_first[range-1]->copyData(*D_vx_motion_first[range-2]);
	D_vy_motion_first[range-1]->copyData(*D_vy_motion_first[range-2]);
# endif
	///////////////////////////////////////
# endif

# ifdef USE_OPTICAL_FLOW
	// frame index starts from 0
	# ifndef LUV
		u[0] = segment_image(output_path, input_first, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# else
		u[0] = segment_image_LUV(output_path, input_first_LUV, D_vx_motion_first, D_vy_motion_first, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# endif
# else
	// frame index starts from 0
	# ifndef LUV
		u[0] = segment_image(output_path, input_first,0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# else
		u[0] = segment_image_LUV(output_path, input_first_LUV, 0, range - 1, c, c_reg, min_size,
				sigma, hie_num, NULL,start_frame_num);
	# endif
# endif

	for (int j = 0; j < range; j++) {
		delete input_first[j];
# ifdef LUV
		delete input_first_LUV[j];
# endif
	}

# ifdef USE_OPTICAL_FLOW  //subarna
	for (int j = 0; j < range; j++) {	

		delete D_vx_motion_first[j];
		delete D_vy_motion_first[j];

		delete D_input_first[j];
	}
# endif

	// clip 2 -- last
	int ii;
	for (ii = 1; ii < num_clip; ii++) 
	{
		printf("processing subsequence -- %d\n", ii);

		for (int j = 0; j < range + 1; j++) 
		{
			sprintf(filepath, "%s/%05d.ppm", input_path, ii * range + j + start_frame_num);
			input_middle[j] = loadPPM(filepath);
			
			printf("load --> %s\n", filepath);

# ifdef LUV
			input_middle_LUV[j] = new DImage (width, height,3);
			int m = 0;
			//convert to LUV and then apply bilateral filter
			for (int s = 0; s < height*width; s++)
			{     
				double x1,y1,z1;
				double x2,y2,z2;
				ccRGBtoXYZ(input_middle[j]->data[s].r, input_middle[j]->data[s].g, input_middle[j]->data[s].b, &x1, &y1, &z1);
				ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
				input_middle_LUV[j]->pData[m] = x2, 
				input_middle_LUV[j]->pData[m+1] = y2, 
				input_middle_LUV[j]->pData[m+2] = z2;
				m = m+3;
			}
			//pass input_middle_LUV
# endif
		}

# ifdef USE_OPTICAL_FLOW
		for (int i = 0; i < range+1; i++ )
		{
			D_input_middle[i] = new DImage(width,height,3);
			D_vx_motion_middle[i] = new DImage(width,height);
			D_vy_motion_middle[i] = new DImage(width,height);
		}
		//initialize 
		m = 0;
		m1 = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_middle[0]->pData[m1] = input_middle[0]->data[m].r;
				D_input_middle[0]->pData[m1+1] = input_middle[0]->data[m].g;
				D_input_middle[0]->pData[m1+2] = input_middle[0]->data[m].b;
				m++;
				m1=m1+3;
			}
		}
		D_input_middle[0]->im2double();
		//calculate pair-wise optical flow 
		for (int j = 0; j < range; j++) {
			// initialization for consecutive frames
			m = 0;	
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_input_middle[j+1]->pData[m1] = input_middle[j+1]->data[m].r;
					D_input_middle[j+1]->pData[m1+1] = input_middle[j+1]->data[m].g;
					D_input_middle[j+1]->pData[m1+2] = input_middle[j+1]->data[m].b;
					m++;
					m1 =m1+3;
				}
			}
			D_input_middle[j+1]->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[j], *D_input_middle[j+1], &D_vx_motion_middle[j], &D_vy_motion_middle[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
		
# ifdef DUMP_FLOW
			//debug: to be deleted
			image<rgb>* test_out = new image<rgb>(width, height);
			int m = 0;	
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].b = 0 ; //test_out->data[m].r;

					test_out->data[m].r = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
					test_out->data[m].g = 50 + 40*((int)(fabs(D_vx_motion_middle[j]->pData[m])));
					test_out->data[m].b = 0 ; //test_out->data[m].r;
				
					m++;
				}
			}
			sprintf(filepath,"%s/motion/%05d.ppm",output_path, i * range + j + start_frame_num);
			savePPM(test_out, filepath); //"out_test/test1.ppm"
			delete test_out;
# endif

		}
				
		// subarna: fix crash for specific frame number case
		if ( (ii == num_clip - 1 ) && (last_clip == 0) )
		{
			// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					D_vx_motion_middle[range]->pData[i*width + j] = -D_vx_motion_middle[range-1]->pData[i*width + j];
					D_vy_motion_middle[range]->pData[i*width + j] = -D_vy_motion_middle[range-1]->pData[i*width + j];
				}
			}
		}
		else
		{
			delete input_middle[0];
			sprintf(filepath, "%s/%05d.ppm", input_path, (ii+1) * range +1 + start_frame_num);
			input_middle[0] = loadPPM(filepath);		

			m = 0;	
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_motion_buffer->pData[m1] = input_middle[0]->data[m].r;
					D_motion_buffer->pData[m1+1] = input_middle[0]->data[m].g;
					D_motion_buffer->pData[m1+2] = input_middle[0]->data[m].b;
					m++;
					m1=m1+3;
				}
			}
			
			// dummy place holder
			D_motion_buffer->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_middle[range], *D_motion_buffer, &D_vx_motion_middle[range], &D_vy_motion_middle[range], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);
			/////////////////////////////////////
		}
# endif


# ifdef USE_OPTICAL_FLOW
	# ifndef LUV
		u[ii] = segment_image(output_path, input_middle, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
				ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
				u[ii - 1], start_frame_num);
	# else
			u[ii] = segment_image_LUV(output_path, input_middle_LUV, D_vx_motion_middle, D_vy_motion_middle, ii * range - 1,
					ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
					u[ii - 1],start_frame_num);
	# endif
# else
	# ifndef LUV
		u[ii] = segment_image(output_path, input_middle, ii * range - 1,
				ii * range + range - 1, c, c_reg, min_size, sigma, hie_num,
				u[ii - 1], start_frame_num);
	# else
			u[ii] = segment_image_LUV(output_path, input_middle_LUV, ii * range - 1,
					ii* range + range - 1, c, c_reg, min_size, sigma, hie_num,
					u[ii - 1],start_frame_num);
	# endif
# endif

		if ( u[ii-1] ) delete u[ii - 1];

		////////////////////
		for (int j = 0; j < range + 1; j++) 
		{
			delete input_middle[j];			
# ifdef LUV
			delete input_middle_LUV[j];
# endif
		}

		# ifdef USE_OPTICAL_FLOW
		for (int j = 0; j < range + 1; j++) 
		{

			delete D_vx_motion_middle[j];
			delete D_vy_motion_middle[j];
			delete D_input_middle[j];
		}
		# endif
	}

	// clip last
	if (last_clip > 0) {
		printf("processing subsequence -- %d\n", num_clip);

		for (int j = 0; j < last_clip + 1; j++) 
		{
			sprintf(filepath, "%s/%05d.ppm", input_path, num_clip * range + j + start_frame_num);

			input_last[j] = loadPPM(filepath);

			printf("load --> %s\n", filepath);

# ifdef LUV
			input_last_LUV[j] = new DImage (width, height,3);
			int m=0;
			for (int s = 0; s < width*height; s++)
			{     
				double x1,y1,z1;
				double x2,y2,z2;
				ccRGBtoXYZ(input_last[j]->data[s].r, input_last[j]->data[s].g, input_last[j]->data[s].b, &x1, &y1, &z1);
				ccXYZtoCIE_Luv(x1, y1, z1, &x2, &y2, &z2);
				input_last_LUV[j]->pData[m] = x2, 
				input_last_LUV[j]->pData[m+1] = y2, 
				input_last_LUV[j]->pData[m+2] = z2;
				m = m+3;
			}
# endif
		}

# ifdef USE_OPTICAL_FLOW
		//subarna
		for (int i = 0; i < last_clip+1; i++ )
		{
			D_input_last[i] = new DImage(width,height,3);
			D_vx_motion_last[i] = new DImage(width,height);
			D_vy_motion_last[i] = new DImage(width,height);
		}
		//subarna
		//initialize
		m1 = 0;
		m = 0;
		for(int k1= 0; k1 < width; k1++)
		{
			for (int k2 = 0; k2 < height; k2++)
			{
				D_input_last[0]->pData[m1] = input_last[0]->data[m].r;
				D_input_last[0]->pData[m1+1] = input_last[0]->data[m].g;
				D_input_last[0]->pData[m1+2] = input_last[0]->data[m].b;//0
				m++;
				m1=m1+3;
			}
		}
		D_input_last[0]->im2double();
		//calculate pair-wise optical flow 
		for (int j = 0; j < last_clip; j++) {
			// initialization for consecutive frames
			m = 0;
			m1 = 0;
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					D_input_last[j+1]->pData[m1] = input_last[j+1]->data[m].r;
					D_input_last[j+1]->pData[m1+1] = input_last[j+1]->data[m].g;
					D_input_last[j+1]->pData[m1+2] = input_last[j+1]->data[m].b;
					m++;
					m1=m1+3;
				}
			}
			D_input_last[j+1]->im2double();
			OpticalFlow::Coarse2FineTwoFrames(*D_input_last[j], *D_input_last[j+1], &D_vx_motion_last[j], &D_vy_motion_last[j], alpha,ratio,minWidth,nOuterFPIterations,nInnerFPIterations,nSORIterations);

# ifdef DUMP_FLOW
			//debug: to be deleted
			image<rgb>* test_out = new image<rgb>(width, height);
			int m = 0;	
			for(int k1= 0; k1 < width; k1++)
			{
				for (int k2 = 0; k2 < height; k2++)
				{
					//test_out->data[m].r = ((int)(fabs(D_vx_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].g = ((int)(fabs(D_vy_motion_first[j]->pData[m])*30)>>4)<<4;
					//test_out->data[m].b = 0 ; //test_out->data[m].r;

					test_out->data[m].r = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
					test_out->data[m].g = 50 + 30*((int)(fabs(D_vx_motion_last[j]->pData[m])));
					test_out->data[m].b = 0 ; //test_out->data[m].r;
				
					m++;
				}
			}
			sprintf(filepath,"%s/motion/%05d.ppm",output_path, num_clip * range + j + start_frame_num);
			savePPM(test_out, filepath); //"out_test/test1.ppm"
			delete test_out;
# endif
		}

		// for the last frame motion feature -- copy feature value from last frame, but with opposite sign
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				D_vx_motion_last[last_clip]->pData[i*width + j] = -D_vx_motion_last[last_clip-1]->pData[i*width + j];
				D_vy_motion_last[last_clip]->pData[i*width + j] = -D_vy_motion_last[last_clip-1]->pData[i*width + j];
			}
		}
		//////////////////////////////////////
# endif

# ifdef USE_OPTICAL_FLOW
	# ifndef LUV
			u[num_clip] = segment_image(output_path, input_last, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
					hie_num, u[num_clip - 1],start_frame_num);
	# else
			u[num_clip] = segment_image_LUV(output_path, input_last_LUV, D_vx_motion_last,D_vy_motion_last, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
	# endif
# else
	# ifndef LUV
			u[num_clip] = segment_image(output_path, input_last,num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
					hie_num, u[num_clip - 1],start_frame_num);
	# else
			u[num_clip] = segment_image_LUV(output_path, input_last_LUV, num_clip * range - 1,
					num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,hie_num, u[num_clip - 1],start_frame_num);
	# endif
# endif


		if (u[num_clip - 1]) delete u[num_clip - 1];
		delete u[num_clip];

		for (int j = 0; j < last_clip + 1; j++) 
		{
			delete input_last[j];			
# ifdef LUV
			delete input_last_LUV[j];
# endif
		}

		//subarna
# ifdef USE_OPTICAL_FLOW
		for (int j = 0; j < last_clip + 1; j++) {

			delete D_vx_motion_last[j];
			delete D_vy_motion_last[j];
			delete D_input_last[j];
		}
		delete(D_motion_buffer);
# endif
	}

	//////////////////////////////////////
# ifdef LUV
	delete input_first_LUV;
	delete input_middle_LUV;
	delete input_last_LUV;
# endif

# ifdef USE_OPTICAL_FLOW
	delete D_vx_motion_first;
	delete D_vy_motion_first;
	delete D_vx_motion_middle;
	delete D_vy_motion_middle;
	delete D_vx_motion_last;
	delete D_vy_motion_last;
# endif
	/////////////////////////////////////

	delete[] u;

	// Time Recorder
	End_t = time(NULL);
	time_task = difftime(End_t, Start_t);
	std::ofstream myfile;
	char timefile[1024];
	sprintf(timefile, "%s/%s", output_path, "time.txt");
	myfile.open(timefile);
	myfile << time_task << endl;
	myfile.close();

	printf("Congratulations! It's done!\n");
	printf("Time_total = %d seconds\n", time_task);
	return 0;
}
Example #15
0
int main(int argc, char** argv) {
    if (argc != 9) {
        printf("%s c c_reg min sigma range hie_num input output\n", argv[0]);
        printf("       c --> value for the threshold function in over-segmentation\n");
        printf("   c_reg --> value for the threshold function in hierarchical region segmentation\n");
        printf("     min --> enforced minimum supervoxel size\n");
        printf("   sigma --> variance of the Gaussian smoothing.\n");
        printf("   range --> number of frames as one subsequence (k in the paper)\n");
        printf(" hie_num --> desired number of hierarchy levels\n");
        printf("   input --> input path of ppm video frames\n");
        printf("  output --> output path of segmentation results\n");
        return 1;
    }

    // Read Parameters
    float c = atof(argv[1]);
    float c_reg = atof(argv[2]);
    int min_size = atoi(argv[3]);
    float sigma = atof(argv[4]);
    int range = atoi(argv[5]);
    int hie_num = atoi(argv[6]);
    char* input_path = argv[7];
    char* output_path = argv[8];
    if (c <= 0 || c_reg <= 0 || min_size < 0 || sigma < 0 || hie_num < 0) {
        fprintf(stderr, "Uable to use the input parameters.");
        return 1;
    }
    printf("c %.2f, c_reg %.2f, min_size %d, sigma %.2f, range %d, hie_num %d\n", c, c_reg, min_size, sigma, range, hie_num);

    // count files in the input directory
    int frame_num = 0;
    struct dirent* pDirent;
    DIR* pDir;
    pDir = opendir(input_path);
    if (pDir != NULL) {
        while ((pDirent = readdir(pDir)) != NULL) {
            int len = strlen(pDirent->d_name);
            if (len >= 4) {
                if (strcmp(".ppm", &(pDirent->d_name[len - 4])) == 0)
                    frame_num++;
            }
        }
    }
    if (frame_num == 0) {
        fprintf(stderr, "Unable to find video frames at %s", input_path);
        return 1;
    }
    printf("Total number of frames in fold is %d\n", frame_num);
    // check if the range is right
    if (range > frame_num)
        range = frame_num;
    if (range < 1)
        range = 1;


    // make the output directory
    struct stat st;
    int status = 0;
    char savepath[1024];
    snprintf(savepath,1023,"%s",output_path);
    if (stat(savepath, &st) != 0) {
        /* Directory does not exist */
        if (mkdir(savepath) != 0) {
            status = -1;
        }
    }
    for (int i = 0; i <= hie_num; i++) {
        snprintf(savepath,1023,"%s/%02d",output_path,i);
        if (stat(savepath, &st) != 0) {
            /* Directory does not exist */
            if (mkdir(savepath) != 0) {
                status = -1;
            }
        }
    }
    if (status == -1) {
        fprintf(stderr,"Unable to create the output directories at %s",output_path);
        return 1;
    }

    // Initialize Parameters
    int last_clip = frame_num % range;
    int num_clip = frame_num / range;
    char filepath[1024];
    universe** u = new universe*[num_clip + 1];
    image<rgb>** input_first = new image<rgb>*[range];
    image<rgb>** input_middle = new image<rgb>*[range + 1];
    image<rgb>** input_last = new image<rgb>*[last_clip + 1];

    // Time Recorder
    time_t Start_t, End_t;
    int time_task;
    Start_t = time(NULL);

    // clip 1
    printf("processing subsequence -- 0\n");
    for (int j = 0; j < range; j++) {
        snprintf(filepath, 1023, "%s/%05d.ppm", input_path, j + 1);
        input_first[j] = loadPPM(filepath);
        printf("load --> %s\n", filepath);
    }
    // frame index starts from 0
    u[0] = segment_image(output_path, input_first, 0, range - 1, c, c_reg, min_size,
                         sigma, hie_num, NULL);
    for (int j = 0; j < range; j++) {
        delete input_first[j];
    }

    // clip 2 -- last
    for (int i = 1; i < num_clip; i++) {
        printf("processing subsequence -- %d\n", i);
        for (int j = 0; j < range + 1; j++) {
            snprintf(filepath, 1023, "%s/%05d.ppm", input_path, i * range + j);
            input_middle[j] = loadPPM(filepath);
            printf("load --> %s\n", filepath);
        }
        u[i] = segment_image(output_path, input_middle, i * range - 1,
                             i * range + range - 1, c, c_reg, min_size, sigma, hie_num,
                             u[i - 1]);
        delete u[i - 1];
        for (int j = 0; j < range + 1; j++) {
            delete input_middle[j];
        }
    }

    // clip last
    if (last_clip > 0) {
        printf("processing subsequence -- %d\n", num_clip);
        for (int j = 0; j < last_clip + 1; j++) {
            snprintf(filepath, 1023, "%s/%05d.ppm", input_path, num_clip * range + j);
            input_last[j] = loadPPM(filepath);
            printf("load --> %s\n", filepath);
        }
        u[num_clip] = segment_image(output_path, input_last, num_clip * range - 1,
                                    num_clip * range + last_clip - 1, c, c_reg, min_size, sigma,
                                    hie_num, u[num_clip - 1]);
        delete u[num_clip - 1];
        delete u[num_clip];
        for (int j = 0; j < last_clip + 1; j++) {
            delete input_last[j];
        }
    }

    delete[] u;

    // Time Recorder
    End_t = time(NULL);
    time_task = difftime(End_t, Start_t);
    std::ofstream myfile;
    char timefile[1024];
    snprintf(timefile, 1023, "%s/%s", output_path, "time.txt");
    myfile.open(timefile);
    myfile << time_task << endl;
    myfile.close();

    printf("Time_total = %d seconds\n", time_task);
    return 0;
}