Example usage for org.opencv.core Mat Mat

List of usage examples for org.opencv.core Mat Mat

Introduction

In this page you can find the example usage for org.opencv.core Mat Mat.

Prototype

public Mat() 

Source Link

Usage

From source file:com.sikulix.api.Image.java

License:Open Source License

private Mat get() {
    Mat mContent = new Mat();
    if (urlImg != null) {
        File imgFile = new File(urlImg.getPath());
        mContent = Highgui.imread(imgFile.getAbsolutePath());
        if (!mContent.empty()) {
            log.debug("get: loaded: (%dx%s) %s", mContent.width(), mContent.height(), urlImg);
        } else {/*www.  j a v  a  2s.c o  m*/
            log.error("get: not loaded: %s", urlImg);
        }
    }
    return mContent;
}

From source file:com.sikulix.api.Image.java

License:Open Source License

private synchronized static void changeCache(Boolean upOrDown, URL urlImg, Mat mat) {
    if (SX.isNotSet(upOrDown) || ImageCache < currentMemory + getMatSize(mat)) {
        for (URL url : imageFiles.keySet()) {
            imageFiles.put(url, new Mat());
        }/*from w w  w . ja v a  2 s.c o  m*/
        currentMemory = 0;
        if (SX.isNotSet(upOrDown)) {
            return;
        }
    }
    if (upOrDown) {
        imageFiles.put(urlImg, mat);
        currentMemory += getMatSize(mat);
    } else {
        imageFiles.put(urlImg, new Mat());
        currentMemory -= getMatSize(mat);
        currentMemory = currentMemory < 0 ? 0 : currentMemory;
    }
}

From source file:com.sikulix.api.Image.java

License:Open Source License

/**
 * resize the image's CV-Mat by factor/*w w  w  . j a  v a 2 s . c om*/
 *
 * @param factor resize factor
 * @return a new inMemory Image
 */
public Image resize(float factor) {
    Image img = new Image();
    if (isValid()) {
        Mat newMat = new Mat();
        Size newS = new Size(w * factor, h * factor);
        Imgproc.resize(content, newMat, newS, 0, 0, Imgproc.INTER_AREA);
        img = new Image(newMat);
    }
    return img;
}

From source file:com.sikulix.api.Picture.java

License:Open Source License

protected void copy(Element elem) {
    if (elem.hasContent()) {
        setContent(elem.getContent().clone());
    } else {//from  w  ww . j a  va 2  s .  c  om
        setContent(new Mat());
    }
    urlImg = elem.urlImg;
    setName(elem.getName());
    setAttributes();
}

From source file:com.sikulix.api.Picture.java

License:Open Source License

public Picture(Mat mat) {
    this();/*  ww w . j  a v  a 2  s . co m*/
    if (SX.isNull(mat)) {
        setContent(new Mat());
    } else {
        long start = new Date().getTime();
        setContent(mat.clone());
        timeToLoad = new Date().getTime() - start;
    }
    init(0, 0, getContent().width(), getContent().height());
    setAttributes();
}

From source file:com.sikulix.api.Picture.java

License:Open Source License

private void setContent(String fpImg) {
    URL url = Picture.searchOnImagePath(fpImg);
    if (SX.isSet(url)) {
        setContent(url);/*w  w  w. ja  v  a  2 s  .  c  om*/
    } else {
        setContent(new Mat());
        setName(getNameFromFileL(new File(fpImg)));
    }
}

From source file:com.sikulix.api.Picture.java

License:Open Source License

private void setContent(URL url) {
    setContent(new Mat());
    if (SX.isSet(url)) {
        urlImg = url;/* w w  w . jav a  2  s . c  o  m*/
        setName(getNameFromURL(urlImg));
        if (urlImg != null) {
            long start = new Date().getTime();
            String urlProto = urlImg.getProtocol();
            if (urlProto.equals("file")) {
                File imgFile = new File(urlImg.getPath());
                setContent(Imgcodecs.imread(imgFile.getAbsolutePath()));
            } else {
                try {
                    setContent(makeMat(ImageIO.read(urlImg)));
                } catch (IOException e) {
                    log.error("load(): %s for %s", e.getMessage(), urlImg);
                }
            }
            timeToLoad = new Date().getTime() - start;
            if (isValid()) {
                setAttributes();
                log.debug("get: loaded: (%dx%s) %s", getContent().width(), getContent().height(), urlImg);
            } else {
                log.error("get: not loaded: %s", urlImg);
            }
        }
    }
}

From source file:com.sikulix.api.Picture.java

License:Open Source License

public Mat getResizedMat(double factor) {
    Mat newMat = getContent();/*from  ww  w.j ava  2  s  . c om*/
    if (isValid()) {
        newMat = new Mat();
        Size newS = new Size(w * factor, h * factor);
        Imgproc.resize(getContent(), newMat, newS, 0, 0, Imgproc.INTER_AREA);
    }
    return newMat;
}

From source file:com.sikulix.core.Finder.java

License:Open Source License

