opencv3_java 添加mask掩码 AddMask

添加mask掩码 AddMask


package opencv_java_demo;

import org.opencv.core.*;
import org.opencv.imgcodecs.*;

public class AddMask {

	private static Mat makeMask(Mat src){
		Size sz=new Size(src.width(),src.height());
		Mat mask=new Mat(sz,CvType.CV_8UC1);
		mask=Mat.zeros(sz, CvType.CV_8U);
		for(int y=src.height()/4;y<src.height()*3/4;y++){
			for(int x=src.height()/4;x<src.width()*3/4;x++){
				mask.put(y, x, 255);
			}
		}
		return mask;
	}
	
	public static void main(String[] args) {
		try{
			System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
			
			Mat src1=Imgcodecs.imread("./images/lenna.jpg");
			Mat src2=Imgcodecs.imread("./images/Sobel.jpg");
			//读取图像到矩阵中
			if(src1.empty()||src2.empty()){
				throw new Exception("no file");
			}
			
			Mat dst = new Mat();
			
			Rect roiArea=new Rect(src1.width()/4,src1.height()/4,src1.width()/2,src1.height()/2);
			
			
			Mat mask=makeMask(src1);
			Core.add(src1, src2, dst,mask);
			Imgcodecs.imwrite("./images/AddMask1.jpg", dst);
			
			dst=src1.clone();
			Core.add(src1, src2,dst, mask);
			Imgcodecs.imwrite("./images/AddMask2.jpg", dst);
		}catch(Exception e){
			System.out.println("例外:" + e);
		}

	}

}







你可能感兴趣的:(java,opencv3)