在 Python3 中彼此创建遮罩或边界
Create mask or boundary from each other in Python3
我想从有边界的图像中获取掩码,或者从掩码图像中获取边界。
这里有两张图片:
我经历了以下,我不明白那里的逻辑:
, , link3,
谢谢,
伊利亚斯
从这个 link 可以使用以下内容;
从掩码获取边界:
im = cv2.imread('mask.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
boundary[boundary > 0] = 255
plt.imsave("mask_boundary.png", boundary, cmap = "gray")
我还是不明白其中的逻辑,但是,可以从掩码创建边界,从边界创建掩码。
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
out = "mask_boundary.png"
inp = "mask.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
plt.imsave(out, boundary, cmap = "gray")
out = "boundary_mask.png"
inp = "mask_boundary.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,0,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
for i in range(1,len(contours)):
boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
plt.imsave(out, boundary, cmap = "gray")
out = "boun_mask.png"
inp = "boundary.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,0,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
for i in range(1,len(contours)):
boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
plt.imsave(out, boundary, cmap = "gray")
注意: 使用上面的蒙版图像,我首先创建了边界,然后从该边界创建了蒙版。但是,我无法将上面的所有边界都转换为遮罩,尤其是图像 edges/border 处的边界。看来边境也得有分界线啊! 如有任何帮助,我们将不胜感激!
将上面的掩码转换为边界:
现在将新边界转换为遮罩:
现在将上述边界转换为遮罩:
我想从有边界的图像中获取掩码,或者从掩码图像中获取边界。
这里有两张图片:
我经历了以下,我不明白那里的逻辑:
谢谢, 伊利亚斯
从这个 link 可以使用以下内容;
从掩码获取边界:
im = cv2.imread('mask.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
boundary[boundary > 0] = 255
plt.imsave("mask_boundary.png", boundary, cmap = "gray")
我还是不明白其中的逻辑,但是,可以从掩码创建边界,从边界创建掩码。
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
out = "mask_boundary.png"
inp = "mask.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, -1, (255,255,255), 1)
plt.imsave(out, boundary, cmap = "gray")
out = "boundary_mask.png"
inp = "mask_boundary.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,0,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
for i in range(1,len(contours)):
boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
plt.imsave(out, boundary, cmap = "gray")
out = "boun_mask.png"
inp = "boundary.png"
im = cv2.imread(inp)
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,0,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
tmp = np.zeros_like(im)
boundary = cv2.drawContours(tmp, contours, 0,(255,255,255), -1)
for i in range(1,len(contours)):
boundary = cv2.drawContours(boundary, contours, i,(255,255,255), -1)
plt.imsave(out, boundary, cmap = "gray")
注意: 使用上面的蒙版图像,我首先创建了边界,然后从该边界创建了蒙版。但是,我无法将上面的所有边界都转换为遮罩,尤其是图像 edges/border 处的边界。看来边境也得有分界线啊! 如有任何帮助,我们将不胜感激!
将上面的掩码转换为边界:
现在将新边界转换为遮罩:
现在将上述边界转换为遮罩: