在IDEA中利用OpenCV已经实现了图像直方图,怎样画累计直方图?
代码如下
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.List;
public class calHistogram {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
test();
}
public static void test() {
Mat m = Imgcodecs.imread("D:\\Project\\IDEA_Project\\RS01\\src\\rs01\\3.jpg");
HighGui.imshow("原图", m);
//灰度
Mat m1 = new Mat();
Imgproc.cvtColor(m, m1, Imgproc.COLOR_BGR2GRAY);
HighGui.imshow("灰度", m1);
List<Mat> images = new ArrayList<>();
images.add(m1);
//计算直方图
Mat hist = new Mat();
Imgproc.calcHist(images, new MatOfInt(0), new Mat(), hist, new MatOfInt(256), new MatOfFloat(0, 255));
//数值归一化
Core.normalize(hist, hist, 0, 255, Core.NORM_MINMAX);
int h = hist.rows();
Mat m2 = new Mat(new Size(400, 400), m.type(), new Scalar(200, 200, 200));
float[] histData = new float[256];
hist.get(0, 0, histData);
int x = 20;
int y = 300;
Imgproc.line(m2, new Point(x, 0), new Point(x, y), new Scalar(255, 0, 0));
Imgproc.line(m2, new Point(x, y), new Point(400, y), new Scalar(255, 0, 0));
for (int i = 0; i < h - 1; i++) {
int y1 = (int) histData[i];
Rect rect = new Rect();
rect.x = x + i;
rect.y = y - y1;
rect.width = 1;
rect.height = y1;
Imgproc.rectangle(m2, rect.tl(), rect.br(), new Scalar(15, 15, 15));
}
HighGui.imshow("直方图", m2);
HighGui.waitKey(0);
}
}
求接下来累计直方图该怎么写,谢谢各位
在网上搜了很久,都没找到利用Java实现的例子