java opecv 基本操作,创建图片,逻辑操作,通道分离
// 创建方法 - 克隆
Mat m1 = src.clone();
// 复制
Mat m2 = new Mat();
src.copyTo(m2);
// 赋值法
Mat m3 = src;
// 创建空白图像
Mat m4 = Mat.zeros(src.size(), src.type());
Mat m5 = Mat.zeros(new Size(512, 512), CvType.CV_8UC3);
Mat m6 = Mat.ones(new Size(512, 512), CvType.CV_8UC3);
//创建白色背景图片
Mat m7 = new Mat(512, 512, CvType.CV_8UC3, new Scalar(255, 255, 255));
//create image one
Mat src1 = Mat.zeros(new Size(400, 400), CvType.CV_8UC3);
//绘制红色矩形
Imgproc.rectangle(src1, new Point(100, 100), new Point(200, 200), new Scalar(0, 0, 255), -1);
// create image two
Mat src2 = Mat.zeros(new Size(400, 400), CvType.CV_8UC3);
//绘制红色矩形
Imgproc.rectangle(src2, new Point(150, 150), new Point(250, 250), new Scalar(0, 0, 255), -1);
// 逻辑操作
Mat dst1 = new Mat(), dst2 = new Mat(), dst3 = new Mat();
Core.bitwise_and(src1, src2, dst1);
Core.bitwise_xor(src1, src2, dst2);
Core.bitwise_or(src1, src2, dst3);
//通道分离
List<Mat> mv = new ArrayList<Mat>();
Mat sdst1 = new Mat(), sdst2 = new Mat(), sdst3 = new Mat();
// 蓝色通道为零
Core.split(src, mv);
mv.get(0).setTo(new Scalar(0));
Core.merge(mv, sdst1);
// 绿色通道为零
Core.split(src, mv);
mv.get(1).setTo(new Scalar(0));
Core.merge(mv, sdst2);
// 红色通道为零
Core.split(src, mv);
mv.get(2).setTo(new Scalar(0));
Core.merge(mv, sdst3);