python-3.x - 查找包含图案的 socks 的轮廓

标签 python-3.x opencv image-processing

我想弄清楚如何在图片上隔离不均匀的 socks 。

正如您在我的代码中看到的那样,现在我主要使用边缘检测:

主要内容:

# We import the image
image = importImage(filename)
# Save the shapes variables
height, width, _ = np.shape(image)

# Get the gray scale image in a foot shape
grayImage, bigContourArea = getFootShapeImage(image, True)
minArea = width * height / 50

# Extract all contours
contours = getAllContours(grayImage)

# Keep only the contours that are not too big nor too small
relevantContours = getRelevantContours(contours, minArea, maxArea)

getAllContours 执行以下操作:

kernel = np.ones((5, 5), np.uint8)
# Apply Canny Edge detection algorithm
# We apply a Gaussian blur first
edges = cv2.GaussianBlur(grayIm, (5, 5), 0)
# Then we apply Edge detection
edges = cv2.Canny(edges, 10, 100)
# And we do a dilatation followed by erosion to fill gaps
edges = cv2.dilate(edges, kernel, iterations=2)
edges = cv2.erode(edges, kernel, iterations=2)

_, contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

这是我的代码生成的一些图片:

因此,正如您所见, socks 的某些部分未包含在 socks 轮廓中,我尝试使用多种技术将整只 socks 包括在内,但从未成功。

我尝试了以下方法:

  • 使用 Otsu 阈值分割,Itti 的显着性(为了在图像中有 socks 的掩码并避免所有剩余部分)
  • 将较小的轮廓与较大的轮廓重新组合以创建更大的轮廓(但我无法避免采用 socks 外面的其他轮廓)

您知道我该如何继续吗?

提前致谢!我希望它足够清楚,如果您需要澄清,请直接询问。

最佳答案

为了解决这个问题,我必须执行一些颜色检测算法来检测这里用于此特殊用途的白纸。我这样做了:

# Define a mask for color I want to isolate
mask = cv2.inRange(image, lowerWhiteVals, upperWhiteVals)
# I also applied some morphological operations on the mask to make it cleaner

这是在操作前后获得的蒙版图像:

Before operations After operations

然后我通过获取蒙版上最左边的轮廓来检测图像上的纸张,并将其用作 left 边界,我还拆分纸张轮廓以获得 bottom 边界井代表。

对于 topright,我使用了我拥有的第一个 socks 轮廓,假设这一个总是至少有这两个边界,因为 socks 是这样的。

完成此操作后,我将边界内的所有轮廓都提取出来,然后通过将它们全部绘制到空白图像上并再次找到新轮廓来从中创建一个新轮廓(感谢@Alexander Reynolds)。

我还必须对我的算法进行一些微调,以便在最后获得更具代表性的 socks 轮廓,您可以在下图中看到我的最终结果,即使它不完美也绰绰有余对于这个使用 opencv 的小型试验。

Before After

感谢@Alexander 的帮助。并希望有一天它能帮助其他人!

关于python-3.x - 查找包含图案的 socks 的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47560833/

相关文章:

python-3.x - Ubuntu 16.04 Docker容器中的PySpark开发环境

java - 尝试使用 OpenCV (Java) 更改小 png 图像的颜色

python - 在OpenCv中从图像提取墙

cvFindContours 没有产生我期望的输出

android - 通过蓝牙将图像从arduino发送到android

python - 为每个文件名生成新的 MD5 哈希值

python-3.x - 来自 Python 的 AWS 网络负载均衡器后面的客户端 IP

c++ - 如何在 C++ OpenCV 中将图像 (Mat) 转换为 inputArray?

c# - 在.NET中读取佳能CR2原始图像文件

python-3.x - SQLAlchemy:调用函数并始终将返回值保存在表中