拉普拉斯锐化算法是读取目标像素上下左右四个像素值,将上下左右四个像素值分别减去目标像素值,再将结果之和加上目标像素值作为目标像素最后的值,也就是说当目标像素与周围像素差值较大,那么计算后会进一步拉大差值,突出图像上物体边缘部分,达到锐化效果。
一般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();
}
处理前影像:
处理后影像: