由于之前工作需要研究了图片比对技术,实践过python的PIL感觉达不到我的测试需要,之后研究opencv与javacv(javaCV整合了很多图像影音处理的jar,并且windows、linux、Mac都兼容),opencv与javacv的安装大家直接google自行解决吧,话不多说直接上代码。

java(javaCV)版图片部分匹配代码:

TemplateMatch工具类:

package com.javacv.test;

import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_core.cvMinMaxLoc;
import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_TM_CCORR_NORMED;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMatchTemplate;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class TemplateMatch {

    private opencv_core.IplImage image;

    public void load(String filename) {
        image = cvLoadImage(filename); 
    }

    public boolean matchTemplate(IplImage source) {
        boolean matchRes;
        IplImage result = cvCreateImage(opencv_core.cvSize(
                source.width() - this.image.width() + 1,
                source.height() - this.image.height() + 1),
                opencv_core.IPL_DEPTH_32F, 1);

        opencv_core.cvZero(result);
        cvMatchTemplate(source, this.image, result, CV_TM_CCORR_NORMED);
        opencv_core.CvPoint maxLoc = new opencv_core.CvPoint();
        opencv_core.CvPoint minLoc = new opencv_core.CvPoint();
        double[] minVal = new double[2];
        double[] maxVal = new double[2];

        cvMinMaxLoc(result, minVal, maxVal, minLoc, maxLoc, null);
        matchRes = maxVal[0] > 0.99f ? true : false;
        cvReleaseImage(result);
        return matchRes;
    }
}

JavaCVTest测试类:

package com.javacv.test;

import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
public class JavaCVTest {

    public static void main(String[] args) {
        System.out.println("START...");
        TemplateMatch tm = new TemplateMatch();//实例化TemplateMatch对象
        tm.load("data/585.png");//加载带比对图片,注此图片必须小于源图
        boolean result = tm.matchTemplate(cvLoadImage("data/58home.png"));//校验585.png是否包含于原图58home.png
        if (result){//打印匹配结果,boolean
            System.out.println("match");
        }else{
            System.out.println("un-match");
        }
        System.out.println("END...");
    }
}

python(opencv)版图片部分匹配代码:

#coding=utf-8
import cv2  
import numpy as np   

class imgMatcher:

    def __init__(self,sourceimg):
        self.sourceimg = sourceimg

    def find(self,templateimg,threshold=0.8):
        image = cv2.imread(self.sourceimg)  
        template = cv2.imread(templateimg)  
        result = cv2.matchTemplate(image,template,cv2.cv.CV_TM_CCOEFF_NORMED) 
        similarity = cv2.minMaxLoc(result)[1]
        if similarity < threshold:
            return similarity
        else:
            return np.unravel_index(result.argmax(),result.shape)


if __name__=="__main__":
    matcher = imgMatcher("58home.png")
    for img in ['581.png','582.png','583.png','584.png','585.png','586.png','587.png','588.png','589.png','5810.png']:
        print img,matcher.find(img)

58home.png:


585.png:

目前我运用图片匹配技术在脚本开发过中,为了丰富验证方式,以及正在开发一个脚本录制工具,基于坐标点击+图片验证点插入。希望大家一起探讨运用图片匹配技术,如何更好的开展无线UI兼容测试。

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