如何在不覆盖货币的情况下标记感兴趣区域(伤口)?

2024-06-01 07:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在计算伤口的大小。为此,我用一枚硬币拍摄了一张图像,作为了解图像规模和计算伤口区域大小的基础。我使用分水岭算法进行分割,但货币被跟踪覆盖。有人能帮我绕过硬币,把它和伤口一起从图像上分离出来吗

输入 enter image description here

enter image description here

出口: enter image description here

enter image description here 将numpy作为np导入 进口cv2

# Read the image and perfrom an OTSU threshold
img = cv2.imread('feridatest.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# Remove hair with opening
kernel = np.ones((6,6),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)

# Combine surrounding noise with ROI
kernel = np.ones((6,6),np.uint8)
dilate = cv2.dilate(opening,kernel,iterations=3)

# Blur the image for smoother ROI
blur = cv2.blur(dilate,(5,5))

# Perform another OTSU threshold and search for biggest contour
ret, thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)

# Create a new mask for the result image
h, w = img.shape[:2]
mask = np.zeros((h, w), np.uint8)

# Draw the contour on the new mask and perform the bitwise operation
cv2.drawContours(mask, [cnt],-1, 255, -1)
res = cv2.bitwise_and(img, img, mask=mask)

# Display the result
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

Input
[![enter image description here][1]][1]

Exit:
[![enter image description here][2]][2]

Tags: andthe图像imageimgthresholdnpmask