bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.42k stars 1.57k forks source link

A problem about pic compare #2079

Open cheyanbo opened 11 months ago

cheyanbo commented 11 months ago

hi,expert

I faced a problem when use javacv to accomplish a image compare,the background is I want to use java and selenium to acccomplish a automation test of slide confirmation , I use opencv_imgproc.matchTemplate to get the position of slide image in background image, and then move the button the the position to accomplish the confirmation. but the position is always not right , below is my code ,the verion I used is 1.5.8 , the matchTemplate I used is pencv_imgproc.TM_CCORR_NORMED the sample of slide is https://dun.163.com/trial/jigsaw ,Please help to give some advice ,Thanks

    Mat sliderMat = imread("/Users/cheyanbo/blockImage.png", opencv_imgcodecs.IMREAD_GRAYSCALE);
    Mat bgMat = imread("/Users/cheyanbo/bgImage.jpeg", opencv_imgcodecs.IMREAD_GRAYSCALE);

    opencv_imgproc.threshold(sliderMat, sliderMat, 127, 255, opencv_imgproc.THRESH_BINARY);
    opencv_imgproc.threshold(bgMat, bgMat, 127, 255, opencv_imgproc.THRESH_BINARY);

    imwrite("/Users/cheyanbo/block_black.png", sliderMat);
    imwrite("/Users/cheyanbo/bg_black.jpeg", bgMat);

    Mat result = new Mat();

    opencv_imgproc.matchTemplate(sliderMat, bgMat, result, opencv_imgproc.TM_CCORR_NORMED);

    opencv_core.normalize(result, result, 0, 1, opencv_core.NORM_MINMAX, -1, new Mat());

    DoublePointer pointer = new DoublePointer(new double[2]);
    org.bytedeco.opencv.opencv_core.Point maxLoc = new org.bytedeco.opencv.opencv_core.Point();

    opencv_core.minMaxLoc(result, null, pointer, null, maxLoc, null);

    opencv_imgproc.rectangle(sliderMat, maxLoc, new Point(maxLoc.x() + bgMat.cols(), maxLoc.y() + bgMat.rows()), new Scalar(0, 255, 0, 1));

System.out.println(maxLoc.x() + "," + maxLoc.y() + " x-y=" + (maxLoc.x() - maxLoc.y()));

    return maxLoc.x();