def __bound_contours(roi):
"""
returns modified roi(non-destructive) and rectangles that founded by the algorithm.
@roi region of interest to find contours
@return (roi, rects)
"""
roi_copy = roi.copy()
roi_hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)
# filter black color
mask1 = cv2.inRange(roi_hsv, np.array([0, 0, 0]), np.array([180, 255, 125]))
mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))
mask1 = cv2.Canny(mask1, 100, 300)
mask1 = cv2.GaussianBlur(mask1, (1, 1), 0)
mask1 = cv2.Canny(mask1, 100, 300)
# mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
# Find contours for detected portion of the image
im2, cnts, hierarchy = cv2.findContours(mask1.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5] # get largest five contour area
rects = []
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
x, y, w, h = cv2.boundingRect(approx)
if h >= 15:
# if height is enough
# create rectangle for bounding
rect = (x, y, w, h)
rects.append(rect)
cv2.rectangle(roi_copy, (x, y), (x+w, y+h), (0, 255, 0), 1);
return (roi_copy, rects)
python类morphologyEx()的实例源码
def MoG2(vid, min_thresh=800, max_thresh=10000):
'''
Args : Video object and threshold parameters
Returns : None
'''
cap = cv2.VideoCapture(vid)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
fgbg = cv2.createBackgroundSubtractorMOG2()
connectivity = 4
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
fgmask = fgbg.apply(frame)
fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
output = cv2.connectedComponentsWithStats(
fgmask, connectivity, cv2.CV_32S)
for i in range(output[0]):
if output[2][i][4] >= min_thresh and output[2][i][4] <= max_thresh:
cv2.rectangle(frame, (output[2][i][0], output[2][i][1]), (
output[2][i][0] + output[2][i][2], output[2][i][1] + output[2][i][3]), (0, 255, 0), 2)
cv2.imshow('detection', frame)
cap.release()
cv2.destroyAllWindows()
def select_largest_obj(self, img_bin, lab_val=255, fill_holes=False,
smooth_boundary=False, kernel_size=15):
'''Select the largest object from a binary image and optionally
fill holes inside it and smooth its boundary.
Args:
img_bin (2D array): 2D numpy array of binary image.
lab_val ([int]): integer value used for the label of the largest
object. Default is 255.
fill_holes ([boolean]): whether fill the holes inside the largest
object or not. Default is false.
smooth_boundary ([boolean]): whether smooth the boundary of the
largest object using morphological opening or not. Default
is false.
kernel_size ([int]): the size of the kernel used for morphological
operation. Default is 15.
Returns:
a binary image as a mask for the largest object.
'''
n_labels, img_labeled, lab_stats, _ = \
cv2.connectedComponentsWithStats(img_bin, connectivity=8,
ltype=cv2.CV_32S)
largest_obj_lab = np.argmax(lab_stats[1:, 4]) + 1
largest_mask = np.zeros(img_bin.shape, dtype=np.uint8)
largest_mask[img_labeled == largest_obj_lab] = lab_val
# import pdb; pdb.set_trace()
if fill_holes:
bkg_locs = np.where(img_labeled == 0)
bkg_seed = (bkg_locs[0][0], bkg_locs[1][0])
img_floodfill = largest_mask.copy()
h_, w_ = largest_mask.shape
mask_ = np.zeros((h_ + 2, w_ + 2), dtype=np.uint8)
cv2.floodFill(img_floodfill, mask_, seedPoint=bkg_seed,
newVal=lab_val)
holes_mask = cv2.bitwise_not(img_floodfill) # mask of the holes.
largest_mask = largest_mask + holes_mask
if smooth_boundary:
kernel_ = np.ones((kernel_size, kernel_size), dtype=np.uint8)
largest_mask = cv2.morphologyEx(largest_mask, cv2.MORPH_OPEN,
kernel_)
return largest_mask
def __filterRedColor(image_hsv):
"""
Filters the red color from image_hsv and returns mask.
"""
mask1 = cv2.inRange(image_hsv, np.array([0, 100, 65]), np.array([10, 255, 255]))
mask2 = cv2.inRange(image_hsv, np.array([155, 100, 70]), np.array([179, 255, 255]))
mask = mask1 + mask2
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2)))
mask = cv2.Canny(mask, 50, 100)
mask = cv2.GaussianBlur(mask, (13, 13), 0)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2,2)))
return mask
def skin_detect(self, raw_yrb, img_src):
# use median blurring to remove signal noise in YCRCB domain
raw_yrb = cv2.medianBlur(raw_yrb, 5)
mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb)
# morphological transform to remove unwanted part
kernel = np.ones((5, 5), np.uint8)
#mask_skin = cv2.morphologyEx(mask_skin, cv2.MORPH_OPEN, kernel)
mask_skin = cv2.dilate(mask_skin, kernel, iterations=2)
res_skin = cv2.bitwise_and(img_src, img_src, mask=mask_skin)
#res_skin_dn = cv2.fastNlMeansDenoisingColored(res_skin, None, 10, 10, 7,21)
return res_skin
# Do background subtraction with some filtering
def animpingpong(self):
obj=self.Object
img=None
if not obj.imageFromNode:
img = cv2.imread(obj.imageFile)
else:
print "copy image ..."
img = obj.imageNode.ViewObject.Proxy.img.copy()
print "cpied"
print " loaded"
# print (obj.blockSize,obj.ksize,obj.k)
# edges = cv2.Canny(img,obj.minVal,obj.maxVal)
# color = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
# edges=color
#
kernel = np.ones((obj.xsize,obj.ysize),np.uint8)
opening = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel, iterations = obj.iterations)
if True:
print "zeige"
cv2.imshow(obj.Label,opening)
print "gezeigt"
else:
from matplotlib import pyplot as plt
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst,cmap = 'gray')
plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
plt.show()
print "fertig"
self.img=opening
def animpingpong(self):
obj=self.Object
img=None
if not obj.imageFromNode:
img = cv2.imread(obj.imageFile)
else:
print "copy image ..."
img = obj.imageNode.ViewObject.Proxy.img.copy()
print "cpied"
print " loaded"
# print (obj.blockSize,obj.ksize,obj.k)
# edges = cv2.Canny(img,obj.minVal,obj.maxVal)
# color = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
# edges=color
#
kernel = np.ones((obj.xsize,obj.ysize),np.uint8)
closing = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel, iterations = obj.iterations)
if True:
print "zeige"
cv2.imshow(obj.Label,closing)
print "gezeigt"
else:
from matplotlib import pyplot as plt
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst,cmap = 'gray')
plt.title('Corner Image'), plt.xticks([]), plt.yticks([])
plt.show()
print "fertig"
self.img=closing
def getContours(img,kernel=(10,10)):
#Define kernel
kernel = np.ones(kernel, np.uint8)
#Open to erode small patches
thresh = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
#Close little holes
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE,kernel, iterations=4)
#Find contours
#contours=skimsr.find_contours(thresh,0)
thresh=thresh.astype('uint8')
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
areas=[]
for c in contours:
areas.append(cv2.contourArea(c))
return contours,thresh,areas
def recognize_text(original):
idcard = original
gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY)
# Morphological gradient:
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
opening = cv2.morphologyEx(gray, cv2.MORPH_GRADIENT, kernel)
# Binarization
ret, binarization = cv2.threshold(opening, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# Connected horizontally oriented regions
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
connected = cv2.morphologyEx(binarization, cv2.MORPH_CLOSE, kernel)
# find countours
_, contours, hierarchy = cv2.findContours(
connected, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE
)
return contours, hierarchy
def _extract_arm(self, img):
# find center region of image frame (assume center region is 21 x 21 px)
center_half = 10 # (=(21-1)/2)
center = img[self.height/2 - center_half : self.height/2 + center_half, self.width/2 - center_half : self.width/2 + center_half]
# determine median depth value
median_val = np.median(center)
'''mask the image such that all pixels whose depth values
lie within a particular range are gray and the rest are black
'''
img = np.where(abs(img-median_val) <= self.abs_depth_dev, 128, 0).astype(np.uint8)
# Apply morphology operation to fill small holes in the image
kernel = np.ones((5,5), np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
# Find connected regions (to hand) to remove background objects
# Use floodfill with a small image area (7 x 7 px) that is set gray color value
kernel2 = 3
img[self.height/2-kernel2:self.height/2+kernel2, self.width/2-kernel2:self.width/2+kernel2] = 128
# a black mask to mask the 'non-connected' components black
mask = np.zeros((self.height + 2, self.width + 2), np.uint8)
floodImg = img.copy()
# Use floodFill function to paint the connected regions white
cv2.floodFill(floodImg, mask, (self.width/2, self.height/2), 255, flags=(4 | 255 << 8))
# apply a binary threshold to show only connected hand region
ret, floodedImg = cv2.threshold(floodImg, 129, 255, cv2.THRESH_BINARY)
return floodedImg
def update(dummy=None):
sz = cv2.getTrackbarPos('op/size', 'morphology')
iters = cv2.getTrackbarPos('iters', 'morphology')
opers = cur_mode.split('/')
if len(opers) > 1:
sz = sz - 10
op = opers[sz > 0]
sz = abs(sz)
else:
op = opers[0]
sz = sz*2+1
str_name = 'MORPH_' + cur_str_mode.upper()
oper_name = 'MORPH_' + op.upper()
st = cv2.getStructuringElement(getattr(cv2, str_name), (sz, sz))
res = cv2.morphologyEx(img, getattr(cv2, oper_name), st, iterations=iters)
draw_str(res, (10, 20), 'mode: ' + cur_mode)
draw_str(res, (10, 40), 'operation: ' + oper_name)
draw_str(res, (10, 60), 'structure: ' + str_name)
draw_str(res, (10, 80), 'ksize: %d iters: %d' % (sz, iters))
cv2.imshow('morphology', res)
def remove_noise_and_smooth(file_name):
logging.info('Removing noise and smoothening image')
img = cv2.imread(file_name, 0)
filtered = cv2.adaptiveThreshold(img.astype(np.uint8), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 41, 3)
kernel = np.ones((1, 1), np.uint8)
opening = cv2.morphologyEx(filtered, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
img = image_smoothening(img)
or_image = cv2.bitwise_or(img, closing)
return or_image
def simple_feature_size_filter(img, minradius, maxradius):
feature_radius_min = minradius | 1 # play with these to see show they affect highlighting of structures of various sizes
feature_radius_max = maxradius | 1
if 0:
w = feature_radius_min*2 | 1
blurred = cv2.GaussianBlur(img, (w, w), feature_radius_min)
w = feature_radius_max*2 | 1
veryblurred = cv2.GaussianBlur(img, (w, w), feature_radius_max)
else:
s = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (feature_radius_min, feature_radius_min))
blurred = cv2.morphologyEx(img, cv2.MORPH_OPEN, s)
s = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (feature_radius_max, feature_radius_max))
veryblurred = cv2.morphologyEx(img, cv2.MORPH_OPEN, s)
bandfiltered = blurred - np.minimum(veryblurred, blurred)
return bandfiltered
def isInvEmpty():
bag, bagx,bagy = get_bag('bag and coords', 'hsv')
# looks for color of empty inv
low = np.array([10,46,58])
high= np.array([21,92,82])
# applies mask
mask = cv2.inRange(bag, low, high)
# removes any noise
kernel = np.ones((5,5), np.uint8)
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# looks to see if the inv is all white pixels
# returns true, else False
if (closing.view() == 255).all():
return True
return False
def process_letter(thresh,output):
# assign the kernel size
kernel = np.ones((2,1), np.uint8) # vertical
# use closing morph operation then erode to narrow the image
temp_img = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=3)
# temp_img = cv2.erode(thresh,kernel,iterations=2)
letter_img = cv2.erode(temp_img,kernel,iterations=1)
# find contours
(contours, _) = cv2.findContours(letter_img.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# loop in all the contour areas
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(output,(x-1,y-5),(x+w,y+h),(0,255,0),1)
return output
#processing letter by letter boxing
def process_word(thresh,output):
# assign 2 rectangle kernel size 1 vertical and the other will be horizontal
kernel = np.ones((2,1), np.uint8)
kernel2 = np.ones((1,4), np.uint8)
# use closing morph operation but fewer iterations than the letter then erode to narrow the image
temp_img = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=2)
#temp_img = cv2.erode(thresh,kernel,iterations=2)
word_img = cv2.dilate(temp_img,kernel2,iterations=1)
(contours, _) = cv2.findContours(word_img.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(output,(x-1,y-5),(x+w,y+h),(0,255,0),1)
return output
#processing line by line boxing
def process_line(thresh,output):
# assign a rectangle kernel size 1 vertical and the other will be horizontal
kernel = np.ones((1,5), np.uint8)
kernel2 = np.ones((2,4), np.uint8)
# use closing morph operation but fewer iterations than the letter then erode to narrow the image
temp_img = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel2,iterations=2)
#temp_img = cv2.erode(thresh,kernel,iterations=2)
line_img = cv2.dilate(temp_img,kernel,iterations=5)
(contours, _) = cv2.findContours(line_img.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(output,(x-1,y-5),(x+w,y+h),(0,255,0),1)
return output
#processing par by par boxing
def _smooth_ball_mask(self, mask):
"""
The mask created inDetectBallPosition might be noisy.
:param mask: The mask to smooth (Image with bit depth 1)
:return: The smoothed mask
"""
# create the disk-shaped kernel for the following image processing,
r = 3
kernel = np.ones((2*r, 2*r), np.uint8)
for x in range(0, 2*r):
for y in range(0, 2*r):
if(x - r + 0.5)**2 + (y - r + 0.5)**2 > r**2:
kernel[x, y] = 0
# remove noise
# see http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
return mask
def SmoothFieldMask(self, mask):
# erst Close und dann DILATE führt zu guter Erkennung der Umrandung oben
kernel = np.ones((20,20),np.uint8)
kernel = np.ones((5,5),np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
#kernel = np.ones((20,20),np.uint8)
#mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel)
#kernel = np.ones((20,20),np.uint8)
mask = cv2.GaussianBlur(mask,(11,11),0)
#mask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
# plt.imshow(cv2.cvtColor(cv2.bitwise_and(self.ImgHSV,self.ImgHSV,mask=mask),cv2.COLOR_HSV2RGB),cmap="gray")
# plt.show()
return mask
def update_edge_mask(self, previous_mask, previous_line, slope_sign, thrs1, thrs2, debug):
lines = cv2.HoughLinesP(self.edge, 1, np.pi / 180, 70, minLineLength = 10, maxLineGap = 200)
lines = filter_lines(lines, self.vanishing_height, self.edge.shape[0], slope_sign)
self.lines.extend(lines)
mask = np.zeros(self.edge.shape, np.uint8)
for line in lines:
x1,y1,x2,y2 = line
cv2.line(mask, (x1,y1),(x2,y2), 255, MASK_WIDTH)
mask = cv2.addWeighted(mask, MASK_WEIGHT, previous_mask, 1 - MASK_WEIGHT, 0)
#self.current_mask *= int(255.0 / self.current_mask.max())
previous_mask = mask.copy()
_, mask = cv2.threshold(mask, 40, 255, cv2.THRESH_BINARY)
masked_edges = cv2.morphologyEx(cv2.bitwise_and(self.edge, self.edge, mask = mask), cv2.MORPH_CLOSE, np.array([[1] * EDGE_DILATION] *EDGE_DILATION))
lines2 = cv2.HoughLinesP(masked_edges, 1, np.pi / 180, 70, minLineLength = 10, maxLineGap = 200)
lines2 = filter_lines(lines2, self.vanishing_height, self.edge.shape[0], slope_sign)
self.lines2.extend(lines2)
for line in lines2:
x1,y1,x2,y2 = line
cv2.line(mask, (x1,y1),(x2,y2), 255, MASK_WIDTH)
previous_line[0] = add(previous_line[0], (x2,y2))
previous_line[1] = add(previous_line[1], (x_at_y(self.edge.shape[0]*0.6, x1, y1, x2, y2), self.edge.shape[0]*0.6))
previous_line[0] = scale(previous_line[0], 1.0 / (len(lines2) + 1))
previous_line[1] = scale(previous_line[1], 1.0 / (len(lines2) + 1))
return masked_edges, mask, previous_mask, previous_line
def execute_Morphing(proxy,obj):
try: img=obj.sourceObject.Proxy.img.copy()
except: img=cv2.imread(__dir__+'/icons/freek.png')
ks=obj.kernel
kernel = np.ones((ks,ks),np.uint8)
if obj.filter == 'dilation':
dilation = cv2.dilate(img,kernel,iterations = 1)
img=dilation
if obj.filter == 'erosion':
dilation = cv2.erode(img,kernel,iterations = 1)
img=dilation
if obj.filter == 'opening':
dilation = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
img=dilation
if obj.filter == 'closing':
dilation = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
img=dilation
obj.Proxy.img = img
#
# property functions for HoughLines
#
def denoise_foreground(img, fgmask):
img_bw = 255*(fgmask > 5).astype('uint8')
se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
se2 = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
mask = cv2.morphologyEx(img_bw, cv2.MORPH_CLOSE, se1)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, se2)
mask = np.dstack([mask, mask, mask]) / 255
img_dn = img * mask
return img_dn
def filter_smooth_thres(self, RANGE, color):
for (lower, upper) in RANGE:
lower = np.array(lower, dtype='uint8')
upper = np.array(upper, dtype='uint8')
mask_bottom = cv2.inRange(self.img_roi_bottom_hsv, lower, upper)
mask_top = cv2.inRange(self.img_roi_top_hsv, lower, upper)
blurred_bottom = cv2.medianBlur(mask_bottom, 5)
blurred_top = cv2.medianBlur(mask_top, 5)
# Morphological transformation
kernel = np.ones((2, 2), np.uint8)
smoothen_bottom = blurred_bottom #cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)
smoothen_top = blurred_top # cv2.morphologyEx(blurred, cv2.MORPH_OPEN, kernel, iterations=5)
"""
if self.debug:
cv2.imshow('mask bottom ' + color, mask_bottom)
cv2.imshow('blurred bottom' + color, blurred_bottom)
cv2.imshow('mask top ' + color, mask_top)
cv2.imshow('blurred top' + color, blurred_top)
"""
return smoothen_bottom, smoothen_top
# Gets metadata from our contours
def __call__(self, image):
"""Returns a foreground mask of the image."""
return cv2.morphologyEx(self.fgbg.apply(image), cv2.MORPH_OPEN,
self.strel)
def cv2_morph_close(binary_image, size=5):
import cv2
from skimage.morphology import disk
kernel = disk(size)
result = cv2.morphologyEx(binary_image, cv2.MORPH_CLOSE, kernel)
return result
def cv2_morph_open(binary_image, size=5):
import cv2
from skimage.morphology import disk
kernel = disk(size)
result = cv2.morphologyEx(binary_image, cv2.MORPH_OPEN, kernel)
return result
def segment(self, im):
mask = np.square(im.astype('float32') - self.bgim
).sum(axis=2) / 20
mask = np.clip(mask, 0, 255).astype('uint8')
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, self.kernel)
mask = cv2.dilate(mask, self.dilate_k)
mask = mask.astype('uint8')
return (mask > 10).astype('float32') *255
def closing(mask):
assert isinstance(mask, numpy.ndarray), 'mask must be a numpy array'
assert mask.ndim == 2, 'mask must be a greyscale image'
logger.debug("closing mask of shape {0}".format(mask.shape))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=2)
return mask
def morphology(msk):
assert isinstance(msk, numpy.ndarray), 'msk must be a numpy array'
assert msk.ndim == 2, 'msk must be a greyscale image'
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
msk = cv2.erode(msk, kernel, iterations=1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
msk = cv2.morphologyEx(msk, cv2.MORPH_CLOSE, kernel)
msk[msk < 128] = 0
msk[msk > 127] = 255
return msk
def reduce_noise_edges(im):
structuring_element = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, structuring_element)
maxed_rows = rank_filter(opening, -4, size=(1, 20))
maxed_cols = rank_filter(opening, -4, size=(20, 1))
debordered = np.minimum(np.minimum(opening, maxed_rows), maxed_cols)
return debordered