语义分割 使用Python OpenCV 进行边界轮廓处理
cv2.findContours 提取轮廓边界
import cv2
import matplotlib.pyplot as plt
img1 = cv2.imread(r"E:\My Projects\unet-pytorch-Buliiiing\results\NAJING0_4(2)-0.CT.PELVIS_ROCK_(ADU_20121220_153828421_000004.BMP")
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) # 彩色图变灰度图
_,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # 灰度图变二值图
tmp_contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 根据二值图找轮廓
# cv2.drawContours(img1,contours,-1,(0,0,255),1) # 把轮廓画在原图上(0,0,255) 表示 RGB 三通道,红色
plt.figure(figsize=(10,10))
plt.axis("off")
plt.imshow(img1)
传入一个图像,返回该图像的所有mask的边界轮廓矩阵
def extract_contour(r_img):
"""传入一个图像,返回该图像的所有mask的边界轮廓矩阵
r_img: np.ndarray
"""
img = cv2.cvtColor(np.asarray(r_img),cv2.COLOR_RGB2BGR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 彩色图变灰度图
_,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # 灰度图变二值图
contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 根据二值图找轮廓
# cv2.drawContours(img,contours,-1,(0,0,255),1) # 把轮廓画在原图上(0,0,255) 表示 RGB 三通道,红色
return contours
cv2.pointPolygonTest 判断点和多边形的关系
OpenCV的API:pointPolygonTest(contour, pt, measureDist),用来判断点和contour的位置关系
- contour: 使用接口cv2.findContours找到对应图片的contour
- point: 需要准备好需要的点,需要tuple类型
- measureDist:measureDist如果为true,则该函数估计从点到最近的轮廓边缘的有符号距离(负号为在外部,0为在边界上,正号为在内部)。 否则,该功能仅检查该点是否在轮廓内。
当measureDist设置为false时,若返回值为+1,表示点在轮廓内部,返回值为-1,表示在轮廓外部,返回值为0,表示在轮廓上。
flag = cv2.pointPolygonTest(contour,(100,100), False)


297

被折叠的 条评论
为什么被折叠?



