【发布时间】:2017-12-04 17:05:02
【问题描述】:
我正在尝试在 Xamarin 上开发移动应用程序。首先,我正在为 android 设备做这件事。我希望 Oncamera 功能能够自动检测轮廓并测量对象的大小。作为主要步骤,我试图实时检测轮廓。阅读大量表格和许多文件,但没有任何帮助
public Mat OnCameraFrame(CameraBridgeViewBase.ICvCameraViewFrame inputFrame)
{
Mat input = inputFrame.Rgba();
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat gray = new Mat();
//Mat hierarchy = new Mat();
Imgproc.CvtColor(p0: input, p1: gray, p2: Imgproc.ColorRgb2gray);
Mat blur = new Mat();
Imgproc.GaussianBlur(gray, blur, new Size(7, 7), -2);
Mat thresh = new Mat();
Imgproc.Threshold(blur, thresh, 127, 250, Imgproc.ThreshBinary);
Mat edged = new Mat();
Imgproc.Canny(thresh, thresh, 25, 50);
Imgproc.Dilate(thresh, thresh, new Mat(), new Point(-1, 1), 1);
Mat hierarchy = thresh.Clone();
Imgproc.FindContours(hierarchy, contours, new Mat(),
Imgproc.RetrExternal, Imgproc.ChainApproxNone);
Java.Lang.JavaSystem.Out.Println("contours" + contours);
if (contours != null)
{
Java.Lang.JavaSystem.Out.Println("found contours");
for (int i = 0; i < contours.Count(); i++)
{
Imgproc.DrawContours(input, contours, i, new Scalar(255, 0, 0), -1);
}
}
else
{
Java.Lang.JavaSystem.Out.Println("no contours");
}
return input;
我在代码中使用了上述逻辑。但是我在应用程序中的输出显示的是没有绘制任何轮廓的正常图像。如果我返回“thresh”,那么精明的边缘检测就可以完美地工作。但 Drawcontours 没有显示任何内容。 我使用 Contours.count() 因为我的 Xamarin ide 显示 contours.Size() 错误;
【问题讨论】:
-
您不需要遍历所有轮廓来绘制它们。删除
for循环并将i替换为-1,这将绘制所有轮廓。 -
@Jyr 我也试过这样。但我无法实时绘制任何轮廓。我不明白哪里出了问题!
-
@Shofwan Amrullah 你能看看这个吗?
标签: java android opencv xamarin