Search in sources :

Example 11 with Mat

use of org.opencv.core.Mat in project Frankenstein by olir.

the class SlideShowInput method process.

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
    int sid = (frameId - 1) / (fps * fpSlide);
    if (sid < slides.size()) {
        Slide s = slides.get(sid);
        File[] f = s.getFiles();
        Mat img = Imgcodecs.imread(f[0].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
        img.convertTo(img, CvType.CV_8UC3);
        Imgproc.resize(img, tmpFrame, new Size((double) smallWidth, (double) smallHeight));
        Rect roi = new Rect(0, 0, smallWidth, smallHeight);
        tmpFrame.copyTo(new Mat(newFrame, roi));
        if (mode3D && f.length > 1) {
            img = Imgcodecs.imread(f[1].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
            img.convertTo(img, CvType.CV_8UC3);
            Imgproc.resize(img, tmpFrame, new Size((double) smallWidth, (double) smallHeight));
        }
        roi = new Rect(smallWidth, 0, smallWidth, smallHeight);
        tmpFrame.copyTo(new Mat(newFrame, roi));
    } else
        newFrame.setTo(new Scalar(0, 0, 0, 0));
    return newFrame;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) Size(org.opencv.core.Size) File(java.io.File) Scalar(org.opencv.core.Scalar)

Example 12 with Mat

use of org.opencv.core.Mat in project Auto.js by hyb1996.

the class ColorFinder method findColorInner.

private MatOfPoint findColorInner(ImageWrapper image, int color, int threshold, Rect rect) {
    Mat bi = new Mat();
    Scalar lowerBound = new Scalar(Color.red(color) - threshold, Color.green(color) - threshold, Color.blue(color) - threshold, 255);
    Scalar upperBound = new Scalar(Color.red(color) + threshold, Color.green(color) + threshold, Color.blue(color) + threshold, 255);
    if (rect != null) {
        Core.inRange(new Mat(image.getMat(), rect), lowerBound, upperBound, bi);
    } else {
        Core.inRange(image.getMat(), lowerBound, upperBound, bi);
    }
    Mat nonZeroPos = new Mat();
    Core.findNonZero(bi, nonZeroPos);
    if (nonZeroPos.rows() == 0 || nonZeroPos.cols() == 0) {
        return null;
    }
    return new MatOfPoint(nonZeroPos);
}
Also used : Mat(org.opencv.core.Mat) MatOfPoint(org.opencv.core.MatOfPoint) Scalar(org.opencv.core.Scalar)

Example 13 with Mat

use of org.opencv.core.Mat in project Auto.js by hyb1996.

the class TemplateMatching method buildPyramid.

public static List<Mat> buildPyramid(Mat mat, int maxLevel) {
    List<Mat> pyramid = new ArrayList<>();
    pyramid.add(mat);
    for (int i = 0; i < maxLevel; i++) {
        Mat m = new Mat((mat.rows() + 1) / 2, (mat.cols() + 1) / 2, mat.type());
        Imgproc.pyrDown(mat, m);
        pyramid.add(m);
        mat = m;
    }
    return pyramid;
}
Also used : Mat(org.opencv.core.Mat) ArrayList(java.util.ArrayList) Point(org.opencv.core.Point)

Example 14 with Mat

use of org.opencv.core.Mat in project Auto.js by hyb1996.

the class TemplateMatching method fastTemplateMatching.

/**
 * 采用图像金字塔算法快速找图
 *
 * @param img             图片
 * @param template        模板图片
 * @param matchMethod     匹配算法
 * @param weakThreshold   弱阈值。该值用于在每一轮模板匹配中检验是否继续匹配。如果相似度小于该值,则不再继续匹配。
 * @param strictThreshold 强阈值。该值用于检验最终匹配结果,以及在每一轮匹配中如果相似度大于该值则直接返回匹配结果。
 * @param maxLevel        图像金字塔的层数
 * @return
 */
public static Point fastTemplateMatching(Mat img, Mat template, int matchMethod, float weakThreshold, float strictThreshold, int maxLevel) {
    TimingLogger logger = new TimingLogger(LOG_TAG, "fast_tm");
    if (maxLevel == MAX_LEVEL_AUTO) {
        // 自动选取金字塔层数
        maxLevel = selectPyramidLevel(img, template);
        logger.addSplit("selectPyramidLevel:" + maxLevel);
    }
    // 保存每一轮匹配到模板图片在原图片的位置
    Point p = null;
    Mat matchResult;
    double similarity = 0;
    boolean isFirstMatching = true;
    for (int level = maxLevel; level >= 0; level--) {
        // 放缩图片
        Mat src = getPyramidDownAtLevel(img, level);
        Mat currentTemplate = getPyramidDownAtLevel(template, level);
        // 如果在上一轮中没有匹配到图片,则考虑是否退出匹配
        if (p == null) {
            // 如果不是第一次匹配,并且不满足shouldContinueMatching的条件,则直接退出匹配(返回null)
            if (!isFirstMatching && !shouldContinueMatching(level, maxLevel)) {
                break;
            }
            matchResult = matchTemplate(src, currentTemplate, matchMethod);
            Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
            p = bestMatched.first;
            similarity = bestMatched.second;
        } else {
            // 根据上一轮的匹配点,计算本次匹配的区域
            Rect r = getROI(p, src, currentTemplate);
            matchResult = matchTemplate(new Mat(src, r), currentTemplate, matchMethod);
            Pair<Point, Double> bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold);
            // 不满足弱阈值,返回null
            if (bestMatched.second < weakThreshold) {
            // p = null;
            // break;
            }
            p = bestMatched.first;
            similarity = bestMatched.second;
            p.x += r.x;
            p.y += r.y;
        }
        // 满足强阈值,返回当前结果
        if (similarity >= strictThreshold) {
            pyrUp(p, level);
            break;
        }
        logger.addSplit("level:" + level + " point:" + p);
        isFirstMatching = false;
    }
    logger.addSplit("result:" + p);
    logger.dumpToLog();
    if (similarity < strictThreshold) {
        return null;
    }
    return p;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) TimingLogger(android.util.TimingLogger) Point(org.opencv.core.Point) Point(org.opencv.core.Point)

Example 15 with Mat

use of org.opencv.core.Mat in project Auto.js by hyb1996.

the class TemplateMatching method matchTemplate.

public static Mat matchTemplate(Mat img, Mat temp, int match_method) {
    int result_cols = img.cols() - temp.cols() + 1;
    int result_rows = img.rows() - temp.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
    Imgproc.matchTemplate(img, temp, result, match_method);
    return result;
}
Also used : Mat(org.opencv.core.Mat) Point(org.opencv.core.Point)

Aggregations

Mat (org.opencv.core.Mat)285 Point (org.opencv.core.Point)50 ArrayList (java.util.ArrayList)45 MatOfPoint (org.opencv.core.MatOfPoint)43 MatOfKeyPoint (org.opencv.core.MatOfKeyPoint)20 Size (org.opencv.core.Size)20 KeyPoint (org.opencv.core.KeyPoint)18 Scalar (org.opencv.core.Scalar)17 Rect (org.opencv.core.Rect)16 File (java.io.File)8 RotatedRect (org.opencv.core.RotatedRect)6 BufferedImage (java.awt.image.BufferedImage)5 FilterContext (de.serviceflow.frankenstein.plugin.api.FilterContext)4 SegmentVideoFilter (de.serviceflow.frankenstein.plugin.api.SegmentVideoFilter)4 DefaultFilterContext (de.serviceflow.frankenstein.vf.DefaultFilterContext)4 VideoFilter (de.serviceflow.frankenstein.vf.VideoFilter)4 DataBufferByte (java.awt.image.DataBufferByte)4 IOException (java.io.IOException)4 FilterElement (de.serviceflow.frankenstein.vf.FilterElement)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3