如何修剪与给定范围匹配的数组数组中的值对?

时间:2017-02-25 15:16:01

标签: python arrays opencv numpy

我正在处理带有字典定义的图像,如下所示: This is what I have 我想摆脱相邻条目(顶部和机器人)中的那些小元素,如果它们触摸图像的上部或机器人边界并且不超过20个像素(不包括任何接触顶部或机器人的实际字母) ,如此图像所示(红色): This is what I want

我尝试这样做的方式是:  1.以灰度加载图像  2.使用 public class DbInitializer { public static void Seed(IApplicationBuilder applicationBuilder) { AppDbContext context = applicationBuilder.ApplicationServices.GetRequiredService<AppDbContext>(); if (!context.Categories.Any()) { context.AddRange( new Category { CategoryName = "First Pie", Description="Descriptionslqdfq vdfhsqdqsdhfs qsdhf" }, new Category { CategoryName = "Cheese Cackes", Description = "Descriptionslqdfq vdfhsqdqsdhfs qsdhf" }, new Category { CategoryName = "Saesonal Pie", Description = "Descriptionslqdfq vdfhsqdqsdhfs qsdhf" }); } if (!context.Pies.Any()) { context.AddRange(new Pie { Name = "Apple Pie", ShortDescrition = "short description", LongDescription = "Long description", AllegryInformation = "sflqjq", ImageUrl = "#", IsPieOfTheWeek = true, InStock = true, Price = 15.65M }, new Pie { Name = "Apple Pie", ShortDescrition = "short description", LongDescription = "Long description", AllegryInformation = "sflqjq", ImageUrl = "#", IsPieOfTheWeek = true, InStock = true, Price = 15.65M }, new Pie { Name = "Apple Pie", ShortDescrition = "short description", LongDescription = "Long description", AllegryInformation = "sflqjq", ImageUrl = "#", IsPieOfTheWeek = true, InStock = true, Price = 15.65M }, new Pie { Name = "Apple Pie", ShortDescrition = "short description", LongDescription = "Long description", AllegryInformation = "sflqjq", ImageUrl = "#", IsPieOfTheWeek = true, InStock = true, Price = 15.65M }, new Pie { Name = "Apple Pie", ShortDescrition = "short description", LongDescription = "Long description", AllegryInformation = "sflqjq", ImageUrl = "#", IsPieOfTheWeek = true, InStock = true, Price = 15.65M }); } context.SaveChanges(); } } 获取图像的轮廓  3.找到从x = 0开始但结尾不超过x = 20的轮廓  4.找到从高度-1开始到高度为21的轮廓  5.用白色绘制这些轮廓

问题是cv2.findContours返回一组坐标数组的数组。虽然我能够删除某些坐标对,但我在这里应用它很困难。

我尝试了很多方法,目前我坚持这个:

cv2.findContours

1 个答案:

答案 0 :(得分:1)

我认为没有必要在这里使用findContours

在您的情况下,我要做的是简单地迭代图像边框上的像素,并使用增长区域算法删除那些触摸边框的组件。更详细:

  • 迭代边框像素,直到找到黑色像素。
  • 初始化列表以存储像素坐标。
  • 在相邻的黑色像素上使用递归来移除它们并将它们的坐标存储在列表中。如果您的递归距图像边界的距离超过20像素,请在使用列表中存储的坐标之前停止删除像素并恢复已删除的像素。
  • 从头开始重复,直到没有剩下其他边框组件。