python 边缘检测 - 屏蔽完全黑色的区域
python edge detector - mask the area were it's completly black
我在图像上使用了 canny 边缘检测器。
它检测到图像中的某些区域,而其他区域则什么也不显示。
现在,我希望它能在原始图像上遮盖完全黑色的区域。
我该怎么做?
我正在使用 python 和 skimage 或 opencv(无论哪个)
from skimage.feature import canny
from skimage.morphology import closing
import skimage.io
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
img = skimage.io.imread("test.jpg",as_grey=True)
fig, ax = plt.subplots(1, 1, figsize=(20,20))
ax.imshow(img,'gray')
ax.set_axis_off()
plt.show()
edges = canny(img)
close = closing(edges)
fig, ax = plt.subplots(1, 1, figsize=(20,20))
ax.imshow(close,'gray')
ax.set_axis_off()
plt.show()
现在我想要的是白色部分(在第二个图像中)将是原始图像中唯一显示的部分(遮罩)
您可以使用以下方法简单地在 RGB 图像上应用二进制掩码:
close_BGR = cv2.cvtColor(close, cv2.COLOR_GRAY2BGR)
# Assuming that the img is of RGB format
masked_image = cv2.min(close_BGR, img)
我在图像上使用了 canny 边缘检测器。 它检测到图像中的某些区域,而其他区域则什么也不显示。 现在,我希望它能在原始图像上遮盖完全黑色的区域。 我该怎么做?
我正在使用 python 和 skimage 或 opencv(无论哪个)
from skimage.feature import canny
from skimage.morphology import closing
import skimage.io
import numpy as np
import os
import matplotlib.pyplot as plt
import cv2
img = skimage.io.imread("test.jpg",as_grey=True)
fig, ax = plt.subplots(1, 1, figsize=(20,20))
ax.imshow(img,'gray')
ax.set_axis_off()
plt.show()
edges = canny(img)
close = closing(edges)
fig, ax = plt.subplots(1, 1, figsize=(20,20))
ax.imshow(close,'gray')
ax.set_axis_off()
plt.show()
现在我想要的是白色部分(在第二个图像中)将是原始图像中唯一显示的部分(遮罩)
您可以使用以下方法简单地在 RGB 图像上应用二进制掩码:
close_BGR = cv2.cvtColor(close, cv2.COLOR_GRAY2BGR)
# Assuming that the img is of RGB format
masked_image = cv2.min(close_BGR, img)