我试图检测一个区域何时有一些“漏洞”,以及它们在源图像中的位置。源图像可以是随机的,并且有超过一个孔。

我想用我的代码找到红圈区域,但我不知道从哪里开始。
我看不出有什么功能能帮到我。drawContours在那里不会有任何帮助,我认为(已经对获得这些“区域”很有用了)
有人能在Python中找到这样的方法吗?(也许不使用openCV,我可以使用任何免费的库)
谢谢!
发布于 2021-12-13 23:43:33
一种可能的方法是提取最大轮廓(这是图像中最大的“路径”),滤波标准也可能包括直接获取最外层轮廓。然后,检查不连续点,换句话说,路径的端点。实际上,您需要大量的代码。幸运的是,我开发了一个类似的算法。有两个步骤:
让我们看看代码:
# Imports
import cv2
import numpy as np
fileName = "shape01.png"
path = "D://opencvImages//"
# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Prepare a deep copy of the input for results:
inputImageCopy = inputImage.copy()
# Grayscale conversion:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Threshold via Otsu:
_, binaryImage = cv2.threshold(grayscaleImage, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# Find the biggest blob:
binaryImage = findBiggestBlob(binaryImage)第一步包括计算输入的二进制图像。然后,从那里提取最大的轮廓。我实现了一个名为findBiggestBlob的函数,它只计算图像上最大的blob,使用它的区域。它返回一个最大的孤立的blob的图像;这来自我所写的关于相同想法的一个C++实现。
这是二值图像:

这是最大的轮廓:

现在,下面是获得二值图像的骨架。骨架是二进制图像的一个版本,其中的线条已经标准化为宽度为1 pixel。这很有用,因为我们可以将图像与3 x 3内核进行转换,并寻找特定的像素模式--那些识别端点的像素模式。让我们使用OpenCV的扩展图像处理模块计算骨架:
# Compute the skeleton:
skeleton = cv2.ximgproc.thinning(binaryImage, None, 1)这是骨架:

等高线现在有宽度的1 px,所以我们可以检测端点。这种方法的主要思想是卷积产生一个非常特定的值,在输入图像中发现黑白像素的图案。我们正在寻找的值是110,但我们需要在实际卷积之前执行一些操作。有关详细信息,请参阅原始员额。这些行动是:
# Threshold the image so that white pixels get a value of 0 and
# black pixels a value of 10:
_, binaryImage = cv2.threshold(skeleton, 128, 10, cv2.THRESH_BINARY)
# Set the end-points kernel:
h = np.array([[1, 1, 1],
[1, 10, 1],
[1, 1, 1]])
# Convolve the image with the kernel:
imgFiltered = cv2.filter2D(binaryImage, -1, h)
# Extract only the end-points pixels, those with
# an intensity value of 110:
endPointsMask = np.where(imgFiltered == 110, 255, 0)
# The above operation converted the image to 32-bit float,
# convert back to 8-bit uint
endPointsMask = endPointsMask.astype(np.uint8)如果我们imshow endPointsMask,我们会得到这样的东西:

在上面的图像中,你可以隐约看到所识别的端点的位置.让我们获取这些白色像素的坐标:
# Get the coordinates of the end-points:
(Y, X) = np.where(endPointsMask == 255)最后,循环遍历这些点并绘制位于端点的一些圆圈:
# Draw the end-points:
for i in range(len(X)):
# Get coordinates:
x = X[i]
y = Y[i]
# Set circle color:
color = (0, 0, 255)
# Draw Circle
cv2.circle(inputImageCopy, (x, y), 3, color, -1)
cv2.imshow("Points", inputImageCopy)
cv2.waitKey(0)对于第一个形状,这是最后的结果:

这是findBiggestBlob函数:
def findBiggestBlob(inputImage):
# Store a copy of the input image:
biggestBlob = inputImage.copy()
# Set initial values for the
# largest contour:
largestArea = 0
largestContourIndex = 0
# Find the contours on the binary image:
contours, hierarchy = cv2.findContours(inputImage, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
# Get the largest contour in the contours list:
for i, cc in enumerate(contours):
# Find the area of the contour:
area = cv2.contourArea(cc)
# Store the index of the largest contour:
if area > largestArea:
largestArea = area
largestContourIndex = i
# Once we get the biggest blob, paint it black:
tempMat = inputImage.copy()
cv2.drawContours(tempMat, contours, largestContourIndex, (0, 0, 0), -1, 8, hierarchy)
# Erase smaller blobs:
biggestBlob = biggestBlob - tempMat
return biggestBlob发布于 2021-12-15 17:55:48
谢谢,你的第一个回答太棒了!所以,我在上面贴出的简单表格上做了这件事。现在我开始了第二步:真正的生活。
我想做的就是:

隔离地图并找到输出。一些过滤功能包括:
def highlightWalls(image):
img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsv_color3 = np.asarray([18, 98, 80])
hsv_color4 = np.asarray([28, 125, 137])
hsv_color5 = np.asarray([9, 0, 57])
hsv_color6 = np.asarray([71, 29, 134])
kernel = np.ones((3, 3))
mask1 = cv2.inRange(img_hsv, hsv_color3,hsv_color4)
mask2 = cv2.inRange(img_hsv, hsv_color5,hsv_color6)
mask = cv2.bitwise_xor(mask1, mask2)
blur = cv2.bilateralFilter(image,9,75,75)
image1= cv2.bitwise_and(blur,blur,mask=mask)
cv2.imshow("iamge1", image1)
finalMinimap = cv2.Canny(image1, 150, 200, None, 3)
img_dilate = cv2.dilate(finalMinimap, kernel, iterations=3)
cv2.imshow("walls", img_dilate)
return img_dilate给予:

骷髅现在给出:

干得好!但我错过了最后一步:移除骨架上所有的“树枝”。若否,则:

有老板的意见吗?如果你有一些想法来更好地过滤我的第一张图像,当然,让我知道!
https://stackoverflow.com/questions/70341070
复制相似问题