Exemple #1
0
image<float> *imageINTtoFLOAT(image<int> *input) {
  int width = input->width();
  int height = input->height();
  image<float> *output = new image<float>(width, height, false);

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      imRef(output, x, y) = imRef(input, x, y);
    }
  }
  return output;
}
Exemple #2
0
image<long> *imageUCHARtoLONG(image<uchar> *input) {
  int width = input->width();
  int height = input->height();
  image<long> *output = new image<long>(width, height, false);

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      imRef(output, x, y) = imRef(input, x, y);
    }
  }
  return output;
}
Exemple #3
0
image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
  int width = input->width();
  int height = input->height();
  image<uchar> *output = new image<uchar>(width, height, false);

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      imRef(output, x, y) = (uchar)
    (imRef(input, x, y).r * RED_WEIGHT +
     imRef(input, x, y).g * GREEN_WEIGHT +
     imRef(input, x, y).b * BLUE_WEIGHT);
    }
  }
  return output;
}
Exemple #4
0
image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
  int width = input->width();
  int height = input->height();
  image<uchar> *output = new image<uchar>(width, height, false);

  if (max == min)
    return output;

  float scale = UCHAR_MAX / (float)(max - min);
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      uchar val = (uchar)((imRef(input, x, y) - min) * scale);
      imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
    }
  }
  return output;
}
void Match::SubPixel(GrayImage Im, GrayImage ImMin, GrayImage ImMax)
{
	Coord p;
	int I, I1, I2, I3, I4, I_min, I_max;

	for (p.y=0; p.y<im_size.y; p.y++)
	for (p.x=0; p.x<im_size.x; p.x++)
	{
		I = I_min = I_max = imRef(Im, p.x, p.y);
		if (p.x>0)           I1 = (imRef(Im, p.x-1, p.y) + I) / 2;
		else                 I1 = I;
		if (p.x<im_size.x-1) I2 = (imRef(Im, p.x+1, p.y) + I) / 2;
		else                 I2 = I;
		if (p.y>0)           I3 = (imRef(Im, p.x, p.y-1) + I) / 2;
		else                 I3 = I;
		if (p.y<im_size.y-1) I4 = (imRef(Im, p.x, p.y+1) + I) / 2;
		else                 I4 = I;

		if (I_min > I1) I_min = I1;
		if (I_min > I2) I_min = I2;
		if (I_min > I3) I_min = I3;
		if (I_min > I4) I_min = I4;
		if (I_max < I1) I_max = I1;
		if (I_max < I2) I_max = I2;
		if (I_max < I3) I_max = I3;
		if (I_max < I4) I_max = I4;

		imRef(ImMin, p.x, p.y) = I_min;
		imRef(ImMax, p.x, p.y) = I_max;
	}
}
void EtGcSegmentRgb::segment(image<rgb> *im, image<rgb>*dest, float sigma, float c, int minSize,  std::string sortingMethod){
	EtTimer tmr;

	//smooth each color channel  
	#pragma omp parallel for
	for (int y = 0; y < imgH; y++) {
		for (int x = 0; x < imgW; x++) {
			imRef(this->r, x, y) = imRef(im, x, y).r;
			imRef(this->g, x, y) = imRef(im, x, y).g;
			imRef(this->b, x, y) = imRef(im, x, y).b;
		}
	}

	if( doPreprocess ){
	    tmr.start();

		preprocess(sigma);
	    
		tmr.stop();
		std::cout << im->width() << " " << im->height() << " Preprocess " << tmr.getElapsedTime() << endl; 
	}

    tmr.start();

	buildGraph( );

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

    //EtGcSegment::segment( im, dest, sigma, c, minSize, sortingMethod );
	EtGcSegment::segment( dest, c, minSize, sortingMethod );
}
Exemple #7
0
int main(int argv, char **argc) {  
  if (argv != 4) {
    fprintf(stderr, "usage: %s in(pgm) out(pgm) sigma\n", argc[0]);
    exit(1);
  }

  srand48(time(NULL));
  float sigma = atof(argc[3]);

  image<uchar> *im = loadPGM(argc[1]);
  image<uchar> *out = new image<uchar>(im->width(), im->height());
  for (int y = 0; y < im->height(); y++) {
    for (int x = 0; x < im->width(); x++) {
      double r = gaussian()*sigma;
      double v = imRef(im, x, y);
      imRef(out, x, y) = bound(vlib_round(v + r), 0, 255);
    } 
  }

  savePGM(out, argc[2]);
  return 0;
}
void putdigit(int d, GrayImage im, int x0, int y0, int val)
{
    int x, y;

    assert(x0+DIGIT_WIDTH < imGetWidth(im));
    assert(y0+DIGIT_HEIGHT < imGetHeight(im));
    assert(d >= 0 && d <= 10);

    for (y=0; y < DIGIT_HEIGHT; y++)
        for (x=0; x < DIGIT_WIDTH; x++)
            if (digits[d][y][x])
                imRef(im, x0+x, y0+y) = val;
}
grid_map::Matrix SignedDistanceField::getPlanarSignedDistanceField(Eigen::Matrix<bool, Eigen::Dynamic, Eigen::Dynamic>& data) const
{
  image<uchar> *input = new image<uchar>(data.rows(), data.cols(), true);

  for (int y = 0; y < input->height(); y++) {
    for (int x = 0; x < input->width(); x++) {
      imRef(input, x, y) = data(x, y);
    }
  }

  // Compute dt.
  image<float> *out = dt(input);

  Matrix result(data.rows(), data.cols());

  // Take square roots.
  for (int y = 0; y < out->height(); y++) {
    for (int x = 0; x < out->width(); x++) {
      result(x, y) = sqrt(imRef(out, x, y));
    }
  }
  return result;
}
Exemple #10
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;
}
void LFLineFitter::Find(int x0,int y0,Point<int> *windPoints,int &nWindPoints,Image<unsigned char> *inputImage,int localWindSize)
{
	int x,y;
	nWindPoints = 0;

	for(y=max(y0-localWindSize,0);y<min(y0+localWindSize,inputImage->height());y++)
		for(x=max(x0-localWindSize,0);x<min(x0+localWindSize,inputImage->width());x++)
		{
			//if(cvGetReal2D(inputImage,y,x)!=0)
			if(imRef(inputImage,x,y)!=0)
			{
				windPoints[nWindPoints].x = x - x0;
				windPoints[nWindPoints].y = y - y0;
				nWindPoints++;
			}
		}
}
Exemple #12
0
/* compute laplacian */
static image<float> *laplacian(image<float> *src) {
  int width = src->width();
  int height = src->height();
  image<float> *dst = new image<float>(width, height);  

  for (int y = 1; y < height-1; y++) {
    for (int x = 1; x < width-1; x++) {
      float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) -
	2*imRef(src, x, y);
      float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) -
	2*imRef(src, x, y);
      imRef(dst, x, y) = d2x + d2y;
    }
  }
  return dst;
}
Exemple #13
0
image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
  int width = input->width();
  int height = input->height();
  image<rgb> *output = new image<rgb>(width, height, false);

  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      imRef(output, x, y).r = imRef(input, x, y);
      imRef(output, x, y).g = imRef(input, x, y);
      imRef(output, x, y).b = imRef(input, x, y);
    }
  }
  return output;
}
Exemple #14
0
// dissimilarity measure between pixels
static inline float diff(image<float> *r, image<float> *g, image<float> *b,
			 int x1, int y1, int x2, int y2) {
  return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) +
	      square(imRef(g, x1, y1)-imRef(g, x2, y2)) +
	      square(imRef(b, x1, y1)-imRef(b, x2, y2)));
}
void LMDirectionalIntegralDistanceImage::ComputeII(Image<float>* image)
{
	int x, y;
	Image<float> *tiimage = &iimage_;
	for (x = 0 ; x <= width_ ; x++)
	{
		imRef(tiimage,x,0) = 0;
		//cvSetReal2D(iimage_,0,x,0);
		//WriteFloat(iimage_,x,0) = 0;
	}

	for (y = 0 ; y <= height_ ; y++)
	{
		imRef(tiimage,0,y) = 0;
		//cvSetReal2D(iimage_,y,0,0);
		//WriteFloat(iimage_,0,y) = 0;
	}		


	if (xindexed_)
	{
		int miny, maxy;
		int py, cy;

		if (indices_[width_-1]> 0 )
		{
			miny = -indices_[width_-1];
			maxy = height_;
		}
		else
		{
			miny = 0;
			maxy = height_-indices_[width_-1];
		}

		for (y=miny ; y<=maxy ; y++)
		{
			for (x=1 ; x<width_ ; x++)
			{
				py = y+indices_[x-1];
				cy = y+indices_[x];

				if (cy > 0 &&  cy < height_-1)
				{
					imRef(tiimage,x,cy) = imRef(tiimage,x-1,py)+imRef(image,x,cy);
					//cvSetReal2D( iimage_, cy, x,cvGetReal2D(iimage_,py,x-1) + cvGetReal2D(image,cy,x));
					//WriteFloat( iimage_, x , cy) = ReadFloat(iimage_,x-1,py) + ReadFloat(image,x,cy);
				}
			}
		}
	}
	else
	{
		int minx, maxx;
		int px, cx;

		if (indices_[height_-1]> 0 )
		{
			minx = -indices_[height_-1];
			maxx = width_;
		}
		else
		{
			minx = 0;
			maxx = width_-indices_[height_-1];
		}

		for (x=minx ; x<=maxx ; x++)
		{
			for (y=1 ; y<height_; y++)
			{
				px = x+indices_[y-1];
				cx = x+indices_[y];

				if (cx > 0 &&  cx < width_-1)
				{
					imRef(tiimage,cx,y) = imRef(tiimage,px,y-1) + imRef(image,cx,y);
					//cvSetReal2D( iimage_, y, cx,cvGetReal2D(iimage_,y-1,px) + cvGetReal2D(image,y,cx));
					//WriteFloat( iimage_, cx , y) = ReadFloat(iimage_,px,y-1) + ReadFloat(image,cx,y);
				}
			}
		}
		

	}

	
}
/*
 * Segment an image
 *
 * Returns a color image representing the segmentation. 
 * JASPER: Random is replaced by just an index.
 *
 * im: image to segment.
 * sigma: to smooth the image.
 * c: constant for treshold function.
 * min_size: minimum component size (enforced by post-processing stage).
 * num_ccs: number of connected components in the segmentation.
 */
