lsgrmSplitter.txx 4.64 KiB
#ifndef __LSGRM_SPLITTER_TXX
#define __LSGRM_SPLITTER_TXX
namespace lsgrm
	template <class TInputImage>
	void SplitOTBImage(const std::string& inputImagePath, // Path to the input image
					   const std::string& ouputDirectoryPath, // Path to the directory storing the tiles
					   unsigned int& tileWidth, // width of the tile
					   unsigned int& tileHeight, // height of the tile
					   const unsigned int margin, // stability margin
					   const unsigned int niter)
		/* Some convenient typedefs to short the code. */
		using ImageType = TInputImage;
		using InternalPixelType = typename ImageType::InternalPixelType;
		using Extractor = otb::MultiChannelExtractROI<InternalPixelType, InternalPixelType>;
		using Reader = otb::ImageFileReader<ImageType>;
		using Writer = otb::ImageFileWriter<ImageType>;
		/* Read the input image and update information about its dimension */
		auto reader = Reader::New();
		reader->SetFileName(inputImagePath);
		reader->UpdateOutputInformation();
		auto imagePtr = reader->GetOutput();
		auto imageWidth = imagePtr->GetLargestPossibleRegion().GetSize()[0];
		auto imageHeight = imagePtr->GetLargestPossibleRegion().GetSize()[1];
		auto imageBands = imagePtr->GetNumberOfComponentsPerPixel();
		if(tileWidth + 2*margin >= imageWidth || tileHeight + 2* margin >= imageHeight)
			std::cout << "One dimension of the tile is greater than the image" << std::endl;
			exit(1);
		// Determine final size of the tiles according to the dimension of the image
		unsigned int k = std::ceil(imageWidth / tileWidth);
		while(imageWidth % k)
			k++;
		tileWidth = imageWidth / k;
		assert(tileWidth > 0);
		k = std::ceil(imageHeight / tileHeight);
		while(imageWidth % k)
			k++;
		tileHeight = imageHeight / k;
		assert(tileHeight > 0);
		/* Determine the number of tiles according to the tile dimension given by the user. */
		const unsigned int nbTilesX = imageWidth / tileWidth + ( imageWidth % tileWidth > 0 ? 1 : 0);
		const unsigned int nbTilesY = imageHeight / tileHeight + ( imageHeight % tileHeight > 0 ? 1 : 0);
		/* Open text file to record general parameters for the LSGRM library. */
		std::ofstream lsgrmLog(ouputDirectoryPath + "info.txt");
		assert(lsgrmLog.good());
		lsgrmLog << "Image width:"<< imageWidth << "\n" 
				 << "Image height:"<< imageHeight << "\n"
				 << "Number of bands:"<< imageBands << "\n"
				 << "Number of tiles according to X:" << nbTilesX << "\n"
				 << "Number of tiles according to Y:" << nbTilesY << "\n"
				 << "Tile width:" << tileWidth << "\n"
				 << "Tile height:" << tileHeight << "\n"
				 << "Stability Margin value:" << margin << "\n"
				 << "Number of iterations: " << niter << "\n";
		/* Local variables for the next loop */
		unsigned int startX, startY; // Upper left coordinates of the tile.
		unsigned int sizeX, sizeY; // Size of the tiles.
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
/* Loop over the tiles*/ for(unsigned int row = 0; row < nbTilesY; ++row) { for(unsigned int col = 0; col < nbTilesX; ++col) { startX = col * tileWidth; startY = row * tileHeight; sizeX = std::min(tileWidth, static_cast<unsigned int>(imageWidth - startX)); sizeY = std::min(tileHeight, static_cast<unsigned int>(imageHeight - startY)); lsgrmLog << "Tile " << row*nbTilesY + col << ":" << startX << "," << startY << "," << sizeX << "," << sizeY << ","; /* Margin at the top ? */ if( row > 0 ) { startY -= margin; sizeY += margin; lsgrmLog << "1,"; }else lsgrmLog << "0,"; /* Margin at the right */ if( col < nbTilesX - 1 ) { sizeX += margin; lsgrmLog << "1,"; } else lsgrmLog << "0,"; /* Margin at the bottom */ if( row < nbTilesY - 1) { sizeY += margin; lsgrmLog << "1,"; } else lsgrmLog << "0,"; /* Margin at the left */ if( col > 0 ) { startX -= margin; sizeX += margin; lsgrmLog << "1\n"; } else lsgrmLog << "0\n"; /* Extract the tile */ auto extractPtr = Extractor::New(); extractPtr->SetStartX(startX); extractPtr->SetStartY(startY); extractPtr->SetSizeX(sizeX); extractPtr->SetSizeY(sizeY); /* Write the tile to the ouput directory. */ auto writer = Writer::New(); writer->SetFileName(ouputDirectoryPath + "tile_" + std::to_string(row) + "_" + std::to_string(col) + ".tif"); /* OTB streaming pipeline */ extractPtr->SetInput(reader->GetOutput()); writer->SetInput(extractPtr->GetOutput()); writer->Update(); } // end for(unsigned int col = 0; col < nbTilesX; ++col)
141142143144145146147148
} // for(unsigned int row = 0; row < nbTilesY; ++row) /* Close the lsgrm info file. */ lsgrmLog.close(); } } // end of namespace lsgrm #endif