使用gdal和java对TIF格式正射影像进行拉普拉斯锐化

6 篇文章 0 订阅
3 篇文章 2 订阅

拉普拉斯锐化算法是读取目标像素上下左右四个像素值,将上下左右四个像素值分别减去目标像素值,再将结果之和加上目标像素值作为目标像素最后的值,也就是说当目标像素与周围像素差值较大,那么计算后会进一步拉大差值,突出图像上物体边缘部分,达到锐化效果。

一般jgp或者png格式图像采用Java ImageIO直接读取所有像素后进行拉普拉斯锐化即可,为了更方便读取tif格式影像则需要引入gdal。使用gdal读取tif影像基本信息,获取影像宽、高、通道数。一般彩色影像包含RGB三个通道或者还有一个表示透明度的阿尔法通道。RGB通道一般对应tif图像1、2、3通道,阿尔法通道一般为第4通道。

读取影像基本信息:

gdal.AllRegister();
Dataset srcDataset = gdal.Open(srcImage.toString(), gdalconstConstants.GA_ReadOnly);
int width = srcDataset.getRasterXSize();
int height = srcDataset.getRasterYSize();
Driver driver = srcDataset.GetDriver();
int count = srcDataset.GetRasterCount();

对tif影像进行拉普拉斯锐化,首先读取影像信息后转为BufferdImage,使用处理BufferdImage的拉普拉斯算法进行处理,之后再将BufferdImage转为tif图像。处理过程需要考虑到BufferdImage最大支持的图像宽和高为26663,而tif格式正射影像没有这一限制,那么处理过程需要考虑tif影像宽高是否超限,可以设定当tif影像宽或高超过26000像素就对影像进行拆分处理,如果没有超限则直接读取tif影像并转换为BufferdImage。

具体实现:

if (count <= 3) {
				Dataset destDataset = driver.Create(destImage.toString(), width, height, count);
				destDataset.SetMetadata(srcDataset.GetMetadata_Dict()); 
				Band srcBandR = srcDataset.GetRasterBand(1);
				Band srcBandG = srcDataset.GetRasterBand(2);
				Band srcBandB = srcDataset.GetRasterBand(3);
				Band destBandR = destDataset.GetRasterBand(1);
				Band destBandG = destDataset.GetRasterBand(2);
				Band destBandB = destDataset.GetRasterBand(3);
				
				int xIndex = (int)(width / 26000);
				int yIndex = (int)(height / 26000);
				int innerWidth = width / (xIndex + 1);
				int innerHeight = height / (yIndex + 1);
				BufferedImage innerImage = new BufferedImage(innerWidth, innerHeight, BufferedImage.TYPE_INT_RGB);
				for(int i = 0; i <= yIndex ; i ++) {
					for(int j = 0; j <= xIndex ;j++) {
						for (int y = 0; y < innerHeight; y++) {
							int[] arrayR = new int[innerWidth];
							int[] arrayG = new int[innerWidth];
							int[] arrayB = new int[innerWidth];
							srcBandR.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayR);
							srcBandG.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayG);
							srcBandB.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayB);
							for (int x = 0; x < innerWidth; x++) {
								int valueR = arrayR[x];
								int valueG = arrayG[x];
								int valueB = arrayB[x];
								int rgb = (clamp((int) valueR) & 0xff) << 16 | (clamp((int) valueG) & 0xff) << 8
										| (clamp((int) valueB) & 0xff);
								innerImage.setRGB(x, y, rgb);
							}
							
						}
						innerImage = new LaplacianSharpen().lapLaceSharp(innerImage);
						for (int y = 0; y < innerHeight; y++) {
							int[] arrayR = new int[innerWidth];
							int[] arrayG = new int[innerWidth];
							int[] arrayB = new int[innerWidth];
							for (int x = 0; x < innerWidth; x++) {
								int rgb = innerImage.getRGB(x, y);
								int valueR = (rgb >> 16) & 0xff;
								int valueG = (rgb >> 8) & 0xff;
								int valueB = rgb & 0xff;
								arrayR[x] = valueR;
								arrayG[x] = valueG;
								arrayB[x] = valueB;
							}
							destBandR.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayR);
							destBandG.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayG);
							destBandB.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayB);
						}
					}
				}
				driver.delete();
				srcDataset.delete();
				destDataset.delete();
			}else {
				Dataset destDataset = driver.Create(destImage.toString(), width, height, 4);
				destDataset.SetMetadata(srcDataset.GetMetadata_Dict()); 
				Band srcBandR = srcDataset.GetRasterBand(1);
				Band srcBandG = srcDataset.GetRasterBand(2);
				Band srcBandB = srcDataset.GetRasterBand(3);
				Band destBandR = destDataset.GetRasterBand(1);
				Band destBandG = destDataset.GetRasterBand(2);
				Band destBandB = destDataset.GetRasterBand(3);
				// 读取信息
				int xIndex = (int)(width / 26000);
				int yIndex = (int)(height / 26000);
				int innerWidth = width / (xIndex + 1);
				int innerHeight = height / (yIndex + 1);
				BufferedImage innerImage = new BufferedImage(innerWidth, innerHeight, BufferedImage.TYPE_INT_RGB);
				for(int i = 0; i <= yIndex ; i ++) {
					for(int j = 0; j <= xIndex ;j++) {
						for (int y = 0; y < innerHeight; y++) {
							int[] arrayR = new int[innerWidth];
							int[] arrayG = new int[innerWidth];
							int[] arrayB = new int[innerWidth];
							srcBandR.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayR);
							srcBandG.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayG);
							srcBandB.ReadRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayB);
							for (int x = 0; x < innerWidth; x++) {
								int valueR = arrayR[x];
								int valueG = arrayG[x];
								int valueB = arrayB[x];
								int rgb = (clamp((int) valueR) & 0xff) << 16 | (clamp((int) valueG) & 0xff) << 8
										| (clamp((int) valueB) & 0xff);
								innerImage.setRGB(x, y, rgb);
							}
							
						}
						innerImage = new LaplacianSharpen().lapLaceSharp(innerImage);
						for (int y = 0; y < height; y++) {
							int[] arrayR = new int[innerWidth];
							int[] arrayG = new int[innerWidth];
							int[] arrayB = new int[innerWidth];
							for (int x = 0; x < innerWidth; x++) {
								int rgb = innerImage.getRGB(x, y);
								int valueR = (rgb >> 16) & 0xff;
								int valueG = (rgb >> 8) & 0xff;
								int valueB = rgb & 0xff;
								arrayR[x] = valueR;
								arrayG[x] = valueG;
								arrayB[x] = valueB;
							}
							destBandR.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayR);
							destBandG.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayG);
							destBandB.WriteRaster(j * innerWidth, i * innerHeight + y, innerWidth, 1, arrayB);
						}
					}
				}
				Band srcBand = srcDataset.GetRasterBand(4);
				Band destBand = destDataset.GetRasterBand(4);
				for (int y = 0; y < height; y++) {
					int[] array = new int[width];
					srcBand.ReadRaster(0, y, width, 1, array);
					destBand.WriteRaster(0, y, width, 1, array);
				}
				driver.delete();
				srcDataset.delete();
				destDataset.delete();
			}
		

处理前影像:

处理后影像:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TheMatrixs

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值