我应用opencv中的泛光填充函数从背景中提取前景,但图像中的某些对象未被算法识别,因此我想知道如何改进检测,以及需要进行哪些修改
image = cv2.imread(args["image"])
image = cv2.resize(image, (800, 800))
h,w,chn = image.shape
ratio = image.shape[0] / 800.0
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)
# show the original image and the edge detected image
print("STEP 1: Edge Detection")
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
warped1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped1, 11, offset = 10, method = "gaussian")
warped1 = (warped1 > T).astype("uint8") * 255
print("STEP 3: Apply perspective transform")
seed = (10, 10)
foreground, birdEye = floodFillCustom(image, seed)
cv2.circle(birdEye, seed, 50, (0, 255, 0), -1)
cv2.imshow("originalImg", birdEye)
cv2.circle(birdEye, seed, 100, (0, 255, 0), -1)
cv2.imshow("foreground", foreground)
cv2.imshow("birdEye", birdEye)
gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)
cv2.imwrite("gray.jpg", gray)
threshImg = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)[1]
h_threshold,w_threshold = threshImg.shape
area = h_threshold*w_threshold
cv2.imshow("threshImg", threshImg)[![enter image description here][1]][1]
自定义功能如下所示-
def floodFillCustom(originalImage, seed):
originalImage = np.maximum(originalImage, 10)
foreground = originalImage.copy()
cv2.floodFill(foreground, None, seed, (0, 0, 0),
loDiff=(10, 10, 10), upDiff=(10, 10, 10))
return [foreground, originalImage]
有点晚了,但这里有一个分割工具的替代解决方案。它涉及将图像转换为CMYK颜色空间并提取
K
(关键)组件。这个组件可以是thresholded
为了得到一个不错的工具二进制掩码,过程非常简单:让我们看看代码:
第一步是将
BGR
图像转换为CMYK
。OpenCV中没有直接的转换,所以我直接应用了conversion formula。我们可以从这个公式中得到每个颜色空间分量,但我们只对K
通道感兴趣。转换很容易,但是我们需要小心使用数据类型。我们需要对float
数组进行操作。获取K
通道后,我们将图像转换回unsigned 8-bit
数组,这是结果图像:让我们使用大津的阈值化方法对该图像设置阈值:
这将生成以下二进制图像:
看起来很漂亮!此外,我们可以使用
morphological closing
来稍微清理它(加入小间隙)。让我们应用大小为5 x 5
的rectangular structuring element
并使用2
迭代:其结果是:
很酷。以下内容是可选的。通过查找外部(外部)轮廓,我们可以获得每个工具的
bounding rectangles
:将生成最终图像:
相关问题 更多 >
编程相关推荐