double *segment_image_index(image<rgb> *im, float sigma, float c, int min_size,
			  int *num_ccs) {
  int width = im->width();
  int height = im->height();

  image<float> *r = new image<float>(width, height);
  image<float> *g = new image<float>(width, height);
  image<float> *b = new image<float>(width, height);

  // smooth each color channel  
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      imRef(r, x, y) = imRef(im, x, y).r;
      imRef(g, x, y) = imRef(im, x, y).g;
      imRef(b, x, y) = imRef(im, x, y).b;
    }
  }
  image<float> *smooth_r = smooth(r, sigma);
  image<float> *smooth_g = smooth(g, sigma);
  image<float> *smooth_b = smooth(b, sigma);
  delete r;
  delete g;
  delete b;
 
  // build graph
  edge *edges = new edge[width*height*4];
  int num = 0;
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      if (x < width-1) {
	edges[num].a = y * width + x;
	edges[num].b = y * width + (x+1);
	edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
	num++;
      }

      if (y < height-1) {
	edges[num].a = y * width + x;
	edges[num].b = (y+1) * width + x;
	edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
	num++;
      }

      if ((x < width-1) && (y < height-1)) {
	edges[num].a = y * width + x;
	edges[num].b = (y+1) * width + (x+1);
	edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
	num++;
      }

      if ((x < width-1) && (y > 0)) {
	edges[num].a = y * width + x;
	edges[num].b = (y-1) * width + (x+1);
	edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
	num++;
      }
    }
  }
  delete smooth_r;
  delete smooth_g;
  delete smooth_b;

  // segment
  universe *u = segment_graph(width*height, num, edges, c);
  
  // post process small components
  for (int i = 0; i < num; i++) {
    int a = u->find(edges[i].a);
    int b = u->find(edges[i].b);
    if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
      u->join(a, b);
  }
  delete [] edges;
  *num_ccs = u->num_sets();

  //image<rgb> *output = new image<rgb>(width, height);

  // pick random colors for each component
  double *colors = new double[width*height];
  for (int i = 0; i < width*height; i++)
    colors[i] = 0;
  
  int idx = 1;
  double* indexmap = new double[width * height];
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int comp = u->find(y * width + x);
      if (!(colors[comp])){
          colors[comp] = idx;
          idx = idx + 1;
      }

      //imRef(output, x, y) = colors[comp];
      indexmap[x * height + y] = colors[comp];
    }
  }  
  //mexPrintf("indexmap 0: %f\n", indexmap[0]);
  //mexPrintf("indexmap 1: %f\n", indexmap[1]);

  delete [] colors;
  delete u;

  return indexmap;
}
void mexFunction(int nlhs, mxArray *out[], int nrhs, const mxArray *input[])
{

    
    // Checking number of arguments
    if(nlhs > 3){
        mexErrMsgTxt("Function has three return values");
        return;
    }

    if(nrhs != 4){
        mexErrMsgTxt("Usage: mexFelzenSegment(UINT8 im, double sigma, double c, int minSize)");
        return;
    }

    if(!mxIsClass(input[0], "uint8")){
        mexErrMsgTxt("Only image arrays of the UINT8 class are allowed.");
        return;
    }

    // Load in arrays and parameters
    UInt8* matIm = (UInt8*) mxGetPr(input[0]);
    int nrDims = (int) mxGetNumberOfDimensions(input[0]);
    int* dims = (int*) mxGetDimensions(input[0]);
    double* sigma = mxGetPr(input[1]);
    double* c = mxGetPr(input[2]);
    double* minSize = mxGetPr(input[3]);
    int min_size = (int) *minSize;

    int height = dims[0];
    int width = dims[1];
    int imSize = height * width;

    //SMANEN: Assertion
    int nChannels = dims[2];
    if(nChannels!=3){
        mexErrMsgTxt("Felzenszwalb segmentation should be called for images with 3 channels.");
        return;
    }

    int idx;
    image<rgb>* theIm = new image<rgb>(width, height);
    for (int x = 0; x < width; x++){
      for (int y = 0; y < height; y++){
        idx = x * height + y;
        imRef(theIm, x, y).r = matIm[idx];
        imRef(theIm, x, y).g = matIm[idx + imSize];
        imRef(theIm, x, y).b = matIm[idx + 2 * imSize];
      }
    }

    //SMANEN: Delete this
    /*mexPrintf("Warning: Saving passed image as a test. Delete/comment this afterwards.\n");
    srand(time(NULL));
    double r=rand();
    char filename[50];
    sprintf(filename,"seg_%f.dat",r);
    mexPrintf("Warning: Saving in file %s\n",filename);
    std::ofstream myfile;
    myfile.open (filename);
    for (int x = 0; x < width; x++){
      for (int y = 0; y < height; y++){

        idx = x * height + y;
        myfile<<(int)static_cast<unsigned char>(matIm[idx])<<"\n";
        myfile<<(int)static_cast<unsigned char>(matIm[idx + imSize])<<"\n";
        myfile<<(int)static_cast<unsigned char>(matIm[idx + 2 * imSize])<<"\n";
      }
    }

    myfile.close();*/


    // KOEN: Disable randomness of the algorithm
    //srand(12345);

    // Call Felzenswalb segmentation algorithm
    int num_css;
    //image<rgb>* segIm = segment_image(theIm, *sigma, *c, min_size, &num_css);
    double* segIndices = segment_image_index(theIm, *sigma, *c, min_size, &num_css);
    //mexPrintf("numCss: %d\n", num_css);

    // The segmentation index image
    out[0] = mxCreateDoubleMatrix(dims[0], dims[1], mxREAL);
    double* outSegInd = mxGetPr(out[0]);

    // Keep track of minimum and maximum of each blob
    out[1] = mxCreateDoubleMatrix(num_css, 4, mxREAL);
    double* minmax = mxGetPr(out[1]);
    for (int i=0; i < num_css; i++)
      minmax[i] = dims[0];
    for (int i= num_css; i < 2 * num_css; i++)
      minmax[i] = dims[1];

    // Keep track of neighbouring blobs using square matrix
    out[2] = mxCreateDoubleMatrix(num_css, num_css, mxREAL);
    double* nn = mxGetPr(out[2]);

    // Copy the contents of segIndices
    // Keep track of neighbours
    // Get minimum and maximum
    // These actually comprise of the bounding boxes
    double currDouble;
    int mprev, curr, prevHori, mcurr;
    for(int x = 0; x < width; x++){
      mprev = segIndices[x * height]-1;
      for(int y=0; y < height; y++){
        //mexPrintf("x: %d y: %d\n", x, y);
        idx = x * height + y;
        //mexPrintf("idx: %d\n", idx);
        //currDouble = segIndices[idx]; 
        //mexPrintf("currDouble: %d\n", currDouble);
        curr = segIndices[idx]; 
        //mexPrintf("curr: %d\n", curr);
        outSegInd[idx] = curr; // copy contents
        //mexPrintf("outSegInd: %f\n", outSegInd[idx]);
        mcurr = curr-1;

        // Get neighbours (vertical)
        //mexPrintf("idx: %d", curr * num_css + mprev);
        //mexPrintf(" %d\n", curr + num_css * mprev);
        //mexPrintf("mprev: %d\n", mprev);
        nn[(mcurr) * num_css + mprev] = 1;
        nn[(mcurr) + num_css * mprev] = 1;

        // Get horizontal neighbours
        //mexPrintf("Get horizontal neighbours\n");
        if (x > 0){
          prevHori = outSegInd[(x-1) * height + y] - 1;
          nn[mcurr * num_css + prevHori] = 1;
          nn[mcurr + num_css * prevHori] = 1;
        }

        // Keep track of min and maximum index of blobs
        //mexPrintf("Keep track of min and maximum index\n");
        if (minmax[mcurr] > y)
          minmax[mcurr] = y;
        if (minmax[mcurr + num_css] > x)
          minmax[mcurr + num_css] = x;
        if (minmax[mcurr + 2 * num_css] < y)
          minmax[mcurr + 2 * num_css] = y;
        if (minmax[mcurr + 3 * num_css] < x)
          minmax[mcurr + 3 * num_css] = x;

        //mexPrintf("Mprev = mcurr");
        mprev = mcurr;
      }
    }

    // Do minmax plus one for Matlab
    for (int i=0; i < 4 * num_css; i++)
      minmax[i] += 1;

    delete theIm;
    delete [] segIndices;

    mexPrintf("#########################################################\n");

    return;
}
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;
}
void Match::SubPixelColor(RGBImage Im, RGBImage ImMin, RGBImage ImMax)
{
	Coord p;
	int I, I1, I2, I3, I4, I_min, I_max;

	for (p.y=0; p.y<im_size.y; p.y++)
	for (p.x=0; p.x<im_size.x; p.x++)
	{
		/* red component */
		I = I_min = I_max = imRef(Im, p.x, p.y).r;
		if (p.x>0)           I1 = (imRef(Im, p.x-1, p.y).r + I) / 2;
		else                 I1 = I;
		if (p.x<im_size.x-1) I2 = (imRef(Im, p.x+1, p.y).r + I) / 2;
		else                 I2 = I;
		if (p.y>0)           I3 = (imRef(Im, p.x, p.y-1).r + I) / 2;
		else                 I3 = I;
		if (p.y<im_size.y-1) I4 = (imRef(Im, p.x, p.y+1).r + I) / 2;
		else                 I4 = I;

		if (I_min > I1) I_min = I1;
		if (I_min > I2) I_min = I2;
		if (I_min > I3) I_min = I3;
		if (I_min > I4) I_min = I4;
		if (I_max < I1) I_max = I1;
		if (I_max < I2) I_max = I2;
		if (I_max < I3) I_max = I3;
		if (I_max < I4) I_max = I4;

		imRef(ImMin, p.x, p.y).r = I_min;
		imRef(ImMax, p.x, p.y).r = I_max;


		/* green component */
		I = I_min = I_max = imRef(Im, p.x, p.y).g;
		if (p.x>0)           I1 = (imRef(Im, p.x-1, p.y).g + I) / 2;
		else                 I1 = I;
		if (p.x<im_size.x-1) I2 = (imRef(Im, p.x+1, p.y).g + I) / 2;
		else                 I2 = I;
		if (p.y>0)           I3 = (imRef(Im, p.x, p.y-1).g + I) / 2;
		else                 I3 = I;
		if (p.y<im_size.y-1) I4 = (imRef(Im, p.x, p.y+1).g + I) / 2;
		else                 I4 = I;

		if (I_min > I1) I_min = I1;
		if (I_min > I2) I_min = I2;
		if (I_min > I3) I_min = I3;
		if (I_min > I4) I_min = I4;
		if (I_max < I1) I_max = I1;
		if (I_max < I2) I_max = I2;
		if (I_max < I3) I_max = I3;
		if (I_max < I4) I_max = I4;

		imRef(ImMin, p.x, p.y).g = I_min;
		imRef(ImMax, p.x, p.y).g = I_max;


		/* blue component */
		I = I_min = I_max = imRef(Im, p.x, p.y).b;
		if (p.x>0)           I1 = (imRef(Im, p.x-1, p.y).b + I) / 2;
		else                 I1 = I;
		if (p.x<im_size.x-1) I2 = (imRef(Im, p.x+1, p.y).b + I) / 2;
		else                 I2 = I;
		if (p.y>0)           I3 = (imRef(Im, p.x, p.y-1).b + I) / 2;
		else                 I3 = I;
		if (p.y<im_size.y-1) I4 = (imRef(Im, p.x, p.y+1).b + I) / 2;
		else                 I4 = I;

		if (I_min > I1) I_min = I1;
		if (I_min > I2) I_min = I2;
		if (I_min > I3) I_min = I3;
		if (I_min > I4) I_min = I4;
		if (I_max < I1) I_max = I1;
		if (I_max < I2) I_max = I2;
		if (I_max < I3) I_max = I3;
		if (I_max < I4) I_max = I4;

		imRef(ImMin, p.x, p.y).b = I_min;
		imRef(ImMax, p.x, p.y).b = I_max;
	}
}
void segment_image(const cv::Mat& src_img, float sigma, float c, int min_size, int *num_ccs, cv::Mat& dst_img)
{
	image<rgb>* im = convertfromMat(src_img);
	int width = im->width();
	int height = im->height();
	
	image<float> *r = new image<float>(width, height);
	image<float> *g = new image<float>(width, height);
	image<float> *b = new image<float>(width, height);
	
	// smooth each color channel  
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			imRef(r, x, y) = imRef(im, x, y).r;
			imRef(g, x, y) = imRef(im, x, y).g;
			imRef(b, x, y) = imRef(im, x, y).b;
		}
	}
	image<float> *smooth_r = smooth(r, sigma);
	image<float> *smooth_g = smooth(g, sigma);
	image<float> *smooth_b = smooth(b, sigma);
	delete r;
	delete g;
	delete b;
	
	// build graph
	edge *edges = new edge[width*height*4];
	int num = 0;
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			if (x < width-1) {
				edges[num].a = y * width + x;
				edges[num].b = y * width + (x+1);
				edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y);
				num++;
			}
			
			if (y < height-1) {
				edges[num].a = y * width + x;
				edges[num].b = (y+1) * width + x;
				edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1);
				num++;
			}
			
			if ((x < width-1) && (y < height-1)) {
				edges[num].a = y * width + x;
				edges[num].b = (y+1) * width + (x+1);
				edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1);
				num++;
			}
			
			if ((x < width-1) && (y > 0)) {
				edges[num].a = y * width + x;
				edges[num].b = (y-1) * width + (x+1);
				edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1);
				num++;
			}
		}
	}
	delete smooth_r;
	delete smooth_g;
	delete smooth_b;
	
	// segment
	universe *u = segment_graph(width*height, num, edges, c);
	
	// post process small components
	for (int i = 0; i < num; i++) {
		int a = u->find(edges[i].a);
		int b = u->find(edges[i].b);
		if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
			u->join(a, b);
	}
	delete [] edges;
	*num_ccs = u->num_sets();
		
	dst_img = cv::Mat(src_img.size(), CV_32SC1);
	
//	std::map<int,int> key_values;
	int idx = 0;
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			dst_img.at<int>(y,x) = u->find(y * width + x);
		}
	}
	delete u;
}
Exemple #21
0
int SegmentImage(const image<RGB_f> *im, image<int> *segIdx, float sigma, float c, int min_size)
{
	int width = im->width();
	int height = im->height();
	image<float> *r = new image<float>(width, height);
	image<float> *g = new image<float>(width, height);
	image<float> *b = new image<float>(width, height);

	// smooth each color channel  
	for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) {
		imRef(r, x, y) = imRef(im, x, y).r;
		imRef(g, x, y) = imRef(im, x, y).g;
		imRef(b, x, y) = imRef(im, x, y).b;
	}
	image<float> *smooth_r = smooth(r, sigma);
	image<float> *smooth_g = smooth(g, sigma);
	image<float> *smooth_b = smooth(b, sigma);
	delete r;
	delete g;
	delete b;

	// build graph
	edge *edges = new edge[width*height * 4];
	int num = 0;
	for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) {
		if (x < width - 1) {
			edges[num].a = y * width + x;
			edges[num].b = y * width + (x + 1);
			edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y);
			num++;
		}

		if (y < height - 1) {
			edges[num].a = y * width + x;
			edges[num].b = (y + 1) * width + x;
			edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y + 1);
			num++;
		}

		if ((x < width - 1) && (y < height - 1)) {
			edges[num].a = y * width + x;
			edges[num].b = (y + 1) * width + (x + 1);
			edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y + 1);
			num++;
		}

		if ((x < width - 1) && (y > 0)) {
			edges[num].a = y * width + x;
			edges[num].b = (y - 1) * width + (x + 1);
			edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x + 1, y - 1);
			num++;
		}
		
	}
	delete smooth_r;
	delete smooth_g;
	delete smooth_b;
		
	universe *u = segment_graph(width*height, num, edges, c); // segment

	// post process small components
	for (int i = 0; i < num; i++) {
		int a = u->find(edges[i].a), b = u->find(edges[i].b);
		if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size)))
			u->join(a, b);
	}
	delete[] edges;
	map<int, int> marker;
	// = new image<int>(width, height);
	int idxNum = 0;
	for (int y = 0; y < height; y++) {
		int *imgIdx = segIdx->access[y];
		for (int x = 0; x < width; x++) {
			int comp = u->find(y * width + x);
			if (marker.find(comp) == marker.end())
				marker[comp] = idxNum++;
			imgIdx[x] = marker[comp];
		}
	}
	assert(idxNum == u->num_sets());
	delete u;
	return idxNum;
}
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;
}
void LFLineFitter::FitLine(Image<unsigned char> *inputImage)
{
	//LARGE_INTEGER t1, t2, f;
	//QueryPerformanceFrequency(&f);
	//QueryPerformanceCounter(&t1);

	width_ = inputImage->width();
	height_ = inputImage->height();

	map<int,Point<int> > edgeMap;

	int i,j,k;
	int x0,y0;
	int width,height;
	int index=0;
	int nPixels=0;
	int nEdges=0;
	int maxSupport=0;	
	LFLineSegment tmpLs,bestLs;
	Point<double> lnormal;
	int nWindPoints=0,nWaitingKillingList=0,nProposedKillingList=0;
	Point<int> *windPoints,*waitingKillingList,*proposedKillingList;
	windPoints = new Point<int> [nMaxWindPoints_];
	waitingKillingList = new Point<int>[nMaxWindPoints_];
	proposedKillingList = new Point<int>[nMaxWindPoints_];

	width = inputImage->width();
	height = inputImage->height();
	nPixels = width*height;

	for(int y=0;y<height;y++)
	{
		for(int x=0;x<width;x++)
		{
			i = x + y*width;
			//if(cvGetReal1D(inputImage,i)!=0)
			if(imRef(inputImage,x,y)!=0)
			{				
				edgeMap.insert(pair<int,Point<int> >(i,Point<int>(x,y)));
				nEdges++;
			}
		}
	}


	nInputEdges_ = nEdges;
	nLineSegments_=0;
	for(k=0;k<2;k++)
	{
		if(nEdges<nMinEdges_)
			break;
		for(i=0;i<nLinesToFitInStage_[k];i++)
		{
			maxSupport = 0;
			for(j=0;j<nTrialsPerLineInStage_[k];j++)
			{
				// Sample a point
				index = SampleAPixel(&edgeMap,inputImage,nPixels);
				y0 = index/width;
				x0 = index - y0*width;
				
				// Locate the subwindow
				Find(x0,y0,windPoints,nWindPoints,inputImage,smallLocalWindowSize_);				
				
				// Infer a line direction
				FitALine(nWindPoints,windPoints,sigmaFitALine_,lnormal);

				// Locate the subwindow			
				Find(&edgeMap,x0,y0,windPoints,nWindPoints,inputImage,localWindSize_);

				// Find the support			
				FindSupport(nWindPoints,windPoints,lnormal,sigmaFindSupport_,maxGap_,tmpLs,proposedKillingList,nProposedKillingList,x0,y0);
				
				// Check if need to update
				if(tmpLs.nSupport_ > maxSupport)
				{
					maxSupport = tmpLs.nSupport_;
					nWaitingKillingList = nProposedKillingList;
					memcpy(waitingKillingList,proposedKillingList,sizeof(Point<int>)*nWaitingKillingList);
					bestLs = tmpLs;
				}
			}

			// Remove points
			for(j=0;j<maxSupport;j++)
			{
				//cvSetReal2D(inputImage,waitingKillingList[j].y,waitingKillingList[j].x,0.0);
				imRef(inputImage,waitingKillingList[j].x,waitingKillingList[j].y) = 0;
				edgeMap.erase(waitingKillingList[j].y*width+waitingKillingList[j].x);
			}
			nEdges -= bestLs.nSupport_;
			bestLs.len_ = sqrt( (bestLs.sx_-bestLs.ex_)*(bestLs.sx_-bestLs.ex_) + (bestLs.sy_-bestLs.ey_)*(bestLs.sy_-bestLs.ey_));
			outEdgeMap_[nLineSegments_] = bestLs;
			nLineSegments_++;

			if(nEdges<nMinEdges_)
				break;
		}
	}
	MMFunctions::Sort(outEdgeMap_,nLineSegments_,0);
	delete [] windPoints;
	delete [] waitingKillingList;
	delete [] proposedKillingList;
	edgeMap.clear();
	////////QueryPerformanceCounter(&t2);
	//cout<<"[DO] Fit "<<nLineSegments_<<" lines taking "<<setiosflags(ios::fixed)<<setprecision(6)<<(t2.QuadPart - t1.QuadPart)/(1.0*f.QuadPart)<<"seconds"<<endl;
}