python - 如何填充等值线图蒙版并保留等值线内的数据并遮蔽其余部分

标签 python image opencv mask contour

我有这样的图像, enter image description here

通过使用以下代码,

import numpy as np
import cv2

# load the image
image = cv2.imread("frame50.jpg", 1)

#color boundaries [B, G, R]
lower = [0, 3, 30]
upper = [30, 117, 253]

# create NumPy arrays from the boundaries
lower = np.array(lower, dtype="uint8")
upper = np.array(upper, dtype="uint8")

# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask=mask)

ret,thresh = cv2.threshold(mask, 50, 255, 0)
if (int(cv2.__version__[0]) > 3):
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
else:
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

if len(contours) != 0:
    # find the biggest countour (c) by the area
    c = max(contours, key = cv2.contourArea)
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
cv2.imshow('ROI',ROI)
cv2.imwrite('ROI.png',ROI)

cv2.waitKey(0)

我得到以下裁剪后的图像,

enter image description here

我想保留橙色手绘边界内的所有数据,包括边界本身,并将其余部分涂黑。 在上图中,橙色边界之外的数据仍然存在。我不要那个。我如何填充现在这样的掩码,以便我可以保留橙色边界内的数据,

enter image description here

我仍然想保留其他属性,例如矩形边界框。我不想改变任何其他事情。我该怎么办? 谢谢。

最佳答案

由于您希望(在对我之前的答案的评论中)将外部区域设置为黑色而不是透明,您可以在 Python/OpenCV 中按如下方式执行此操作,更改几行以将蒙版乘以输入而不是将 mask 放入 Alpha channel 。

输入:

enter image description here

import numpy as np
import cv2

# load the image
image = cv2.imread("frame50.jpg")

#color boundaries [B, G, R]
lower = (0, 70, 210)
upper = (50, 130, 255)

# threshold on orange color
thresh = cv2.inRange(image, lower, upper)

# get largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x,y,w,h = cv2.boundingRect(big_contour)

# draw filled contour on black background
mask = np.zeros_like(image)
cv2.drawContours(mask, [big_contour], 0, (255,255,255), -1)

# apply mask to input image
new_image = cv2.bitwise_and(image, mask)

# crop 
ROI = new_image[y:y+h, x:x+w]
   
# save result 
cv2.imwrite('frame50_thresh.jpg',thresh)
cv2.imwrite('frame50_mask.jpg',mask)
cv2.imwrite('frame50_new_image2.jpg',new_image)
cv2.imwrite('frame50_roi2.jpg',ROI)

# show images
cv2.imshow('thresh',thresh)
cv2.imshow('mask',mask)
cv2.imshow('new_image',new_image)
cv2.imshow('ROI',ROI)
cv2.waitKey(0)

应用蒙版后的新图像:

enter image description here

裁剪的 ROI 图像:

enter image description here

关于python - 如何填充等值线图蒙版并保留等值线内的数据并遮蔽其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62757037/

相关文章:

python - 从python中的列表中选择数字

Python OpenCV : Image Denoising for Text Recognition

jquery - 在一页上显示多个依赖的Python模型

python - 使用 BeautifulSoup 提取标题标签

html - 如何从上传的 png 文件中打开 Google Colaboratory notebook 单元格中的图像?

ios - 单击 UIImageView SWIFT 不工作

c++ - VS 2013 OpenCV错误: Cannot find or open the PDB file

algorithm - 查找不同图像之间相似性的最准确算法是什么

python - 使用霍夫变换检测圆

python - Python 调用命令行上是否指示了脚本的路径?