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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations