OpenCVMatcher.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:visual-scripting 作者:
private static ScanMatch findMatch(Mat searchImageMat, Mat templateMat, double templateStdDev, double scale,
        Image.Int searchImageScaled, BigDecimal s) {        

    double templateScale = s.doubleValue() * scale;
    int w = (int) Math.round(templateMat.width() * templateScale);
    int h = (int) Math.round(templateMat.height() * templateScale);

    // early exit - template is bigger than search image
    if (templateMat.cols() * templateScale >= searchImageMat.cols() || templateMat.rows() * templateScale >= searchImageMat.rows()) {
        return null;
    }

    if (isTemplateTooSmall(w, h, s)) {
        return null;
    }           

    // scale
    Mat scaledTemplateMat = new MatOfFloat();
    resize(templateMat, scaledTemplateMat, new Size(w, h), 0, 0, CV_INTER_AREA);

    // normalized cross-corr
    Mat resultMatrix = new MatOfFloat();
    matchTemplate(searchImageMat, scaledTemplateMat, resultMatrix, TM_CCORR_NORMED);            
    MinMaxLocResult minMaxResult = minMaxLoc(resultMatrix);         

    // compute fingerprint for scaled template
    Image.Int templateForFingerprint = ImageUtil.Convert.toImage(OpenCV.matToBufferedImage(scaledTemplateMat));
    ImageFingerprint templateFingerprint = new ImageFingerprint(ImageUtil.toSquare(templateForFingerprint), 0xf2, 0xf1, 0xf0, FINGERPRINT_SIZE);

    // if template has low contrast bump it up
    if (templateStdDev < STDDEV_THRESHOLD) {

        Image.Int contrastedImage = ImageUtil.Convert.toImageInt(Contrast.autoContrast(ImageUtil.Convert.toImageByte(templateForFingerprint)));
        templateFingerprint = new ImageFingerprint(ImageUtil.toSquare(contrastedImage), 0xf2, 0xf1, 0xf0, FINGERPRINT_SIZE);
    }

    // cut the possible area from the image and get fingerprint probability for it      
    Rectangle.Int resultRectangle = new Rectangle.Int((int) minMaxResult.maxLoc.x, (int) minMaxResult.maxLoc.y, w, h);
    SingleScaleMatch singleScaleMatch = getMatchForRectangle(searchImageScaled, templateFingerprint, templateStdDev, minMaxResult, resultRectangle);

    // free
    resultMatrix.release();

    return new ScanMatch(singleScaleMatch.fingerprintMatch, scaleRectangle(singleScaleMatch.result, 1 / scale), s);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号