private FindResult doFind(Element elem, FindType findType) {
    if (!elem.isTarget()) {
        return null;
    }//from  w  w w  . j a v a 2s .co m
    log.trace("doFind: start");
    Element target = elem;
    if (target.getWantedScore() < 0) {
        target.setWantedScore(0.8);
    }
    boolean success = false;
    long begin_t = 0;
    Core.MinMaxLocResult mMinMax = null;
    FindResult findResult = null;
    if (FindType.ONE.equals(findType) && !isCheckLastSeen && SX.isOption("CheckLastSeen")
            && target.getLastSeen().isValid()) {
        begin_t = new Date().getTime();
        Finder lastSeenFinder = new Finder(target.getLastSeen());
        lastSeenFinder.isCheckLastSeen = true;
        target = new Target(target, target.getLastSeen().getScore() - 0.01);
        findResult = lastSeenFinder.doFind(target, FindType.ONE);
        if (findResult.hasNext()) {
            log.trace("doFind: checkLastSeen: success %d msec", new Date().getTime() - begin_t);
            return findResult;
        } else {
            log.trace("doFind: checkLastSeen: not found %d msec", new Date().getTime() - begin_t);
        }
    }
    double rfactor = 0;
    boolean downSizeFound = false;
    double downSizeScore = 0;
    double downSizeWantedScore = 0;
    if (FindType.ONE.equals(findType) && target.getResizeFactor() > resizeMinFactor) {
        // ************************************************* search in downsized
        begin_t = new Date().getTime();
        double imgFactor = target.getResizeFactor();
        Size sb, sp;
        Mat mBase = new Mat(), mPattern = new Mat();
        result = null;
        for (double factor : resizeLevels) {
            rfactor = factor * imgFactor;
            sb = new Size(base.cols() / rfactor, base.rows() / rfactor);
            sp = new Size(target.getContent().cols() / rfactor, target.getContent().rows() / rfactor);
            Imgproc.resize(base, mBase, sb, 0, 0, Imgproc.INTER_AREA);
            Imgproc.resize(target.getContent(), mPattern, sp, 0, 0, Imgproc.INTER_AREA);
            result = doFindMatch(target, mBase, mPattern);
            mMinMax = Core.minMaxLoc(result);
            downSizeWantedScore = ((int) ((target.getWantedScore() - downSimDiff) * 100)) / 100.0;
            downSizeScore = mMinMax.maxVal;
            if (downSizeScore > downSizeWantedScore) {
                downSizeFound = true;
                break;
            }
        }
        log.trace("doFind: down: %%%.2f %d msec", 100 * mMinMax.maxVal, new Date().getTime() - begin_t);
    }
    if (FindType.ONE.equals(findType) && downSizeFound) {
        // ************************************* check after downsized success
        if (base.size().equals(target.getContent().size())) {
            // trust downsized result, if images have same size
            return new FindResult(result, target);
        } else {
            int maxLocX = (int) (mMinMax.maxLoc.x * rfactor);
            int maxLocY = (int) (mMinMax.maxLoc.y * rfactor);
            begin_t = new Date().getTime();
            int margin = ((int) target.getResizeFactor()) + 1;
            Rect rectSub = new Rect(Math.max(0, maxLocX - margin), Math.max(0, maxLocY - margin),
                    Math.min(target.w + 2 * margin, base.width()),
                    Math.min(target.h + 2 * margin, base.height()));
            result = doFindMatch(target, base.submat(rectSub), null);
            mMinMax = Core.minMaxLoc(result);
            if (mMinMax.maxVal > target.getWantedScore()) {
                findResult = new FindResult(result, target, new int[] { rectSub.x, rectSub.y });
            }
            if (SX.isNotNull(findResult)) {
                log.trace("doFind: after down: %%%.2f(?%%%.2f) %d msec", mMinMax.maxVal * 100,
                        target.getWantedScore() * 100, new Date().getTime() - begin_t);
            }
        }
    }
    // ************************************** search in original
    if (((int) (100 * downSizeScore)) == 0) {
        begin_t = new Date().getTime();
        result = doFindMatch(target, base, null);
        mMinMax = Core.minMaxLoc(result);
        if (!isCheckLastSeen) {
            log.trace("doFind: search in original: %d msec", new Date().getTime() - begin_t);
        }
        if (mMinMax.maxVal > target.getWantedScore()) {
            findResult = new FindResult(result, target);
        }
    }
    log.trace("doFind: end");
    return findResult;
}

From source file:com.sikulix.core.Finder.java

License:Open Source License

private Mat doFindMatch(Element target, Mat base, Mat probe) {
    if (SX.isNull(probe)) {
        probe = target.getContent();//from ww  w  .  j a  v  a 2s  . c o  m
    }
    Mat result = new Mat();
    Mat plainBase = base;
    Mat plainProbe = probe;
    if (!target.isPlainColor()) {
        Imgproc.matchTemplate(base, probe, result, Imgproc.TM_CCOEFF_NORMED);
    } else {
        if (target.isBlack()) {
            Core.bitwise_not(base, plainBase);
            Core.bitwise_not(probe, plainProbe);
        }
        Imgproc.matchTemplate(plainBase, plainProbe, result, Imgproc.TM_SQDIFF_NORMED);
        Core.subtract(Mat.ones(result.size(), CvType.CV_32F), result, result);
    }
    return result;
}