GDAL百分比剪輯拉伸,拉伸算法可以並行,但是RasterIO並行會出錯誤。

</pre><pre name="code" class="cpp">//百分比剪輯拉伸
int GFImageStretch::PercentClipStretch(const char* pszSrcFile, const char* pszDstFile, double percentMin, double percentMax)
{
	
	//遍歷所有波段
	for (int iBand =1; iBand <= nBandCount; iBand++)
	{		
		//獲得當前波段
		GDALRasterBand* pSrcBand = poSrcDataset->GetRasterBand(iBand);		
		GDALRasterBand* pDetBand = poDstDataset->GetRasterBand(iBand);
		//獲得統計信息
		cout<< "獲得統計信息" << endl;
		pSrcBand->ComputeStatistics(TRUE, &pdfMin, &pdfMax, &pdfMean, &pdfStdDev, pfnProgress, pProgress);

		switch (dataType)
		{
		case GDT_Byte:	  //對應圖像的Byte類型,8bit.
			{
				uchar* pSrcBuf = new uchar[nXSize]; 
				uchar* pDstBuf = new uchar[nXSize];
				int nBuckets = 256;
				int* pBandHistogram = new int[nBuckets];
				cout << "正在統計直方圖..." << endl;
				pSrcBand->GetHistogram(-0.5, 255.5, nBuckets, pBandHistogram, FALSE, FALSE, pfnProgress, pProgress);

				//#pragma omp parallel for	RasterIO不能使用並行,否則會出現數據錯誤。
				for (int h = 0; h< nYSize; h++)
				{ 
					pSrcBand->RasterIO(GF_Read, 0, h, nXSize, 1, pSrcBuf, nXSize, 1, GDT_Byte, 0, 0);					
					PercentClipStretchAglImpU8(pSrcBuf, pDstBuf, nXSize*nYSize, pBandHistogram, nBuckets, percentMin, percentMax);
					pDetBand->RasterIO(GF_Write, 0, h, nXSize, 1, pDstBuf, nXSize, 1, GDT_Byte, 0,0);	
				}
				//釋放緩衝區
				delete pSrcBuf;
				delete pDstBuf;
				break;
			}
		case GDT_UInt16:	//對應圖像的UInt類型,16bit.
			{
				ushort* pSrcBuf = new ushort[nXSize]; 
				ushort* pDstBuf = new ushort[nXSize];
				int nBuckets = 65536;
				int* imageHist = new int[nBuckets];
				cout << "正在統計直方圖..." << endl;
				//FALSE:不統計範圍之外的像素,TRUE精確計算各個像素值
				pSrcBand->GetHistogram(-0.5, 65535.5, nBuckets, imageHist, FALSE, FALSE, pfnProgress, pProgress);	 

				//#pragma omp parallel for
				for (int h = 0; h< nYSize; h++)
				{ 
					pSrcBand->RasterIO(GF_Read, 0, h, nXSize, 1, pSrcBuf, nXSize, 1, GDT_UInt16, 0, 0);					
					PercentClipStretchAglImpU16(pSrcBuf, pDstBuf, nXSize*nYSize, imageHist, nBuckets, percentMin, percentMax);	 
					pDetBand->RasterIO(GF_Write, 0, h, nXSize, 1, pDstBuf, nXSize, 1, GDT_UInt16, 0,0);	
				}
				//釋放緩衝區
				delete pSrcBuf;
				delete pDstBuf;
			}
			break;
		default:
			return 1; // 不識別的數據類型
		}
	}	 
	// 釋放內存空間
	GDALClose(poDstDataset);			
	GDALClose(poSrcDataset);
	return 0;
}

int GFImageStretch::PercentClipStretchAglImpU8(const uchar* poSrcData, uchar* poDstData, int nAllPixels, int ImageHist[],int histLength, double percentMin, double percentMax)
{ 
	//確定圖像數據灰度值。
	int grayScale = 255;

	int countMin = 0;
	int countMax = 0;
	int dbMin,dbMax;
	percentMin = int(percentMin * nAllPixels);
	percentMax = int(percentMax * nAllPixels);

	//找出 percentMin的像素的灰度值
	//#pragma omp parallel for
	for(int i=0;i<histLength;i++)
	{
		countMin += ImageHist[i]; 
		if(countMin > percentMin)
		{
			dbMin = i;
			break;
		}
	}

	//找出percnetMax的像素的灰度值
	//#pragma omp parallel for
	for(int k=0;k< histLength;k++)
	{ 
		countMax += ImageHist[k];
		if(countMax > percentMax)
		{
			dbMax = k;
			break;
		}
	}

	#pragma omp parallel for
	for (int i = 0;i < nXSize; i ++)
	{
		if (poSrcData[i] <= dbMin)
		{
			poDstData[i] = 0;
		}
		else if (poSrcData[i] >= dbMax)
		{
			poDstData[i] = grayScale;
		}
		else if (poSrcData[i] > dbMin && poSrcData[i] < dbMax)
		{
			poDstData[i] = double(poSrcData[i]-dbMin)/(double)(dbMax-dbMin) * grayScale;
		}
	}
	return 0;
}

int GFImageStretch::PercentClipStretchAglImpU16(const ushort* poSrcData, ushort* poDstData, int nAllPixels, int ImageHist[],int histLength, double percentMin, double percentMax)
{
	//確定圖像數據灰度值。
	int grayScale = 65536;

	int countMin = 0;
	int countMax = 0;
	int dbMin,dbMax;
	int minPixelIndex;
	int maxPixelIndex;

	minPixelIndex = int(percentMin * nAllPixels);
	maxPixelIndex = int(percentMax * nAllPixels);

	int total = 0;
	for(int i=0;i<histLength;i++)
	{
		total += ImageHist[i]; 		
	}

	//找出 percentMin的像素的灰度值
	//#pragma omp parallel for
	for(int i=0;i<histLength;i++)
	{
		countMin += ImageHist[i]; 
		if(countMin >= minPixelIndex)
		{
			dbMin = i;
			break;
		}
	}

	//找出percnetMax的像素的灰度值
	//#pragma omp parallel for
	for(int k=0;k<histLength;k++)
	{ 
		countMax += ImageHist[k];
		if(countMax >= maxPixelIndex)
		{
			dbMax = k;
			break;
		}
	}

	#pragma omp parallel for
	for (int i = 0;i < nXSize; i ++)
	{
		if (poSrcData[i] <= dbMin)
		{
			poDstData[i] = 0;
		}
		else if (poSrcData[i] >= dbMax)
		{
			poDstData[i] = grayScale;
		}
		else if (poSrcData[i] > dbMin && poSrcData[i] < dbMax)
		{
			poDstData[i] = double(poSrcData[i]-dbMin)/(double)(dbMax-dbMin) * grayScale;
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章