Exemple #1
0
void saveImages(unsigned char *images, int height, int width, int numberOfRepeats, int *labels, int numberOfLabels, std::string output)
{
	srand(time(NULL));
	pixel color[height*width];
	for(int i = 0; i < 255; i++)
			color[i] = randomcolor();

	char fileName[32];
	for(int l = 0; l < numberOfRepeats; l++)
	{
		for(int k = 0; k < numberOfLabels; k++)
		{
			Image<pixel> *im = new Image<pixel>(width, height, true);
			for(int i=0; i < height; i++)
				for(int j=0; j < width; j++)
					im->access[i][j] = color[images[(l * numberOfLabels + k) * height * width + i * width + j]];

			strcpy(fileName, "out");
			sprintf(&fileName[3], "%d-%d", l, labels[k]);
			strcat(fileName, ".ppm");
			std::string outputFile;
			if (output.length() != 0 && output[output.length() - 1] != '/')
			{
				outputFile = output + "/" + fileName;
			}
			else
			{
				outputFile = output + fileName;
			}
			savePPM(im, outputFile.c_str());
		}
	}
}
Exemple #2
0
void Renderer::render() const
{
	Ray		ray;
	P2d		pp;

	Random rnd(3141592);

	C3f *image = new C3f[ _vp.width * _vp.height];
	double invNumSample = 1.0 / _vp.numSamples;

	// OpenMP
	#pragma omp parallel for schedule(dynamic, 1) num_threads(4)
	for (int y = 0; y < _vp.height; y++)
	{
		std::cerr << "Rendering (y = " << y << ") " << (100.0 * y / (_vp.height - 1)) << "%" << std::endl;
		for (int x = 0; x < _vp.width; x++)
		{
			const int imageIndex = (_vp.height - y - 1) * _vp.width + x;
			C3f pixelColor(0.0f, 0.0f, 0.0f);
			for (int j = 0; j < _vp.numSamples; j++)
			{
				pp.x = _vp.s*(- x + 0.5 * _vp.width);
				pp.y = _vp.s*(y - 0.5 * _vp.height);
				ray = _camera->getRay(pp);
				pixelColor = pixelColor + radiance(ray, &rnd, 0) / _vp.numSamples;
			}
			image[imageIndex] = C3f(pixelColor);
		}
	}

	savePPM("output.ppm", image, _vp.width, _vp.height);
	delete[] image;
}
Exemple #3
0
void saveImage(unsigned char *img, int height, int width, char *ouputName)
{
	srand(time(NULL));
	pixel color[height*width];
	for(int i = 0; i < 255; i++)
		color[i] = randomcolor();

	Image<pixel> *output = new Image<pixel>(width, height, true);
	for(int i = 0; i < height; i++)
		for(int j = 0; j < width; j++)
			output->access[i][j] = color[img[i * width + j]];

	savePPM(output, ouputName);
}
Exemple #4
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;
}
/*
 * Save an image to one of the formats implemented by this class
 */
