示例#1
0
// Find the boundary used in the multipart POST request
// the boundary is a string that never appears in any of the parts, ex. 'A92'
// keeps looking recursively until it finds one
std::string FindBoundary(std::map<std::string, std::string> parts, std::string boundary)
{
	// we only look for a-zA-Z0-9 chars
	unsigned int map[62];
	size_t blen = boundary.length();
	std::fill(&map[0], &map[62], 0);
	for (std::map<std::string, std::string>::iterator iter = parts.begin(); iter != parts.end(); iter++)
	{
		// loop through every character in each part and search for the substring, adding 1 to map for every character found (character after the substring)
		for (ssize_t j = 0; j < (ssize_t)((*iter).second.length()-blen); j++)
			if (!blen || (*iter).second.substr(j, blen) == boundary)
			{
				unsigned char ch = (*iter).second[j+blen];
				if (ch >= '0' && ch <= '9')
					map[ch-'0']++;
				else if (ch >= 'A' && ch <= 'Z')
					map[ch-'A'+10]++;
				else if (ch >= 'a' && ch <= 'z')
					map[ch-'a'+36]++;
			}
	}
	// find which next character occurs the least (preferably it occurs 0 times which means we have a match)
	unsigned int lowest = 0;
	for (unsigned int i = 1; i < 62; i++)
	{
		if (!map[lowest])
			break;
		if (map[i] < map[lowest])
			lowest = i;
	}

	// add the least frequent character to our boundary
	if (lowest < 10)
		boundary += '0'+lowest;
	else if (lowest < 36)
		boundary += 'A'+(lowest-10);
	else
		boundary += 'a'+(lowest-36);

	if (map[lowest])
		return FindBoundary(parts, boundary);
	else
		return boundary;
}
void CriminisiInpainting::Inpaint()
{
  //std::cout << "CriminisiInpainting::Inpaint()" << std::endl;
  try
  {
    // Start the procedure
    this->Stop = false;
    Initialize();
    ComputeSourcePatches();

    this->Iteration = 0;
    while(HasMoreToInpaint() && !this->Stop)
      {
      std::cout << "Iteration: " << this->Iteration << std::endl;

      FindBoundary();
      if(this->DebugImages)
	{
	Helpers::WriteImage<UnsignedCharScalarImageType>(this->BoundaryImage, "Debug/BoundaryImage.mha");
	}
      DebugMessage("Found boundary.");
    
      ComputeBoundaryNormals();
      if(this->DebugImages)
	{
	Helpers::WriteImage<FloatVector2ImageType>(this->BoundaryNormals, "Debug/BoundaryNormals.mha");
	}
      DebugMessage("Computed boundary normals.");

      ComputeAllDataTerms();
    
      ComputeAllPriorities();
      DebugMessage("Computed priorities.");
      
      itk::Index<2> pixelToFill = FindHighestPriority(this->PriorityImage);
      DebugMessage<itk::Index<2> >("Highest priority found to be ", pixelToFill);
      //std::cout << "Filling: " << pixelToFill << std::endl;

      //this->DebugWritePatch(pixelToFill, "PatchToFill", this->Iteration); // this must be done here because it is before the filling

      //itk::Index<2> bestMatchPixel = SelfPatchMatch<TImage, UnsignedCharImageType>(this->Image, this->Mask, pixelToFill, this->PatchRadius[0], iteration, this->Weights);
      //std::cout << "Best match pixel: " << bestMatchPixel << std::endl;

      itk::ImageRegion<2> targetRegion = Helpers::GetRegionInRadiusAroundPixel(pixelToFill, this->PatchRadius[0]);

      
      //unsigned int bestMatchSourcePatchId = BestPatch<FloatVectorImageType>(this->CurrentImage, this->CurrentMask, this->SourcePatches, targetRegion);
      
      DebugMessage("Finding best patch...");
      
      SelfPatchCompare* patchCompare;
      patchCompare = new SelfPatchCompareColor(this->CurrentImage->GetNumberOfComponentsPerPixel());
      
      //patchCompare->SetImage(this->CurrentImage);
      patchCompare->SetImage(this->CIELabImage);
      patchCompare->SetMask(this->CurrentMask);
      patchCompare->SetSourceRegions(this->SourcePatches);
      patchCompare->SetTargetRegion(targetRegion);
      unsigned int bestMatchSourcePatchId = patchCompare->FindBestPatch();
      //DebugMessage<unsigned int>("Found best patch to be ", bestMatchSourcePatchId);
      //std::cout << "Found best patch to be " << bestMatchSourcePatchId << std::endl;
      
      //this->DebugWritePatch(this->SourcePatches[bestMatchSourcePatchId], "SourcePatch.png");
      //this->DebugWritePatch(targetRegion, "TargetPatch.png");
      
      // Copy the patch. This is the actual inpainting step.
      Helpers::CopySelfPatchIntoValidRegion<FloatVectorImageType>(this->CurrentImage, this->CurrentMask, this->SourcePatches[bestMatchSourcePatchId], targetRegion);

      // Copy the new confidences into the confidence image
      UpdateConfidences(targetRegion);

      // The isophotes can be copied because they would only change slightly if recomputed.
      Helpers::CopySelfPatchIntoValidRegion<FloatVector2ImageType>(this->IsophoteImage, this->CurrentMask, this->SourcePatches[bestMatchSourcePatchId], targetRegion);

      // Update the mask
      this->UpdateMask(pixelToFill);
      DebugMessage("Updated mask.");
      
      // Sanity check everything
      DebugWriteAllImages();

      this->Iteration++;
#if defined(INTERACTIVE)
      emit RefreshSignal();
#endif
      } // end while(HasMoreToInpaint) loop

  }// end try
  catch( itk::ExceptionObject & err )
  {
    std::cerr << "ExceptionObject caught in Inpaint!" << std::endl;
    std::cerr << err << std::endl;
    exit(-1);
  }
}
示例#3
0
// add post data to a request
void Download::AddPostData(std::map<std::string, std::string> data)
{
	postDataBoundary = FindBoundary(data, "");
	postData = GetMultipartMessage(data, postDataBoundary);
}