Add image enhancement without clahe

This commit is contained in:
Bingkun Li 2026-03-03 10:39:55 +08:00
parent 6725634e8e
commit 7451b5e60a

View File

@ -803,6 +803,90 @@ public class SarImageToneAdjuster {
return sharpened; return sharpened;
} }
public static Mat enhanceSarIndustrialNoClahe(Mat src) {
if (src.channels() != 1)
throw new IllegalArgumentException("Only single channel supported");
Mat floatMat = new Mat();
// 1 float
if (src.type() == CvType.CV_16U)
src.convertTo(floatMat, CvType.CV_32F, 1.0 / 65535.0);
else if (src.type() == CvType.CV_8U)
src.convertTo(floatMat, CvType.CV_32F, 1.0 / 255.0);
else
throw new IllegalArgumentException("Unsupported type");
// 2 log 压缩
Core.add(floatMat, Scalar.all(1e-6), floatMat);
Core.log(floatMat, floatMat);
// 3 百分位裁剪
Mat flat = floatMat.reshape(1,1);
Mat sorted = flat.clone();
Core.sort(sorted, sorted, Core.SORT_ASCENDING);
int total = sorted.cols();
double low = sorted.get(0, (int)(total * 0.01))[0];
double high = sorted.get(0, (int)(total * 0.995))[0];
Core.min(floatMat, new Scalar(high), floatMat);
Core.max(floatMat, new Scalar(low), floatMat);
Core.subtract(floatMat, new Scalar(low), floatMat);
Core.divide(floatMat, new Scalar(high - low), floatMat);
// ==============================
// 4 局部自适应对比增强替代 CLAHE
// ==============================
Mat localMean = new Mat();
double sigma = src.cols() / 30.0;
Imgproc.GaussianBlur(floatMat, localMean, new Size(0,0), sigma);
Mat localContrast = new Mat();
Core.divide(floatMat, localMean, localContrast);
// 压缩避免过增强
Core.min(localContrast, new Scalar(3.0), localContrast);
Core.max(localContrast, new Scalar(0.3), localContrast);
// 再归一
Core.normalize(localContrast, localContrast, 0, 1, Core.NORM_MINMAX);
// ==============================
// 5 gamma 提升
// ==============================
Core.pow(localContrast, 0.8, localContrast);
// ==============================
// 6 8bit
// ==============================
Mat img8 = new Mat();
localContrast.convertTo(img8, CvType.CV_8U, 255.0);
// ==============================
// 7 speckle 抑制
// ==============================
Imgproc.medianBlur(img8, img8, 3);
// ==============================
// 8 轻锐化
// ==============================
Mat blur = new Mat();
Imgproc.GaussianBlur(img8, blur, new Size(0,0), 1.0);
Mat sharpened = new Mat();
Core.addWeighted(img8, 1.15, blur, -0.15, 0, sharpened);
return sharpened;
}
static public double computePercentile(Mat mat, double percentile) { static public double computePercentile(Mat mat, double percentile) {
if (mat.empty()) if (mat.empty())