【发布时间】:2019-06-18 19:50:24
【问题描述】:
我正在尝试在实时帧上覆盖用户先前选择的小图像。我已经从以前的活动中获得了图像的路径。我的问题是我无法在框架上显示图像。
我正在尝试检测框架上的矩形,并在矩形上显示所选图像。我可以检测到矩形,但现在我无法在框架的任何部分显示图像(我现在不关心矩形)。 我一直在尝试使用 Adding Image Overlay OpenCV for Android 和 add watermark small image to large image opencv4android 的解释来做到这一点,但它对我没有用。
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat gray = inputFrame.gray();
Mat dst = inputFrame.rgba();
Imgproc.pyrDown(gray, dsIMG, new Size(gray.cols() / 2, gray.rows() / 2));
Imgproc.pyrUp(dsIMG, usIMG, gray.size());
Imgproc.Canny(usIMG, bwIMG, 0, threshold);
Imgproc.dilate(bwIMG, bwIMG, new Mat(), new Point(-1, 1), 1);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
cIMG = bwIMG.clone();
Imgproc.findContours(cIMG, contours, hovIMG, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (MatOfPoint cnt : contours) {
MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());
Imgproc.approxPolyDP(curve, approxCurve, 0.02 * Imgproc.arcLength(curve, true), true);
int numberVertices = (int) approxCurve.total();
double contourArea = Imgproc.contourArea(cnt);
if (Math.abs(contourArea) < 100) {
continue;
}
//Rectangle detected
if (numberVertices >= 4 && numberVertices <= 6) {
List<Double> cos = new ArrayList<>();
for (int j = 2; j < numberVertices + 1; j++) {
cos.add(angle(approxCurve.toArray()[j % numberVertices], approxCurve.toArray()[j - 2], approxCurve.toArray()[j - 1]));
}
Collections.sort(cos);
double mincos = cos.get(0);
double maxcos = cos.get(cos.size() - 1);
if (numberVertices == 4 && mincos >= -0.3 && maxcos <= 0.5) {
//Small watermark image
Mat a = imread(img_path);
Mat bSubmat = dst.submat(0, dst.rows() -1 , 0, dst.cols()-1);
a.copyTo(bSubmat);
}
}
}
return dst;
}
注意:img_path 是我要在框架上显示的所选图像的路径。我是从之前的活动中得到的。
现在,我只想在框架上显示图像。稍后,我将尝试在找到矩形的相同位置显示它。 拜托,欢迎任何建议或建议,因为我是 OpenCV 的新手。我对我的英语感到抱歉,但如果我没有正确解释,请随时问我。我会尽力解释得更好。 非常感谢!
【问题讨论】: