仅从图像中提取t恤,并使用Python中的openCV去除所有其他噪波

仅从图像中提取t恤,并使用Python中的openCV去除所有其他噪波,python,opencv,image-processing,watershed,Python,Opencv,Image Processing,Watershed,我想从图像中提取一件t恤衫并去除所有其他噪声。我一直在尝试使用Python中的分水岭算法(openCV),但是我似乎无法只过滤t恤。我是在使用正确的算法,还是应该完全考虑其他问题? 我的下一步是找到一种计算t恤尺寸的方法 提前谢谢 分水岭太简单了。使用更复杂的算法,如约束随机行走。在蔡建飞教授的页面上搜索示例代码和预编译代码,因为它们太简单了。使用更复杂的算法,如约束随机行走。在蔡建飞教授的页面上搜索示例代码和预编译代码 import cv2 import numpy as np def d

我想从图像中提取一件t恤衫并去除所有其他噪声。我一直在尝试使用Python中的分水岭算法(openCV),但是我似乎无法只过滤t恤。我是在使用正确的算法,还是应该完全考虑其他问题? 我的下一步是找到一种计算t恤尺寸的方法

提前谢谢


分水岭太简单了。使用更复杂的算法,如约束随机行走。在蔡建飞教授的页面上搜索示例代码和预编译代码,因为它们太简单了。使用更复杂的算法,如约束随机行走。在蔡建飞教授的页面上搜索示例代码和预编译代码
import cv2
import numpy as np

def display(img, cmap='gray'):
-->fig = plt.figure(figsize=(12,10))
-->ax = fig.add_subplot(111)
-->ax.imshow(img,cmap='gray')


#read in image --> added hyperlink to image
img = cv2.imread("..")

#blur and grayscale img
img = cv2.medianBlur(img, 5)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#apply threshold to grayscale image + otsu's method
ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

#NOISE REMOVAL
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN, kernel, iterations=6)

#set seeds to distinguish between fore and background
sure_bg = cv2.dilate(opening, kernel, iterations = 2)
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2,5)

#foreground
ret, sure_fg = cv2.threshold(dist_transform,0.80*dist_transform.max(),255,0)

#find region between shade and core white colours
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

#create label markers for watershed algorithm to make distinction between upper and lower body
ret, markers = cv2.connectedComponents(sure_fg)
markers = markers + 1

#set unknown equal to black
markers[unknown==255] = 0 

#apply markers to algorithm
markers = cv2.watershed(img,markers)
image,contours,hierarchy = cv2.findContours(markers.copy(),cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)


# create contours
for i in range(len(contours)):
-->#external contour
-->if hierarchy[0][i][3] == -1:
 ---->cv2.drawContours(img,contours,i,255,-1)