int RImage::save(char file[100]) {
	if (strstr(file, ".ppm") != NULL) {
		savePPM(file);

	} else if (strstr(file, ".pgm") != NULL){
		savePGM(file);

	} else if (strstr(file, ".jpeg") != NULL ||
			strstr(file, ".jpg") != NULL) {
		saveJPEG(file, 100);

	} else if (strstr(file, ".png") != NULL) {
		fprintf(stderr, "ERROR: Format not supported as yet.\n");
		savePNG(file);

	} else if (strstr(file, ".txt") != NULL) {
		saveText(file);

	} else {
		fprintf(stderr, "ERROR: Cannot save image %s. Unsupported format.\n", file);
		exit(1);
	}

}
Exemple #6
0
void saveImages(unsigned char *images, int height, int width, int numberOfRepeats, int *labels, int numberOfLabels)
{
	srand(time(NULL));
	pixel color[height*width];
	for(int i = 0; i < 255; i++)
			color[i] = randomcolor();

	char fileName[32];
	for(int l = 0; l < numberOfRepeats; l++)
	{
		for(int k = 0; k < numberOfLabels; k++)
		{
			Image<pixel> *im = new Image<pixel>(width, height, true);
			for(int i=0; i < height; i++)
				for(int j=0; j < width; j++)
					im->access[i][j] = color[images[(l * numberOfLabels + k) * height * width + i * width + j]];

			strcpy(fileName, "out");
			sprintf(&fileName[3], "%d-%d", l, labels[k]);
			strcat(fileName, ".ppm");
			savePPM(im, fileName);
		}
	}
}
Exemple #7
0
int main(int argc, char** argv)
{
  // Error code returned from openCL calls
  int err;

  // A, B and C arrays
  float *hA = (float *)calloc(LENGTH, sizeof(float));
  float *hB = (float *)calloc(LENGTH, sizeof(float));
  float *hC = (float *)calloc(LENGTH, sizeof(float));

  // Define the scene
  const unsigned int kScreenWidth = 800;
  const unsigned int kScreenHeight = 600;
  float zoomFactor = -4.f;
  float aliasFactor = 3.f;

  size_t globalWorkSize = kScreenWidth * kScreenHeight;
  size_t localWorkSize = 256;

  // Colours
  Vec whiteCol;
  vinit(whiteCol, 8.f, 8.f, 8.f);
  Vec lowerWhite;
  vinit(lowerWhite, 0.5f, 0.5f, 0.5f);
  Vec redCol;
  vinit(redCol, 0.8f, 1.f, 0.7f);
  Vec greenCol;
  vinit(greenCol, 0.4f, 0.5f, 0.7f);
  Vec col1;
  vinit(col1, 0.01f, 0.8f, 0.01f);

  // Setup materials
  struct Material ballMaterial1; // White
  Vec bm1Gloss; vassign(bm1Gloss, redCol);
  Vec bm1Matte; vassign(bm1Matte, greenCol);
  setMatOpacity(&ballMaterial1, 0.8f);
  setMatteGlossBalance(&ballMaterial1, 0.2f, &bm1Matte, &bm1Gloss);
  setMatRefractivityIndex(&ballMaterial1, 1.5500f);

  struct Material ballMaterial2; // Red
  Vec bm2Gloss; vassign(bm2Gloss, redCol);
  Vec bm2Matte; vassign(bm2Matte, greenCol);
  setMatOpacity(&ballMaterial2, 0.3f);
  setMatteGlossBalance(&ballMaterial2, 0.95f, &bm2Matte, &bm2Gloss);
  setMatRefractivityIndex(&ballMaterial2, 1.5500f);

  struct Material ballMaterial3; // Red
  Vec bm3Gloss; vassign(bm3Gloss, col1);
  Vec bm3Matte; vassign(bm3Matte, col1);
  setMatOpacity(&ballMaterial3, 0.6f);
  setMatteGlossBalance(&ballMaterial3, 0.0, &bm3Matte, &bm3Gloss);
  setMatRefractivityIndex(&ballMaterial3, 1.5500f);

  // Setup spheres
  unsigned int sphNum = 3;
  struct Sphere *hSpheres =
    (struct Sphere *)calloc(sphNum, sizeof(struct Sphere));
  hSpheres[0].material = ballMaterial1;
  vinit(hSpheres[0].pos, -9.f, 0.f, -13.f);
  hSpheres[0].radius = 5.f;
  hSpheres[1].material = ballMaterial2;
  vinit(hSpheres[1].pos, -4.f, 1.5f, -5.f);
  hSpheres[1].radius = 2.f;
  hSpheres[2].material = ballMaterial3;
  vinit(hSpheres[2].pos, 1.f, -1.f, -7.f);
  hSpheres[2].radius = 3.f;

  // Setup light sources
  unsigned int lgtNum = 2;
  struct Light *hLights =
    (struct Light *)calloc(lgtNum, sizeof(struct Light));
  vinit(hLights[0].pos, -45.f, 10.f, 85.f);
  vassign(hLights[0].col, lowerWhite);
  vinit(hLights[1].pos, 20.f, 60.f, -5.f);
  vassign(hLights[1].col, lowerWhite);



  // Fill vectors a and b with random float values
  int count = LENGTH;
  for (int i = 0; i < count; i++){
    hA[i] = rand() / (float)RAND_MAX;
    hB[i] = rand() / (float)RAND_MAX;
  }




  cl_uint numPlatforms;

  // Find number of platforms
  err = clGetPlatformIDs(0, NULL, &numPlatforms);
  checkError(err, "Finding platforms");
  if (numPlatforms == 0) {
    printf("Found 0 platforms!\n");
    return EXIT_FAILURE;
  }




  // Get all platforms
  cl_platform_id *platform = (cl_platform_id *)malloc(sizeof(cl_platform_id)* numPlatforms);
  err = clGetPlatformIDs(numPlatforms, platform, NULL);
  checkError(err, "Getting platforms");




  // Define an ID for the device
  cl_device_id deviceId = 0;
  // Secure a GPU
  for (int i = 0; i < numPlatforms; i++) {
    err = clGetDeviceIDs(platform[i], CL_DEVICE_TYPE_GPU, 1, &deviceId, NULL);
    if (err == CL_SUCCESS) {
      break;
    }
  }

  // Once a device has been obtained, print out its info
  err = output_device_info(deviceId);
  checkError(err, "Printing device output");



  // Create a context for the GPU
  cl_context gpuContext;
  gpuContext = clCreateContext(NULL, 1, &deviceId, NULL, NULL, &err);
  checkError(err, "Creating context");




  // Create a command queue
  cl_command_queue commandsGPU;
  commandsGPU = clCreateCommandQueue(gpuContext, deviceId, NULL, &err);
  checkError(err, "Creating command queue");




  // Load the kernel code
  std::ifstream sourceFstream("raytrace_kernel.cl");
  std::string source((std::istreambuf_iterator<char>(sourceFstream)),
    std::istreambuf_iterator<char>());

  // Create a program from the source
  const char* str = source.c_str();
  cl_program program;
  program = clCreateProgramWithSource(gpuContext, 1, &str, NULL, &err);
  checkError(err, "Creating program");




  // Compile the program
  err = clBuildProgram(program, 0, NULL, "-I C:\Drive\Alberto\Projects\Code\C++\raytracer_gamma\raytracer_gamma", NULL, NULL);
    // If there were compilation errors
    if (err != CL_SUCCESS) {
      // Print out compilation log
      size_t len;
      char buffer[2048];

      printf("Error: Failed to build program executable!\n%s\n", err_code(err));
      clGetProgramBuildInfo(program, deviceId, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
      printf("%s\n", buffer);

      // Exit
      return EXIT_FAILURE;
    }




  // Create the kernel
  cl_kernel koRTG;
  koRTG = clCreateKernel(program, "raytrace", &err);
  checkError(err, "Creating kernel");




  // Create the list of spheres and lights in device memory
  cl_mem dSpheres = clCreateBuffer(gpuContext, CL_MEM_READ_WRITE,
    sizeof(struct Sphere) * sphNum, NULL, &err);
  checkError(err, "Creating buffer for spheres");
  cl_mem dLights = clCreateBuffer(gpuContext, CL_MEM_READ_WRITE,
    sizeof(struct Light) * lgtNum, NULL, &err);
  checkError(err, "Creating buffer for lights");
  cl_mem dPixelBuffer = clCreateBuffer(gpuContext, CL_MEM_WRITE_ONLY,
    kScreenWidth * kScreenHeight * sizeof(Vec), NULL, &err);
  checkError(err, "Creating buffer for pixels");

  // Write data from host into device memory (fill the buffers with
  // the host arrays)
  err = clEnqueueWriteBuffer(commandsGPU, dSpheres, CL_TRUE, 0,
    sizeof(struct Sphere) * sphNum, hSpheres, 0, NULL, NULL);
  checkError(err, "Copying hSperes in dSpheres");
  err = clEnqueueWriteBuffer(commandsGPU, dLights, CL_TRUE, 0,
    sizeof(struct Light) * lgtNum, hLights, 0, NULL, NULL);
  checkError(err, "Copying hLights into dLights");

  cl_int   status;
  cl_uint maxDims;
  cl_event events[2];
  size_t maxWorkGroupSize;

  /**
  * Query device capabilities. Maximum
  * work item dimensions and the maximmum
  * work item sizes
  */
  status = clGetDeviceInfo(
	  deviceId,
	  CL_DEVICE_MAX_WORK_GROUP_SIZE,
	  sizeof(size_t),
	  (void*)&maxWorkGroupSize,
	  NULL);
  if (status != CL_SUCCESS)
  {
	  fprintf(stderr, "Error: Getting Device Info. (clGetDeviceInfo)\n");
	  return 1;
  }

  status = clGetDeviceInfo(
	  deviceId,
	  CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
	  sizeof(cl_uint),
	  (void*)&maxDims,
	  NULL);
  if (status != CL_SUCCESS)
  {
	  fprintf(stderr, "Error: Getting Device Info. (clGetDeviceInfo)\n");
	  return 1;
  }

  localWorkSize = maxWorkGroupSize;

  if (globalWorkSize % localWorkSize != 0) {
    globalWorkSize = (globalWorkSize / localWorkSize + 1) * localWorkSize;
  }



  // Set kernel arguments
  err = clSetKernelArg(koRTG, 0, sizeof(cl_mem), &dSpheres);
  err |= clSetKernelArg(koRTG, 1, sizeof(unsigned int), &sphNum);
  err |= clSetKernelArg(koRTG, 2, sizeof(cl_mem), &dLights);
  err |= clSetKernelArg(koRTG, 3, sizeof(unsigned int), &lgtNum);
  err |= clSetKernelArg(koRTG, 4, sizeof(unsigned int), &kScreenWidth);
  err |= clSetKernelArg(koRTG, 5, sizeof(unsigned int), &kScreenHeight);
  err |= clSetKernelArg(koRTG, 6, sizeof(float), &zoomFactor);
  err |= clSetKernelArg(koRTG, 7, sizeof(float), &aliasFactor);
  err |= clSetKernelArg(koRTG, 8, sizeof(cl_mem), &dPixelBuffer);
  err |= clSetKernelArg(koRTG, 9, sizeof(struct Sphere) * sphNum, NULL);
  err |= clSetKernelArg(koRTG, 10, sizeof(struct Light) * lgtNum, NULL);
  checkError(err, "Setting kernel arguments");

  // Start counting the time between kernel enqueuing and completion
  std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();

  // Execute the kernel over the entire range of our 1d input data set
  // letting the OpenCL runtime choose the work-group size
  err = clEnqueueNDRangeKernel(commandsGPU, koRTG, 1, NULL,
    &globalWorkSize, &localWorkSize, 0, NULL, NULL);
  checkError(err, "Enqueueing kernel");

  // Wait for the commands in the queue to be executed
  err = clFinish(commandsGPU);
  checkError(err, "Waiting for commands to finish");

  // Read the time after the kernel has executed
  std::chrono::steady_clock::time_point endTime = std::chrono::steady_clock::now();

  // Compute the duration
  double kernelExecTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();

  

  // Print the duration
  printf("Exec time: %.5f ms", kernelExecTime);



  // Image
  //float *imagePtr = (float *)malloc(globalWorkSize * sizeof(Vec));



  //// Screen in world coordinates
  //const float kImageWorldWidth = 16.f;
  //const float kImageWorldHeight = 12.f;

  //// Amount to increase each step for the ray direction
  //const float kRayXStep = kImageWorldWidth / ((float)kScreenWidth);
  //const float kRayYStep = kImageWorldHeight / ((float)kScreenHeight);
  //const float aspectRatio = kImageWorldWidth / kImageWorldHeight;

  //// Variables holding the current step in world coordinates
  //float rayX = 0.f, rayY = 0.f;

  //int pixelsCounter = 0;

  //// Calculate size of an alias step in world coordinates
  //const float kAliasFactorStepInv = kRayXStep / aliasFactor;
  //// Calculate total size of samples to be taken
  //const float kSamplesTot = aliasFactor * aliasFactor;
  //// Also its inverse
  //const float kSamplesTotinv = 1.f / kSamplesTot;

  //for (int y = 0; y < kScreenWidth * kScreenHeight; ++y, pixelsCounter += 3) {
  //  // Retrieve the global ID of the kernel
  //  const unsigned gid = y;

  //  

  //  // Calculate world position of pixel being currently worked on
  //  const float kPxWorldX = ((((float)(gid % kScreenWidth) - 
  //    (kScreenWidth * 0.5f))) * kRayXStep);
  //  const float kPxWorldY = ((kScreenHeight *0.5f) - ((float)(gid / kScreenWidth))) * kRayYStep;

  //  // The ray to be shot. The vantage point (camera) is at the origin,
  //  // and its intensity is maximum
  //  struct Ray ray; vinit(ray.origin, 0.f, 0.f, 0.f); vinit(ray.intensity, 1.f, 1.f, 1.f);

  //  // The colour of the pixel to be computed
  //  Vec pixelCol = { 0.f, 0.f, 0.f };

  //  // Mock background material
  //  struct Material bgMaterial;
  //  Vec black; vinit(black, 0.f, 0.f, 0.f);
  //  setMatteGlossBalance(&bgMaterial, 0.f, &black, &black);
  //  setMatRefractivityIndex(&bgMaterial, 1.00f);

  //  // For each sample to be taken
  //  for (int i = 0; i < aliasFactor; ++i) {
  //    for (int j = 0; j < aliasFactor; ++j) {
  //      // Calculate the direction of the ray
  //      float x = (kPxWorldX + (float)(((float)j) * kAliasFactorStepInv)) * aspectRatio;
  //      float y = (kPxWorldY + (float)(((float)i) * kAliasFactorStepInv));

  //      // Set the ray's dir and normalise it
  //      vinit(ray.dir, x, y, zoomFactor); vnorm(ray.dir);

  //      // Raytrace for the current sample
  //      Vec currentSampleCol = rayTrace(hSpheres, sphNum, hLights, lgtNum,
  //        ray, bgMaterial, 0);

  //      vsmul(currentSampleCol, kSamplesTotinv, currentSampleCol);

  //      // Compute the average
  //      vadd(pixelCol, pixelCol, currentSampleCol);
  //    }
  //  }

  //  // Write result in destination buffer
  //  *(imagePtr + pixelsCounter) = pixelCol.x;
  //  *(imagePtr + pixelsCounter + 1) = pixelCol.y;
  //  *(imagePtr + pixelsCounter + 2) = pixelCol.z;
  //}

  // Create a buffer to hold the result of the computation on the device
  Vec *pixelsIntermediate = (Vec *)calloc(kScreenHeight * kScreenWidth, sizeof(Vec));
  //Vec *pixelsIntermediate = (Vec *)(imagePtr);

  // Read the results back from the device into the host
  err = clEnqueueReadBuffer(commandsGPU, dPixelBuffer, CL_TRUE, 0,
	  kScreenWidth * kScreenHeight * sizeof(Vec), pixelsIntermediate, 0, NULL, NULL);
  // If the reading operation didn't complete successfully
  if (err != CL_SUCCESS) {
    printf("Error: Failed to read output buffer!\n%s\n", err_code(err));

    // Exit
    exit(1);
  }

  // Calculate the maximum colour value across the whole picture
  float maxColourValue = maxColourValuePixelBuffer(pixelsIntermediate,
    kScreenWidth * kScreenHeight);

  // Cast the buffer to the type accepted by the savePPM function
  RGB *pixels = (RGB *)(pixelsIntermediate);

  // Print execution time
  /*rtime = wtime() - rtime;
  printf("\nThe kernel ran in %lf seconds\n", rtime);*/
 

  // Cleanup
  clReleaseMemObject(dPixelBuffer);
  clReleaseMemObject(dLights);
  clReleaseMemObject(dSpheres);
  clReleaseProgram(program);
  clReleaseKernel(koRTG);
  clReleaseCommandQueue(commandsGPU);
  clReleaseContext(gpuContext);
  // ... Also on host

  free(hLights);
  free(hSpheres);
  free(hA);
  free(hB);
  free(hC);
  free(platform);


  // Try to save a PPM picture
  savePPM(pixels, "testPPM.ppm", kScreenWidth, kScreenHeight, maxColourValue);
  free(pixelsIntermediate);
  //free(imagePtr);
 
  getchar();

  return 0;
}
int main(int argc, char **argv) {
  if (argc != 5) {
    fprintf(stderr, "usage: %s input (without .ppm) output (without .ppm) nbimages ratio \n", argv[0]);
    return 1;
  }
  // (1) variables declarations
  char * imname = new char[100];
  char * outname = new char[100];
  char * appel = new char[1000];

 // (2) reading arguments
  int nb_images = atoi(argv[3]);
  int start=1;
  int i;
  sprintf(imname, "%s0%004d.ppm", argv[1], start);
  sprintf(outname, "%s0%004d.ppm", argv[2], start);
  float ratio = atof(argv[4]);  
  printf("%s\n",imname);
  image<rgb> *input;  
  // (3) loading first image
  printf("loading input image.\n");
  input = loadPPM(imname);
  int width = input->width();
  int height = input->height();
  int N = width*height;
  
  
  
  image<rgb> *output= new image<rgb>((int)(width/ratio), (int)(height/ratio));
  
 for (i=start;i<=nb_images;i++) {
 
   sprintf(imname, "%s0%004d.ppm",argv[1], i); 
   sprintf(outname, "%s0%004d.ppm",argv[2], i);   
   input = loadPPM(imname);
   
   int moy_r, moy_g, moy_b, norm;
   for (int y = 0; y < height/2; y++) {
     for (int x = 0; x < width/2; x++) {
      moy_r = imRef(input, x*2,y*2).r;
      moy_g = imRef(input, x*2,y*2).g;
      moy_b = imRef(input, x*2,y*2).b;
      norm = 1;                      
      if (x*2+1<width) {
	moy_r += imRef(input, x*2+1,y*2).r;
	moy_g += imRef(input, x*2+1,y*2).g;
	moy_b += imRef(input, x*2+1,y*2).b;
	norm++;}                                  
      if (y*2+1<height) {
	moy_r += imRef(input, x*2,y*2+1).r; 
	moy_g += imRef(input, x*2,y*2+1).g; 
	moy_b += imRef(input, x*2,y*2+1).b; 
	norm++;}
      if ((x*2+1<width) && (y*2+1<height)) {
	moy_r +=  imRef(input, x*2+1,y*2+1).r;
	moy_g +=  imRef(input, x*2+1,y*2+1).g;
	moy_b +=  imRef(input, x*2+1,y*2+1).b;
	norm++; }
      imRef(output, x,y).r = moy_r/norm;
      imRef(output, x,y).g = moy_g/norm;
      imRef(output, x,y).b = moy_b/norm;
    }
  }
  
  savePPM(output, outname);

  }

  

  printf("done.\n");
  
  return 0;
}
Exemple #9
0
int main(int argc, char **argv) {
	if (argc != 10) {
		fprintf(stderr, "usage: %s sigma k min input output doPreprocess(0/1) doPostprocess(0/1) sort(s/c/f) rgb(0/1)\n", argv[0]);		
		fprintf(stderr, "   input: portable pixelmap (uncompressed) \n");
		fprintf(stderr, "   output: portable pixelmap (uncompressed)\n");
		fprintf(stderr, "   doPreprocess: 0 - without preprocess,  1 - with preprocess\n");
		fprintf(stderr, "   sort: s   - std::sort\n");
		fprintf(stderr, "         csf - parallel counting sort (round)\n");
		fprintf(stderr, "         csr - parallel counting sort (floor)\n");
		fprintf(stderr, "   rgb: 1 - RGB image, 0 - Gray-level (8 bit) images.\n");

		fprintf(stderr, "example: 0.5 500 20 dragon.ppm dragonOut.ppm 1 1 csr 1 \n");


		return 1;

		//example: 0.5 500 20 d:\testImgs\001_test.ppm d:\out001.ppm 1 1 csr 1 
	}

	EtTimer tmr;

	float sigma = (float)atof(argv[1]);
	float c = (float)atof(argv[2]);
	int minSize = atoi(argv[3]);
	bool preProcess = argv[6][0] == '1';
	bool postProcess = argv[7][0] == '1';
	bool rgbProcess = argv[9][0] == '1';

	image<rgb> *input = loadPPM(argv[4]);
	image<rgb> *res = new image<rgb>( input->width(), input->height());


	if( rgbProcess ){
		tmr.start();

		EtGcSegmentRgb* segmenter = new EtGcSegmentRgb(input->width(), input->height(), preProcess, postProcess, 3 );

		tmr.stop();
		std::cout << input->width() << " " << input->height() << " Inicialization " << tmr.getElapsedTime() << std::endl;

		tmr.start();

		segmenter->segment(input, res, sigma, c, minSize, argv[8]); 

		tmr.stop();
		std::cout << res->width() << " " << res->height() << " Full " << tmr.getElapsedTime() << std::endl;

		savePPM(res, argv[5]);
	}
	else{
		tmr.start();
		EtGcSegmentGray* segmenterG = new EtGcSegmentGray(input->width(), input->height(), preProcess, postProcess, 3 );
		image<uchar>* gray = imageRGBtoGRAY(input);

		tmr.stop();
		std::cout << input->width() << " " << input->height() << " Inicialization " << tmr.getElapsedTime() << std::endl;

		tmr.start();

		segmenterG->segment(gray, res, sigma, c, minSize, argv[8]); 

		tmr.stop();
		std::cout << res->width() << " " << res->height() << " Full " << tmr.getElapsedTime() << std::endl;

		savePPM(res, argv[5]);

	}

	return 0;
}
Exemple #10
0
int main(int argc, char **argv)
{
	if(argc<5)
	{
		printf("Usage : test <yuv image file> <image width> <image height> <output template filename>\n");
		return 1;
	}
	
	const int iteration_number = 100;
	printf("Time will be measured in each configuration for %d iterations...\n", iteration_number);
	
	const char *filename = argv[1];
	uint32_t width = atoi(argv[2]), height = atoi(argv[3]);
	const char *out = argv[4];
	char *out_filename = malloc(strlen(out)+15);
	uint8_t *YUV;
	if(readRawYUV(filename, width, height, &YUV)!=0)
	{
		printf("Error reading image file, check that the file exists and has the correct format and resolution\n");
		return 1;
	}
	uint8_t *Y = YUV, *U = YUV+width*height, *V = YUV+width*height+((width+1)/2)*((height+1)/2);
	
	uint8_t *RGB = malloc(3*width*height);
	
	//std version
	{
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			yuv420_rgb24_std(width, height, Y, U, V, width, (width+1)/2, RGB, width*3, YCBCR_601);
		t = clock()-t;
		printf("Processing time (standard) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_std.ppm");
		savePPM(out_filename, width, height, RGB, 3*width);
	}
	
	//sse2 unaligned version
	{
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			yuv420_rgb24_sseu(width, height, Y, U, V, width, (width+1)/2, RGB, width*3, YCBCR_601);
		t = clock()-t;
		printf("Processing time (sse2) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_sse.ppm");
		savePPM(out_filename, width, height, RGB, 3*width);
	}
	
	// ffmpeg version
	{
		
		struct SwsContext * ctx = sws_getContext(width, height, AV_PIX_FMT_YUV420P, 
			width, height, AV_PIX_FMT_RGB24, 
			0, 0, 0, 0);
		const uint8_t *const inData[3] = { Y, U, V };
		int inLinesize[3] = { width, (width+1)/2, (width+1)/2 };
		int outLinesize[1] = { 3*width};
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			sws_scale(ctx, inData, inLinesize, 0, height, &RGB, outLinesize);
		t = clock()-t;
		printf("Processing time (ffmpeg) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_av.ppm");
		savePPM(out_filename, width, height, RGB, 3*width);
	}
	
	#if USE_IPP
	// ipp version
	{
		const Ipp8u* pSrc[3] = {Y,U,V};
		int srcStep[3] = {width, (width+1)/2, (width+1)/2};
		Ipp8u* pDst = RGB;
		int dstStep = 3*width;
		IppiSize imgSize = {.width=width, .height=height};
		
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			ippiYCbCr420ToRGB_8u_P3C3R(pSrc, srcStep, pDst, dstStep, imgSize);
		t = clock()-t;
		printf("Processing time (ipp) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_ipp.ppm");
		savePPM(out_filename, width, height, RGB, 3*width);
	}
	#endif
	free(RGB);
	
	// now test with aligned data ...
	const size_t y_stride = width + (16-width%16)%16,
		uv_stride = (width+1)/2 + (16-((width+1)/2)%16)%16,
		rgb_stride = width*3 +(16-(3*width)%16)%16;
	const size_t y_size = y_stride*height, uv_size = uv_stride*((height+1)/2);
	uint8_t *Ya = _mm_malloc(y_size, 16);
	uint8_t *Ua = _mm_malloc(uv_size, 16);
	uint8_t *Va = _mm_malloc(uv_size, 16);
	for(unsigned int i=0; i<height; ++i)
	{
		memcpy(Ya+i*y_stride, Y+i*width, width);
		if((i%2)==0)
		{
			memcpy(Ua+(i/2)*uv_stride, U+(i/2)*((width+1)/2), (width+1)/2);
			memcpy(Va+(i/2)*uv_stride, V+(i/2)*((width+1)/2), (width+1)/2);
		}
	}
	
	RGB = _mm_malloc(3*rgb_stride*height, 16);
	
	//sse2 aligned version
	{
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			yuv420_rgb24_sse(width, height, Ya, Ua, Va, y_stride, uv_stride, RGB, rgb_stride, YCBCR_601);
		t = clock()-t;
		printf("Processing time (sse2 aligned) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_sse_a.ppm");
		savePPM(out_filename, width, height, RGB, rgb_stride);
	}
	
	// ffmpeg aligned version
	{
		struct SwsContext * ctx = sws_getContext(width, height, AV_PIX_FMT_YUV420P, 
			width, height, AV_PIX_FMT_RGB24, 
			0, 0, 0, 0);
		const uint8_t *const inData[3] = { Ya, Ua, Va };
		int inLinesize[3] = { y_stride, uv_stride, uv_stride};
		int outLinesize[1] = {rgb_stride};
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			sws_scale(ctx, inData, inLinesize, 0, height, &RGB, outLinesize);
		t = clock()-t;
		printf("Processing time (ffmpeg aligned) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_av_a.ppm");
		savePPM(out_filename, width, height, RGB, rgb_stride);
	}
	
	#if USE_IPP
	// ipp aligned version
	{
		const Ipp8u* pSrc[3] = {Ya,Ua,Va};
		int srcStep[3] = {y_stride, uv_stride, uv_stride};
		Ipp8u* pDst = RGB;
		int dstStep = rgb_stride;
		IppiSize imgSize = {.width=width, .height=height};
		
		clock_t t = clock();
		for(int i=0;i<iteration_number; ++i)
			ippiYCbCr420ToRGB_8u_P3C3R(pSrc, srcStep, pDst, dstStep, imgSize);
		t = clock()-t;
		printf("Processing time (ipp aligned) : %f sec\n", ((float)t)/CLOCKS_PER_SEC);
		
		strcpy(out_filename, out);
		strcat(out_filename, "_ipp_a.ppm");
		savePPM(out_filename, width, height, RGB, rgb_stride);
	}
	#endif
	
	_mm_free(RGB);
	_mm_free(Va);
	_mm_free(Ua);
	_mm_free(Ya);
	
	free(YUV);
	free(out_filename);
	
	return 0;
}
int main(int argc, char **argv) {
  if (argc < 6) {
    fprintf(stderr, "usage: %s  nbimages ratio_horizontal ratio_vertical input1 (without .ppm) input2 (without .ppm) \n", argv[0]);
    return 1;
  }
  // (1) variables declarations
  char * imname = new char[100];
  char * imname2 = new char[100];
  char * outname = new char[100];
  char * appel = new char[1000];

 // (2) reading arguments
  int nb_images = atoi(argv[1]);
  int start=1;
  int i;
  sprintf(imname, "%s0%004d.ppm", argv[4], start);
  sprintf(imname2, "%s0%004d.ppm", argv[5], start);
  int ratio_horizontal = atoi(argv[2]);  
  int ratio_vertical = atoi(argv[3]);  
  printf("%s\n",imname);
  image<rgb> *input; image<rgb> *input2;   
  // (3) loading first image
  printf("loading input image.\n");
  input = loadPPM(imname);
  int width = input->width();
  int height = input->height();
  int N = width*height;
  
  
  image<rgb> *big = new image<rgb>(width*ratio_horizontal, height*ratio_vertical);
  
  
 for (i=start;i<=nb_images;i++) {
 
   sprintf(imname, "%s0%004d.ppm",argv[4], i); 
   sprintf(imname2, "%s0%004d.ppm",argv[5], i);   
   sprintf(outname, "%sout-0%004d.ppm", argv[5], i);
   input = loadPPM(imname);
   input2 = loadPPM(imname2);

   if(ratio_vertical==2){
      for (int y = 0; y < height; y++) {
	for (int x = 0; x < width; x++) {
	  imRef(big,x,y) = imRef(input, x,y);
	  imRef(big,x,y+height) = imRef(input2, x,y);
	}
      }
   }
   else  if(ratio_horizontal==2){
      for (int y = 0; y < height; y++) {
	for (int x = 0; x < width; x++) {
	  imRef(big,x,y) = imRef(input, x,y);
	  imRef(big,x+width,y) = imRef(input2, x,y);
	}
      }
   }
  
  
   savePPM(big, outname);
   sprintf(appel, "convert %s %s.png  ;", outname, outname );
   system(appel);
  }

  printf("done.\n");
  
  return 0;
}
Exemple #12
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;
}