本文整理匯總了Python中cv2.watershed方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.watershed方法的具體用法?Python cv2.watershed怎麽用?Python cv2.watershed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.watershed方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def run(self):
while cv2.getWindowProperty('img', 0) != -1 or cv2.getWindowProperty('watershed', 0) != -1:
ch = cv2.waitKey(50)
if ch == 27:
break
if ch >= ord('1') and ch <= ord('7'):
self.cur_marker = ch - ord('0')
print('marker: ', self.cur_marker)
if ch == ord(' ') or (self.sketch.dirty and self.auto_update):
self.watershed()
self.sketch.dirty = False
if ch in [ord('a'), ord('A')]:
self.auto_update = not self.auto_update
print('auto_update if', ['off', 'on'][self.auto_update])
if ch in [ord('r'), ord('R')]:
self.markers[:] = 0
self.markers_vis[:] = self.img
self.sketch.show()
cv2.destroyAllWindows()
示例2: detect_shirt
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def detect_shirt(self):
#self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8))
self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8))
cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
fg=cv2.erode(self.dst,None,iterations=2)
#cv2.imshow("fore",fg)
bg=cv2.dilate(self.dst,None,iterations=3)
_,bg=cv2.threshold(bg, 1,128,1)
#cv2.imshow("back",bg)
mark=cv2.add(fg,bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
self.m=cv2.convertScaleAbs(mark32)
_,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#cv2.imshow("final_tshirt",self.m)
cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE)
return self.m,cntr
示例3: skimage_watershed_segmentation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def skimage_watershed_segmentation(mask, kernel=k_3x3, k=1):
# mask = cv.dilate(mask, kernel, iterations=k)
distance = ndimage.distance_transform_edt(mask)
local_maxi = peak_local_max(distance, indices=False, footprint=kernel, labels=mask)
markers = measure.label(local_maxi)
labels_ws = watershed(-distance, markers, mask=mask)
if labels_ws.max() < 2:
return [mask], labels_ws
res_masks = []
for idx in range(1, labels_ws.max() + 1):
m = labels_ws == idx
if m.sum() > 20:
res_masks.append(m.astype(np.uint8))
return res_masks, labels_ws
示例4: run
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def run(self):
while True:
ch = 0xFF & cv2.waitKey(50)
if ch == 27:
break
if ch >= ord('1') and ch <= ord('7'):
self.cur_marker = ch - ord('0')
print 'marker: ', self.cur_marker
if ch == ord(' ') or (self.sketch.dirty and self.auto_update):
self.watershed()
self.sketch.dirty = False
if ch in [ord('a'), ord('A')]:
self.auto_update = not self.auto_update
print 'auto_update if', ['off', 'on'][self.auto_update]
if ch in [ord('r'), ord('R')]:
self.markers[:] = 0
self.markers_vis[:] = self.img
self.sketch.show()
cv2.destroyAllWindows()
示例5: watershed
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def watershed(self):
m = self.markers.copy()
cv2.watershed(self.img, m)
overlay = self.colors[np.maximum(m, 0)]
vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3)
cv2.imshow('watershed', vis)
示例6: detect_shirt2
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def detect_shirt2(self):
self.hsv=cv2.cvtColor(self.norm_rgb,cv.CV_BGR2HSV)
self.hue,s,_=cv2.split(self.hsv)
_,self.dst=cv2.threshold(self.hue,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
self.fg=cv2.erode(self.dst,None,iterations=3)
self.bg=cv2.dilate(self.dst,None,iterations=1)
_,self.bg=cv2.threshold(self.bg,1,128,1)
mark=cv2.add(self.fg,self.bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
m=cv2.convertScaleAbs(mark32)
_,m=cv2.threshold(m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cntr,h=cv2.findContours(m,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
print len(cntr)
#print cntr[0].shape
#cntr[1].dtype=np.float32
#ret=cv2.contourArea(np.array(cntr[1]))
#print ret
#cntr[0].dtype=np.uint8
cv2.drawContours(m,cntr,-1,(255,255,255),3)
cv2.imshow("mask_fg",self.fg)
cv2.imshow("mask_bg",self.bg)
cv2.imshow("mark",m)
示例7: subtract_back
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def subtract_back(self,frm):
#dst=self.__back__-self.__foreground__
temp=np.zeros((config.height,config.width),np.uint8)
self.__foreground__=cv2.blur(self.__foreground__,(3,3))
dst=cv2.absdiff(self.__back__,self.__foreground__)
#dst=cv2.adaptiveThreshold(dst,255,cv.CV_THRESH_BINARY,cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C,5,10)
val,dst=cv2.threshold(dst,0,255,cv.CV_THRESH_BINARY+cv.CV_THRESH_OTSU)
fg=cv2.erode(dst,None,iterations=1)
bg=cv2.dilate(dst,None,iterations=4)
_,bg=cv2.threshold(bg,1,128,1)
mark=cv2.add(fg,bg)
mark32=np.int32(mark)
#dst.copy(temp)
#seq=cv.FindContours(cv.fromarray(dst),self.mem,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
#cntr,h=cv2.findContours(dst,cv.CV_RETR_EXTERNAL,cv.CV_CHAIN_APPROX_SIMPLE)
#print cntr,h
#cv.DrawContours(cv.fromarray(temp),seq,(255,255,255),(255,255,255),1,cv.CV_FILLED)
cv2.watershed(frm, mark32)
self.final_mask=cv2.convertScaleAbs(mark32)
#print temp
#--outputs---
#cv2.imshow("subtraction",fg)
#cv2.imshow("thres",dst)
#cv2.imshow("thres1",bg)
#cv2.imshow("mark",mark)
#cv2.imshow("final",self.final_mask)
示例8: opencv_segmentation
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def opencv_segmentation(mask, kernel=k_3x3, k=3):
# noise removal
opening = cv.morphologyEx(mask, cv.MORPH_OPEN, kernel, iterations=k)
# sure background area
sure_bg = cv.dilate(opening, kernel, iterations=k)
# Finding sure foreground area
dist_transform = cv.distanceTransform(opening,cv.DIST_L2, 5)
ret, sure_fg = cv.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv.subtract(sure_bg, sure_fg)
# Marker labelling
ret, markers = cv.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# Now, mark the region of unknown with zero
markers[unknown > 0] = 0
labels_ws = cv.watershed(cv.cvtColor(mask, cv.COLOR_GRAY2RGB), markers)
if labels_ws.max() - 1 < 2:
return [mask], labels_ws
res_masks = []
for idx in range(2, labels_ws.max() + 1):
m = labels_ws == idx
if m.sum() > 5:
m = cv.dilate(m.astype(np.uint8), kernel, iterations=1)
res_masks.append(m)
return res_masks, labels_ws
示例9: watershed
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def watershed(rgb, idx, mask):
'''
Get watershed transform from image
'''
# kernel definition
kernel = np.ones((3, 3), np.uint8)
# sure background area
sure_bg = cv2.dilate(mask, kernel)
sure_bg = np.uint8(sure_bg)
# util.im_gray_plt(sure_bg,"sure back")
# Finding sure foreground area
dist_transform = cv2.distanceTransform(np.uint8(mask), cv2.DIST_L2, 3)
# util.im_gray_plt(dist_transform,"dist transform")
ret, sure_fg = cv2.threshold(
dist_transform, 0.5 * dist_transform.max(), 255, 0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
# util.im_gray_plt(sure_fg,"sure fore")
unknown = cv2.subtract(sure_bg, sure_fg)
# util.im_gray_plt(unknown,"unknown")
# marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# mark the region of unknown with zero
markers[unknown == 255] = 0
# util.im_gray_plt(np.uint8(markers),"markers")
# apply watershed
markers = cv2.watershed(rgb, markers)
# create limit mask
mask = np.zeros(mask.shape, np.uint8)
mask[markers == -1] = 255
return mask
示例10: watershed
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def watershed(src):
# Change color to gray scale
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Use the Otsu's binarization
thresh,bin_img = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
# print(thresh) # print threshold
# Noise removal
kernel = np.ones((3,3), np.uint8)
opening = cv2.morphologyEx(bin_img,cv2.MORPH_OPEN,kernel,iterations = 2)
# Sure background area
sure_bg = cv2.dilate(opening,kernel,iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
# Finding unknown region
sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)
# Marker labelling
ret, markers = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers+1
# Now, mark the region of unknown with zero
markers[unknown==255] = 0
# Apply watershed
markers = cv2.watershed(src,markers)
src[markers == -1] = [255,0,0]
# Check marker (If check markers, please import matplotlib)
# plt.imshow(markers)
# plt.show()
# Check markers data
# print(np.unique(markers,return_counts=True))
return markers, src
示例11: watershed
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def watershed(src):
"""
Performs a marker-based image segmentation using the watershed algorithm.
:param src: 8-bit 1-channel image.
:return: 32-bit single-channel image (map) of markers.
"""
# cv2.imwrite('{}.png'.format(np.random.randint(1000)), src)
gray = src.copy()
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# h, w = gray.shape[:2]
# block_size = (min(h, w) // 4 + 1) * 2 + 1
# thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, block_size, 0)
_ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# noise removal
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)
# sure background area
sure_bg = cv2.dilate(opening, kernel, iterations=3)
# Finding sure foreground area
dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
# dist_transform = opening & gray
# cv2.imshow('dist_transform', dist_transform)
# _ret, sure_bg = cv2.threshold(dist_transform, 0.2 * dist_transform.max(), 255, cv2.THRESH_BINARY_INV)
_ret, sure_fg = cv2.threshold(dist_transform, 0.2 * dist_transform.max(), 255, cv2.THRESH_BINARY)
# Finding unknown region
# sure_bg = np.uint8(sure_bg)
sure_fg = np.uint8(sure_fg)
# cv2.imshow('sure_fg', sure_fg)
unknown = cv2.subtract(sure_bg, sure_fg)
# Marker label
lingret, marker_map = cv2.connectedComponents(sure_fg)
# Add one to all labels so that sure background is not 0, but 1
marker_map = marker_map + 1
# Now, mark the region of unknown with zero
marker_map[unknown == 255] = 0
marker_map = cv2.watershed(img, marker_map)
return marker_map
示例12: split
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def split(self):
"""Split the region according to the normalized cut criterion.
Returns
-------
list of CutRegion
The regions created by splitting.
float
The normalized cut cost.
"""
if not cv2_available:
raise ImportError('OpenCV >= 2.4.8 required')
tmp_im = np.zeros(self.shape[0] * self.shape[1])
tmp_im[self.indices] = 1
labeled_array, num_features = ndimage.label(tmp_im.reshape(self.shape))
if num_features > 1:
labeled_array = labeled_array.reshape(-1)[self.indices]
segments = []
for i in range(1, num_features + 1):
idx = np.nonzero(labeled_array == i)[0]
segments.append(CutRegion(self.affinity_matrix[idx, :][:, idx],
self.indices[idx], self.shape))
return segments, 0.0
C = normcut_vectors(self.affinity_matrix, 1)
im = C[:, -2]
im -= im.min()
im /= im.max()
markers = -np.ones(self.shape[0] * self.shape[1]).astype('uint16')
markers[self.indices] = 0
markers[self.indices[im < 0.02]] = 1
markers[self.indices[im > 0.98]] = 2
markers = markers.reshape(self.shape)
vis2 = 0.5 * np.ones(self.shape[0] * self.shape[1])
vis2[self.indices] = im
vis2 *= (2 ** 8 - 1)
vis2 = cv2.cvtColor(np.uint8(vis2.reshape(self.shape)),
cv2.COLOR_GRAY2BGR)
markers = np.int32(markers)
cv2.watershed(vis2, markers)
cut = ndimage.morphology.binary_dilation(markers == 2).reshape(-1)[
self.indices]
cut[im > 0.98] = True
cost = self._normalized_cut_cost(cut)
for thresh in np.linspace(0, 1, 20)[1:-1]:
tmp_cut = im > thresh
tmp_cost = self._normalized_cut_cost(tmp_cut)
if tmp_cost < cost:
cost = tmp_cost
cut = tmp_cut
a = cut.nonzero()[0]
a = cut.nonzero()[0]
b = np.logical_not(cut).nonzero()[0]
return (
[CutRegion(self.affinity_matrix[x, :][:, x], self.indices[x],
self.shape) for x in [a, b] if len(x)],
self._normalized_cut_cost(cut)
)
示例13: post_process_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import watershed [as 別名]
def post_process_image(image, mask, contour):
""" Watershed on the markers generated on the sure foreground to find all disconnected objects
The (mask - contour) is the true foreground. We set the contour to be unknown area.
Index of contour = -1
Index of unkown area = 0
Index of background = 1 -> set back to 0 after watershed
Index of found objects > 1
"""
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
new_contour = (contour*255).astype(np.uint8)
new_mask = (mask*255).astype(np.uint8)
new_mask = cv2.morphologyEx(new_mask, cv2.MORPH_OPEN, kernel, iterations=1)
_, thresh_mask = cv2.threshold(new_mask,0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, thresh_contour = cv2.threshold(new_contour,0,255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
sure_background = cv2.dilate(thresh_mask,kernel,iterations=3)
sure_foreground = cv2.subtract(thresh_mask, thresh_contour)
mask_plus_contour = cv2.add(thresh_mask, thresh_contour)
mask_plus_contour = cv2.cvtColor(mask_plus_contour, cv2.COLOR_GRAY2RGB)
unknown = cv2.subtract(sure_background, sure_foreground)
# Marker labelling
output = cv2.connectedComponentsWithStats(sure_foreground)
labels = output[1]
stats = output[2]
# Add one to all labels so that sure background is not 0, 0 is considered unknown by watershed
# this way, watershed can distinguish unknown from the background
labels = labels + 1
labels[unknown==255] = 0
try:
# random walker on thresh_mask leads a lot higher mean IoU but lower LB
#labels = random_walker(thresh_mask, labels)
# random walker on thresh_mask leads lower mean IoU but higher LB
labels = random_walker(mask_plus_contour, labels, multichannel=True)
except:
labels = cv2.watershed(mask_plus_contour, labels)
labels[labels==-1] = 0
labels[labels==1] = 0
labels = labels -1
labels[labels==-1] = 0
# discard nuclei which are too big or too small
mean = np.mean(stats[1:,cv2.CC_STAT_AREA])
for i in range(1, labels.max()):
if stats[i, cv2.CC_STAT_AREA] > mean*10 or stats[i, cv2.CC_STAT_AREA] < mean/10:
labels[labels==i] = 0
labels = renumber_labels(labels)
return labels