你描述的可以通过morphology实现。形态学操作是影响图像整体形状的一组(逻辑)操作。它可以“扩展”或“缩小”形状区域,以及许多其他 cool operations。
让我们使用膨胀来扩展图像的形状:
# Imports
import cv2
import numpy as np
# Read image
imagePath = "D://opencvImages//"
inputImage = cv2.imread(imagePath+"lungs.png")
# Convert BGR back to grayscale:
grayInput = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Threshold via Otsu + bias adjustment:
threshValue, binaryImage = cv2.threshold(grayInput, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
第一个位会将您发布的图像转换为二值图像,因为形态学运算只能在 1 通道图像(也是灰度)上执行,但由于我们将应用基本膨胀,因此二值图像就足够了。这是上面sn-p的结果:
让我们应用dilation。该操作可以连续应用,因此您可以指定多个iterations。既然你想要一个有点强烈的效果,让我们试试10 iterations。该操作需要名为“Structuring Element”(SE)的第二个操作数,它选择形状的已定义子区域中的像素。 SEs 有多种类型。最常见的一种是3 x 3rectangular SE:
# Set morph operation iterations:
opIterations = 10
# Set Structuring Element size:
structuringElementSize = (3, 3)
# Set Structuring element shape:
structuringElementShape = cv2.MORPH_RECT
# Get the Structuring Element:
structuringElement = cv2.getStructuringElement(structuringElementShape, structuringElementSize)
# Perform Dilate:
dilateImg = cv2.morphologyEx(binaryImage, cv2.MORPH_DILATE, structuringElement, None, None, opIterations, cv2.BORDER_REFLECT101)
# Show the image:
cv2.imshow("dilateImg", dilateImg)
cv2.waitKey(0)
这是结果: