ROI操作,可以用于裁剪图片,合并图片
public void testRoi() {
Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");
int h = src.rows();
int w = src.cols();
//获取ROI, 相当于裁剪图片
Rect rect = new Rect(h / 2, w / 2, 50, 50);
Mat roi = new Mat(src, rect);
GeneralUtils.saveByteImg(roi, "C:\\图片\\test\\roi.jpg");
//修改roi区域为红色
Mat image = roi.clone();
roi.setTo(new Scalar(0, 0, 255));
GeneralUtils.saveByteImg(roi, "C:\\图片\\test\\updateRoi.jpg");
}
直方图 calcHist
//绘制直方图
//calcHist
@Test
public void test17() {
Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");
int dims = src.channels();
MatOfInt bins = new MatOfInt(256);
MatOfFloat hranges = new MatOfFloat(0, 255);
if (dims == 3) {
List<Mat> bgr_plane = new ArrayList<>();
Core.split(src, bgr_plane);
Mat b_hist = new Mat();
Mat g_hist = new Mat();
Mat r_hist = new Mat();
//计算蓝色通道,绿色通道,红色通道的直方图
Imgproc.calcHist(Arrays.asList(bgr_plane.get(0)), new MatOfInt(0), new Mat(), b_hist, bins, hranges);
Imgproc.calcHist(Arrays.asList(bgr_plane.get(1)), new MatOfInt(0), new Mat(), g_hist, bins, hranges);
Imgproc.calcHist(Arrays.asList(bgr_plane.get(2)), new MatOfInt(0), new Mat(), r_hist, bins, hranges);
//显示直方图
int hist_w = 512;
int hist_h = 400;
int bin_w = (int) (hist_w / bins.get(0, 0)[0]);
Mat histImage = Mat.zeros(hist_h, hist_w, CvType.CV_8UC3);
//归一化直方图
Core.normalize(b_hist, b_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
for (int i = 1; i < bins.get(0,0)[0]; i++) {
Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - b_hist.get(i - 1 , 0)[0]),
new Point(bin_w * i, hist_h - b_hist.get(i, 0)[0]), new Scalar(255, 0, 0), 2, 8, 0);
Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - g_hist.get(i - 1 , 0)[0]),
new Point(bin_w * i, hist_h - g_hist.get(i, 0)[0]), new Scalar(0, 255, 0), 2, 8, 0);
Imgproc.line(histImage, new Point(bin_w * (i - 1), hist_h - r_hist.get(i - 1 , 0)[0]),
new Point(bin_w * i, hist_h - r_hist.get(i, 0)[0]), new Scalar(0, 0, 255), 2, 8, 0);
}
GeneralUtils.saveByteImg(histImage, "C:\\图片\\test\\histImage.jpg");
}
}
直方图均衡化 equalizeHist
//equalizeHist 直方图均衡化,,用于提高图像的质量
public void testEqualizeHist() {
Mat src = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");
Mat gray = new Mat();
Mat dst = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist(gray, dst);
GeneralUtils.saveByteImg(dst, "C:\\图片\\test\\equalizeHist.jpg");
}
图片相识度比较 compareHist
public void testComareHist() {
Mat src1 = GeneralUtils.converMat("C:\\图片\\test\\0001.jpg");
Mat src2 = GeneralUtils.converMat("C:\\图片\\test\\0002.jpg");
//转成hsv色彩空间
Mat hsv1 = new Mat();
Mat hsv2 = new Mat();
Imgproc.cvtColor(src1, hsv1, Imgproc.COLOR_BGR2HSV);
Imgproc.cvtColor(src2, hsv2, Imgproc.COLOR_BGR2HSV);
//计算直方图
MatOfInt histSize = new MatOfInt(60);
MatOfFloat h_ranges = new MatOfFloat(0, 255);
Mat hist1 = new Mat();
Mat hist2 = new Mat();
Imgproc.calcHist(Arrays.asList(hsv1), new MatOfInt(0), new Mat(), hist1, histSize, h_ranges);
Imgproc.calcHist(Arrays.asList(hsv2), new MatOfInt(0), new Mat(), hist2, histSize, h_ranges);
//归一化
Core.normalize(hist1, hist1, 0, 1, Core.NORM_MINMAX, -1, new Mat());
Core.normalize(hist2, hist2, 0, 1, Core.NORM_MINMAX, -1, new Mat());
//比较直方图
double comp = Imgproc.compareHist(hist1, hist2, 0);
double comp1 = Imgproc.compareHist(hist1, hist2, 1);
System.out.println("图片相似度0:" + comp);
System.out.println("图片相似度1:" + comp1);
}
文中所用到的GeneralUtils如下:GeneralUtils
求关注!!!