基于opencv的java版。
package com.sunyang.Test;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class T {
static String path = "保存结果的路径";
public static void main(String[] args) {
System.loadLibrary("opencv_java310");
Mat nat = Imgcodecs.imread("图片路径");
Mat m = new Mat();
Imgproc.cvtColor(nat, m, 6);
Mat result= new Mat();
double threshold = Imgproc.threshold(m, result, 0,255, 8);
Mat pre = new Mat(nat.size(),CvType.CV_8UC3,new Scalar(0,0,0));
Mat fin = new Mat(nat.size(),CvType.CV_8UC3,new Scalar(0,0,0));
System.out.println("切分阈值"+threshold);
for (int i = 0; i < result.rows(); i++) {
for (int j = 0; j < result.cols(); j++) {
double[] ds = result.get(i, j);
double[] data = {ds[0]/255,ds[0]/255,ds[0]/255};
pre.put(i, j, data);
}
}
System.out.println("pre矩阵初始化成功");
//矩阵点乘
for (int i = 0; i < pre.rows(); i++) {
for (int j = 0; j < pre.cols(); j++) {
double[] pre_ds = pre.get(i, j);
double[] nat_ds = nat.get(i, j);
double[] data = {pre_ds[0]*nat_ds[0],pre_ds[1]*nat_ds[1],pre_ds[2]*nat_ds[2]};
fin.put(i, j, data);
}
}
Imgcodecs.imwrite(path, fin);
}
}
初始:
结果: