java 使用opencv进行图像匹配 截屏+匹配

本文使用opencv 进行图像匹配,通过截屏获取图像来源,与预设图片进行匹配。工具为eclipse。

opencv的安装百度即可,不过一点需要注意的是 Native library location要编辑:

5-native-library.png

Select External Folder... and browse to select the folder C:\OpenCV\build\java\x64. If you have a 32-bit system you need to select the x86 folder instead of x64.

7-user-library-final.png

Java获取屏幕截图,并返回mat格式数据

 public static Mat getScreenShot() {

        int captureWidth = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int captureHeight = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        BufferedImage bfImage =   new BufferedImage(captureWidth, captureHeight,      BufferedImage.TYPE_3BYTE_BGR);
        try {
            Robot robot = new Robot();
            
            Rectangle screenRect = new Rectangle(0,0,captureWidth,captureHeight);
            bfImage = robot.createScreenCapture(screenRect);

        } catch (AWTException e) {
            e.printStackTrace();
        }
        
        Mat mat =  new Mat(bfImage.getHeight(), bfImage.getWidth(), CvType.CV_8UC3);

        mat.put(0, 0,getMatrixRGB(bfImage));
        
        return mat;
    }
    
    /**
     * 获取图像RGB格式数据
     * @param image
     * @return
     */
    public static byte[] getMatrixRGB(BufferedImage image){
        if(image.getType()!=BufferedImage.TYPE_3BYTE_BGR){
            // 转sRGB格式
            BufferedImage rgbImage = new BufferedImage(
                        image.getWidth(), 
                        image.getHeight(),  
                        BufferedImage.TYPE_3BYTE_BGR);
            new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB), null).filter(image, rgbImage);
            // 从Raster对象中获取字节数组
           
            return  (byte[])((DataBufferByte)rgbImage.getRaster().getDataBuffer()).getData();
//            return (byte[]) rgbImage.getData().getDataElements(0, 0, rgbImage.getWidth(), rgbImage.getHeight(), null);
        }else{
            return (byte[]) image.getData().getDataElements(0, 0, image.getWidth(), image.getHeight(), null);
        }
    }

将截屏获取的数据与预存图片进行匹配:

	static int CVTYPE=CvType.CV_8UC4;
	static void findimg(Mat source) {
	        //将文件读入为OpenCV的Mat格式
		 	//被匹配的文件
		 	Mat template = Imgcodecs.imread("C:\\Users\\GrantZ\\Desktop\\newfolder\\d.jpg");
	        //创建于原图相同的大小,储存匹配度
	        Mat result = Mat.zeros(source.rows() - template.rows() + 1, source.cols() - template.cols() + 1, CVTYPE);
	        //调用模板匹配方法
	        int method=Imgproc.TM_CCOEFF_NORMED;
	        Imgproc.matchTemplate(source, template, result,method );
	        //规格化
	        Core.normalize(template, template, 0, 1, Core.NORM_MINMAX, -1);
	        //获得最可能点,MinMaxLocResult是其数据格式,包括了最大、最小点的位置x、y
	        Core.MinMaxLocResult mlr = Core.minMaxLoc(result);
	      
	        Point matchLoc;
	        
	        //根据匹配算法选择匹配结果
	        if (method==Imgproc.TM_SQDIFF||method==Imgproc.TM_SQDIFF_NORMED) {
	        	 matchLoc = mlr.minLoc;
	        	  System.out.println(" mlr.min="+mlr.minVal);//匹配度
	        	   //在原图上的对应模板可能位置画一个绿色矩形
	 	        System.out.println(matchLoc.x + template.width()+"  "+matchLoc.y+template.height());
	 	        Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.width(), matchLoc.y + template.height()), new Scalar(0, 255, 0));
			}
	        else {
	       	 matchLoc = mlr.maxLoc;
	       	 System.out.println("mlr.max="+mlr.maxVal);//匹配度
      	   //在原图上的对应模板可能位置画一个绿色矩形
	        System.out.println(matchLoc.x + template.width()+"  "+matchLoc.y+template.height());
	        Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.width(), matchLoc.y + template.height()), new Scalar(0, 255, 0));
	        }
	        
	        //将匹配结果输出为e.jpg 画绿色框
	        Imgcodecs.imwrite("C:\\Users\\GrantZ\\Desktop\\newfolder\\e.jpg", source);
	       
	}

使用:

public static void main(String[] args) {

 System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
	
		 OpencvMath o=new OpencvMath();
		 
		 o.findimg(o.getScreenShot());


}

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值