c# - 如何在图像中找到图像?
问题描述
如何在图像中找到图像?
现在我正在使用 EmguCV,并根据本教程编写了以下代码: https ://www.emgu.com/wiki/index.php/Shape_(Triangle,_Rectangle,_Circle,_Line)_Detection_in_CSharp
using (UMat gray = new UMat())
using (UMat cannyEdges = new UMat())
{
CvInvoke.CvtColor(img, gray, ColorConversion.Bgr2Gray);
CvInvoke.GaussianBlur(gray, gray, new Size(3, 3), 2);
CvInvoke.Canny(gray, cannyEdges, 0, 10, 3);
LineSegment2D[] lines = CvInvoke.HoughLinesP(
cannyEdges,
1, //Distance resolution in pixel-related units
Math.PI / 2, //Angle resolution measured in radians.
0, //threshold
40, //min Line width
1); //gap between lines
foreach (LineSegment2D line in lines)
{
CvInvoke.Line(img, line.P1, line.P2, new Bgr(Color.Red).MCvScalar, 1);
}
}
这是迄今为止我最好的结果,但它并不完美,因为我仍然需要一些边缘才能在图像周围创建所有边界框。
我想要的是找到图像中每个图像的所有边缘,这样我就可以像这样制作完美的边界框。
我是 EmguCV/OpenCV 的新手,但我仍然认为我最好的选择是在这个库上解决这个问题。我只需要找到正确的工具并正确使用它们,这就是我希望这里有人可以帮助我的原因:)
解决方案
以下是您可以执行的操作:
- 加载图像
- 反转 -
img.Not()
- 转换为灰度 -
img.Convert<Gray,byte>()
- 执行二进制阈值 -
img.Convert<Gray, byte>().ThresholdBinary(new Gray(54), new Gray(255))
获取凸包和边界框。执行筛选,以便获得符合条件的对象。您可以使用轮廓面积、周长等。
var Contours = new List<Contour<Point>>(); for (Contour<Point> contours = _gray.FindContours( HAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL); contours != null; contours = contours.HNext) { Seq<Point> pts = contours.GetConvexHull(ORIENTATION.CV_CLOCKWISE); double diff = Math.Round(Math.Abs(pts.Area - contours.Area) / pts.Area, 2); //additional constraint double q = contours.Area / contours.Perimeter; //bounding box of the counter Rectangle rect = contours.BoundingRectangle; //customize the value to suit your need if (contours.BoundingRectangle.Height > 5 && contours.BoundingRectangle.Width > 5) { Contours.Add(contours); } }
做你想做的事
Contours
,(例如填充并用作掩码以提取每个图像,绘制边界框等)
推荐阅读
- reactjs - 使用 axios 发布时反应“未定义”
- ajax - Wicket 7 中的 Ajax 单选按钮
- android - 即使在卸载应用程序后,如何写入 android 中仍然存在的外部存储?
- linux - 将 Unix 时间戳转换为人类可读的
- c++ - 嵌套类名在封闭类模板中使用时是否被视为当前实例化
- ubuntu - 如何在 Ubuntu 服务器上使用 PM2 和 Nuxt 配置 Nginx?
- python - RelatedObjectDoesNotExist at /register/ 用户没有扩展用户模型和配置文件的配置文件
- mysql - 带有附加变量检查的左连接
- regex - 更改 xml 文件中的 & 字符
- python - 在 pytorch 上添加验证数据