【发布时间】:2019-06-28 00:02:36
【问题描述】:
我一直在尝试将图像覆盖在相机检测到的矩形上。我正在使用一个函数,它从我的 sd 卡 (.jpg) 返回我的图像路径,但它看起来没有正确加载它,或者至少我无法在我想要的位置显示它。
首先,我检查图像是否已加载(如果没有,我会加载它)。然后,我继续识别矩形。一旦我认出一个,我想用加载的图像在框架上覆盖那个矩形,然后将修改后的框架返回到 ImageView。
我将与您分享我在 onCameraFrame 函数中的代码(“image”是一个 Mat 对象,用于存储我从 sdcard 加载的图像,“img_path”是存储图像路径的字符串):
Mat dst = inputFrame.rgba();
//Here I check if the image is loaded
if (image.empty()){
Log.v("Image","Empty!");
image = imread(img_path, CV_LOAD_IMAGE_UNCHANGED);
} else {
//If the image is loaded, then I proceed to process the frame.
Mat gray = inputFrame.gray();
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) {
Rect r = Imgproc.boundingRect(cnt);
image.copyTo(dst.submat(r));
Log.v("Test 1",Integer.toString(image.width()));
Log.v("Test 2",Integer.toString(image.height()));
}
}
}
}
return dst;
我想查看图像的矩形,但我一直在查看未修改的相同内容。我只得到一次日志“空!”,所以它应该正确读取图像并将其存储在我的垫子上。此外,在其他 2 个日志中,我正在写入图像的行数和列数,我得到的数字大于 0。所以...
这是检查图像是否正确加载的正确方法吗? 您现在知道为什么该代码不起作用了吗?我还阅读了有关 addWeighted 函数的信息,但我尝试调整较小图像的大小,但失败了。
提前致谢!!
编辑这是我识别矩形的代码部分,我必须在框架上的矩形上渲染新图像:
//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) {
Rect r = Imgproc.boundingRect(cnt);
image.copyTo(dst.submat(r));
Log.v("Test 1",Integer.toString(image.width()));
Log.v("Test 2",Integer.toString(image.height()));
}
}
例如,您可以从 InkHunter 应用的网页查看以下图片:https://i1.wp.com/goosed.ie/wp-content/uploads/2016/10/inkhunter-cover-for-tattoo-ideas-1.jpg?fit=800%2C450&ssl=1
【问题讨论】:
-
您能否附上示例输入图像和预期输出图像以便更好地说明。还建议您在此处仅发布相关代码。
-
@ZdaR 抱歉耽搁了!!我已经用你的问题编辑了我的初始帖子。