Beispiel #1
0
Mat PreGraph::GeneSp(const Mat &img)
{
	int width = img.cols;
	int height = img.rows;
	int sz = width*height;
	UINT *reimg = new UINT[sz * 3];
	for (int c = 0; c<3; c++)
	{
		for (int i = 0; i<width; i++)
		{
			for (int j = 0; j<height; j++)

				reimg[c*(width*height) + i*height + j] = saturate_cast<unsigned int>(img.at<Vec3b>(j, i)[2 - c]);
		}
	}
	int* label = nullptr;
	SLIC slic;
	slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(reimg, height, width, label, spNum, spNumMax, compactness);
	Mat superpixels(img.size(), CV_16U);

	for (int i = 0; i<superpixels.rows; i++)
	{
		for (int j = 0; j<superpixels.cols; j++)
		{
			superpixels.at<ushort>(i, j) = label[i + j*superpixels.rows];
		}
	}
	delete [] reimg;
	delete [] label;
	return superpixels;
}
void SLICSegmentor::Run()
{
	cout<<"====="<<m_Name<<" Runing..."<<endl;

	SLIC slicsp;
	int h = m_Img.rows, w = m_Img.cols;
	uint* imgData = new uint[h*w];

	for (int i = 0; i < h; i++)
	{
		for (int j = 0; j < w; j++)
		{
			cv::Vec3b bgr = m_Img.at<cv::Vec3b>(i, j);
			imgData[i*w+j] = (bgr[2]<<16) | (bgr[1]<<8) | (bgr[0]);
		}
	}	

	int numLabels;
	int *klabels = new int[h*w];
	slicsp.PerformSLICO_ForGivenStepSize(imgData, w, h, klabels, numLabels, m_Step, NULL);

	for (int i = 0; i < h; i++)
	{
		int* ptr = m_Result.ptr<int>(i);
		for (int j = 0; j < w; j++)
		{
			ptr[j] = klabels[i*w+j];
		}
	}

	delete[] klabels;
	delete[] imgData;

	Segmentor::Run();
}
Beispiel #3
0
int main()
{
    string fileName = "fishMan.ppm";
    printf("loading input image.\n");
    image<rgb> *input = loadPPM(fileName.c_str());

    input->labels = new int[input->width() * input->height()];
    int numLabels(0);
    SLIC slic;

    slic.PerformSLICO_ForGivenK(input->getRGBData(), input->width(), input->height(),input->labels,numLabels, 100,10);

    /*for(int j = 0; j < input->height(); j++)
    {
        for(int i = 0; i < input->width(); i++)
            cout<<input->labels[j * input->width() + i]<< " ";
        cout<<endl;
    }*/


    printf("processing\n");
    int num_ccs;
    image<rgb> *seg = segment_image(input, 0.7, 5, 100, &num_ccs);
    savePPM(seg, "output.ppm");

    printf("got %d components\n", num_ccs);
    printf("done! uff...thats hard work.\n");
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{   
    int width(0), height(0);
   // unsigned int (32 bits) to hold a pixel in ARGB format as follows:
   // from left to right,
   // the first 8 bits are for the alpha channel (and are ignored)
   // the next 8 bits are for the red channel
   // the next 8 bits are for the green channel
   // the last 8 bits are for the blue channel
    
    int i,j,sz, k;
    unsigned int* pbuff=NULL;
    unsigned int* Isp;
    double m = 20;
    int* Ilabel;
    //----get input parameters ----//
       pbuff=(unsigned int *) mxGetPr(prhs[0]); 
       height=mxGetScalar(prhs[1]); 
       width=mxGetScalar(prhs[2]);
       k=mxGetScalar(prhs[3]);
       m=mxGetScalar(prhs[4]); //Compactness factor. use a value ranging from 10 to 40 depending on your needs. Default is 10
       sz=height*width;
           
   //--------- set out put space -----//
    // important!//
   plhs[0]=mxCreateNumericMatrix(1,sz,mxUINT32_CLASS,mxREAL);  
   Isp= (unsigned int*) mxGetData(plhs[0]);

   plhs[1]=mxCreateNumericMatrix(1,sz,mxINT32_CLASS,mxREAL);  
   Ilabel=(int*) mxGetData(plhs[1]);
       //----------------------------------
       // Initialize parameters
       //----------------------------------
      
       int* klabels = new int[sz];
       int numlabels(0);
       //----------------------------------
       //Perform SLIC on the image buffer
      // ----------------------------------
       SLIC segment;
       segment.PerformSLICO_ForGivenK(pbuff, width, height, klabels, numlabels, k, m);

       //Draw boundaries around segments
      // ----------------------------------
      segment.DrawContoursAroundSegments(pbuff, klabels, width, height, 0xff0000);
      
    
      for(i=0;i<sz;i++)
      {    Isp[i]= pbuff[i];
           Ilabel[i]= klabels[i];
          // mexPrintf("%d\n",klabels[i]);
      }

} 
Beispiel #5
0
void BasicImage::SLICSuperPixel(int pnum, double compact)
{
	int m = OriginalImage.channels();
	int c = OriginalImage.cols;
	int r = OriginalImage.rows;

	pbuff = new unsigned int[r*c];
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
		{
			pbuff[i*c+j] = (unsigned int)OriginalImage.at<Vec3b>(i,j)[2]*pow(2,16) + (unsigned int)OriginalImage.at<Vec3b>(i,j)[1]*pow(2,8) + (unsigned int)OriginalImage.at<Vec3b>(i,j)[0];
		}
	}

	SLIC segment;
	int* klabels = NULL;
	int numlabels(0);
	segment.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(pbuff, c, r, klabels, numlabels, pnum, compact);

	std::vector<std::vector<Point>> unsorted_kernal_groups;
	unsorted_kernal_groups.resize(numlabels);
	cutted = OriginalImage.clone();
	for (int i = 1; i < r-1; i++)
	{
		for (int j = 1; j < c-1; j++)
		{
			int n0 = klabels[i*c+j];
			int n1 = klabels[i*c+j-1];
			int n2 = klabels[i*c+j+1];
			int n3 = klabels[(i+1)*c+j];
			int n4 = klabels[(i-1)*c+j];
			int n5 = klabels[(i+1)*c+j+1];
			int n6 = klabels[(i+1)*c+j-1];
			int n7 = klabels[(i-1)*c+j+1];
			int n8 = klabels[(i-1)*c+j-1];
			if (n0 != n1 || n0 != n2 || n0 != n3 || n0 != n4 ||
				n0 != n5 || n0 != n6 || n0 != n7 || n0 != n8 )
			{
				cutted.at<Vec3b>(i,j)[0] = 0;
				cutted.at<Vec3b>(i,j)[1] = 0;
				cutted.at<Vec3b>(i,j)[2] = 188;

				int index = klabels[i*c+j];
				unsorted_kernal_groups[index].push_back(Point(i,j));
			}
		}
	}
	sortGroups(unsorted_kernal_groups);
	imwrite("cutted.png",cutted);
}
Beispiel #6
0
Mat GMRsaliency::GetSup(const Mat &image)
{
	Mat labels;

	SLIC slic;
	slic.Run(image, spcount, compactness, 5);
	labels = slic.GetLabels();

	// slic.WriteLabelsToFile("labels.png");
	// slic.WriteCentroidsToFile("centroids.png");


	return labels;
}
Mat GMRsaliency::GetSup(const Mat &image)
{
	int width=image.cols;
	int height=image.rows;
	int sz = width*height;
    UINT *img=new UINT[sz*3];
	for(int c=0;c<3;c++)
	{
		for(int i=0;i<width;i++)
		{
			for(int j=0;j<height;j++)

				img[c*(width*height)+i*height+j]=saturate_cast<unsigned int>(image.at<Vec3b>(j,i)[2-c]);
		}
	}
	int* labels = new int[sz];

	SLIC slic;
	slic.DoSuperpixelSegmentation_ForGivenNumberOfSuperpixels(img, height, width, labels, spcounta, spcount, compactness);




	Mat supLab(image.size(),CV_16U);
	for(int i=0;i<supLab.rows;i++)
	{
		for(int j=0;j<supLab.cols;j++)
		{
			supLab.at<ushort>(i,j)=labels[i+j*supLab.rows];

		}
	}



	if(labels) delete [] labels;
	if(img) delete []img;

	return supLab;

}
int main(int argc, char** argv)
{
	if (argc != 3) {
		printf("usage: test_slic <filename> <number of superpixels>\n");
		exit(-1);
	}

	cv::Mat img, result;
	
	img = imread(argv[1]);
	int numSuperpixel = atoi(argv[2]);

	SLIC slic;
	slic.GenerateSuperpixels(img, numSuperpixel);
	if (img.channels() == 3) 
		result = slic.GetImgWithContours(cv::Scalar(0, 0, 255));
	else
		result = slic.GetImgWithContours(cv::Scalar(128));

	cv::imwrite("result.jpg", result);
}
//===========================================================================
///	OnBnClickedButtonCreatesuperpixels
///
///	The main function
//===========================================================================
void CSLICSuperpixelsDlg::OnBnClickedButtonCreatesuperpixels()
{
	PictureHandler picHand;
	vector<string> picvec(0);
	picvec.resize(0);
	GetPictures(picvec);//user chooses one or more pictures
	string saveLocation = "C:\\rktemp\\";
	BrowseForFolder(saveLocation);

	int numPics( picvec.size() );

	//if(m_spcount < 0 || m_spcount < 20) m_spcount = 20;
	if(m_spcount < 0) m_spcount = 200;

	for( int k = 0; k < numPics; k++ )
	{
		UINT* img = NULL;
		int width(0);
		int height(0);

		picHand.GetPictureBuffer( picvec[k], img, width, height );
		int sz = width*height;
		if(m_spcount > sz) AfxMessageBox(L"Number of superpixels exceeds number of pixels in the image");

		int* labels = new int[sz];
		int numlabels(0);
		SLIC slic;
		slic.PerformSLICO_ForGivenK(img, width, height, labels, numlabels, m_spcount, m_compactness);//for a given number K of superpixels
		//slic.PerformSLICO_ForGivenStepSize(img, width, height, labels, numlabels, m_stepsize, m_compactness);//for a given grid step size
		//slic.DrawContoursAroundSegments(img, labels, width, height, 0);//for black contours around superpixels
		slic.DrawContoursAroundSegmentsTwoColors(img, labels, width, height);//for black-and-white contours around superpixels
		slic.SaveSuperpixelLabels(labels,width,height,picvec[k],saveLocation);
		if(labels) delete [] labels;
		
		picHand.SavePicture(img, width, height, picvec[k], saveLocation, 1, "_SLICO");// 0 is for BMP and 1 for JPEG)

		if(img) delete [] img;
	}
	AfxMessageBox(L"Done!", 0, 0);
}
int _tmain(int argc, _TCHAR* argv[]) {
#else
int main(int argc, char **argv) {
#endif

  if(argc != 2 && argc != 3) {
    fprintf(stderr, "Usage: sc <img1>\n");
    return 1;
  }

  int spSize = 5;
  if(argc == 3) {
    sscanf(argv[2], "%d", &spSize);
  }

  ImageFile imgFile (argv[1]);
  if(!imgFile.Load()) {
    fprintf(stderr, "Error loading file: %s\n", argv[1]);
    return 1;
  }

  FasTC::Image<> *img = imgFile.GetImage();

  const int kWidth = img->GetWidth();
  const int kHeight = img->GetHeight();
  const int nPixels = kWidth * kHeight;
  const uint32 pixelBufSz = nPixels * sizeof(FasTC::Pixel);

  FasTC::Pixel *pixels = new FasTC::Pixel[pixelBufSz];
  memcpy(pixels, img->GetPixels(), pixelBufSz);

  uint32 *rawPixels = new uint32[kWidth * kHeight];

  for(int i = 0; i < nPixels; i++) {
    // Pixels are stored as little endian ARGB, so we want ABGR
    pixels[i].Shuffle(0x6C); // 01 10 11 00
    rawPixels[i] = pixels[i].Pack();
  }

  int *labels = new int[nPixels];
  int numLabels;

  SLIC slic;
  slic.PerformSLICO_ForGivenStepSize(
    rawPixels,
	kWidth,
    kHeight,
    labels,
    numLabels,
	spSize, 1.0);

  std::unordered_map<uint32, Region> regions;
  CollectPixels(kWidth, kHeight, pixels, labels, regions);
  std::cout << "Num regions: " << regions.size() << std::endl;

  for(auto &r : regions) {
    r.second.Compress();
    r.second.Reconstruct();
  }

  for(int i = 0; i < nPixels; i++) {
    pixels[i] = regions[labels[i]].GetNextPixel();
    pixels[i].Shuffle(0x6C);
  }

  std::vector<Partition<4, 4> > partitions;
  EnumerateBPTC(partitions);
  std::cout << partitions.size() << " 4x4 BPTC partitions" << std::endl;

  VpTree<Partition<4, 4>, Partition<4, 4>::Distance> vptree;
  vptree.create(partitions);

  // Just to test, find the partition close to half 0 half 1..
  Partition<4, 4> test;
  for(uint32 i = 0; i < 16; i++) {
    if(i < 8) {
      test[i] = 0;
    } else {
      test[i] = 1;
    }
  }

  vector<Partition<4, 4> > closest;
  vptree.search(test, 1, &closest, NULL);
  std::cout << closest[0].GetIndex() << std::endl;

  BPTCC::CompressionSettings settings;
  settings.m_NumSimulatedAnnealingSteps = 0;
  settings.m_ShapeSelectionFn = ChosePresegmentedShape<4, 4>;

  SelectionInfo info(vptree, labels, kWidth, kHeight);
  settings.m_ShapeSelectionUserData = &info;

  uint8 *outBuf = new uint8[kWidth * kHeight];
  FasTC::CompressionJob cj(
     FasTC::eCompressionFormat_BPTC,
     reinterpret_cast<const uint8 *>(pixels),
     outBuf,
     static_cast<uint32>(kWidth),
     static_cast<uint32>(kHeight));

  StopWatch sw;
  sw.Start();
  BPTCC::Compress(cj, settings);
  sw.Stop();
  std::cout << "Compression time: " << sw.TimeInMilliseconds() << "ms" << std::endl;

  CompressedImage ci(kWidth, kHeight, FasTC::eCompressionFormat_BPTC, outBuf);
  FasTC::Image<> outImg(kWidth, kHeight, pixels);

  std::cout << "PSNR: " << outImg.ComputePSNR(&ci) << "db" << std::endl;

  ImageFile outImgFile("out.png", eFileFormat_PNG, outImg);
  outImgFile.Write();

  delete [] labels;
  delete [] rawPixels;
  delete [] pixels;
  return 0;
}