本文整理匯總了Python中cv2.MORPH_RECT屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.MORPH_RECT屬性的具體用法?Python cv2.MORPH_RECT怎麽用?Python cv2.MORPH_RECT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.MORPH_RECT屬性的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: getSignatureFromPage
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def getSignatureFromPage(img):
imgSize = np.shape(img)
gImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# The values for edge detection can be approximated using Otsu's Algorithm
# Reference - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.402.5899&rep=rep1&type=pdf
threshold, _ = cv2.threshold(src = gImg, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cannyImg = cv2.Canny(image = gImg, threshold1 = 0.5 * threshold, threshold2 = threshold)
# Close the image to fill blank spots so blocks of text that are close together (like the signature) are easier to detect
# Signature usually are wider and shorter so the strcturing elements used for closing will have this ratio
kernel = cv2.getStructuringElement(shape = cv2.MORPH_RECT, ksize = (30, 1))
cannyImg = cv2.morphologyEx(src = cannyImg, op = cv2.MORPH_CLOSE, kernel = kernel)
# findContours is a distructive function so the image pased is only a copy
_, contours, _ = cv2.findContours(image = cannyImg.copy(), mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_SIMPLE)
maxRect = Rect(0, 0, 0, 0)
for contour in contours:
x, y, w, h = cv2.boundingRect(points = contour)
currentArea = w * h
if currentArea > maxRect.getArea():
maxRect.set(x, y, w, h)
# Increase the bounding box to get a better view of the signature
maxRect.addPadding(imgSize = imgSize, padding = 10)
return img[maxRect.y : maxRect.y + maxRect.h, maxRect.x : maxRect.x + maxRect.w]
示例2: getSignatureFromPage
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def getSignatureFromPage(img):
imgSize = np.shape(img)
gImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# The values for edge detection can be approximated using Otsu's Algorithm
# Reference - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.402.5899&rep=rep1&type=pdf
threshold, _ = cv2.threshold(src = gImg, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cannyImg = cv2.Canny(image = gImg, threshold1 = 0.5 * threshold, threshold2 = threshold)
# Close the image to fill blank spots so blocks of text that are close together (like the signature) are easier to detect
# Signature usually are wider and shorter so the strcturing elements used for closing will have this ratio
kernel = cv2.getStructuringElement(shape = cv2.MORPH_RECT, ksize = (30, 1))
cannyImg = cv2.morphologyEx(src = cannyImg, op = cv2.MORPH_CLOSE, kernel = kernel)
# findContours is a distructive function so the image pased is only a copy
_, contours, _ = cv2.findContours(image = cannyImg.copy(), mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_SIMPLE)
maxRect = Rect(0, 0, 0, 0)
maxCorners = 0
for contour in contours:
epsilon = cv2.arcLength(contour, True)
corners = cv2.approxPolyDP(contour, 0.01 * epsilon, True)
x, y, w, h = cv2.boundingRect(points = contour)
currentArea = w * h
# Maybe add w > h ?
# if currentArea > maxRect.getArea():
if len(corners) > maxCorners:
maxCorners = len(corners)
maxRect.set(x, y, w, h)
# Increase the bounding box to get a better view of the signature
maxRect.addPadding(imgSize = imgSize, padding = 10)
return img[maxRect.y : maxRect.y + maxRect.h, maxRect.x : maxRect.x + maxRect.w]
示例3: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def __init__(self, type_of_plate, minPlateArea, maxPlateArea):
self.minPlateArea = minPlateArea # minimum area of the plate
self.maxPlateArea = maxPlateArea # maximum area of the plate
if (type_of_plate == 'RECT_PLATE'):
self.element_structure = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(22, 3))
self.type_of_plate = 0
if (type_of_plate== 'SQUARE_PLATE'):
self.element_structure = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(26, 5))
self.type_of_plate = 1
示例4: skeletonize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def skeletonize(image, size, structuring=cv2.MORPH_RECT):
# determine the area (i.e. total number of pixels in the image),
# initialize the output skeletonized image, and construct the
# morphological structuring element
area = image.shape[0] * image.shape[1]
skeleton = np.zeros(image.shape, dtype="uint8")
elem = cv2.getStructuringElement(structuring, size)
# keep looping until the erosions remove all pixels from the
# image
while True:
# erode and dilate the image using the structuring element
eroded = cv2.erode(image, elem)
temp = cv2.dilate(eroded, elem)
# subtract the temporary image from the original, eroded
# image, then take the bitwise 'or' between the skeleton
# and the temporary image
temp = cv2.subtract(image, temp)
skeleton = cv2.bitwise_or(skeleton, temp)
image = eroded.copy()
# if there are no more 'white' pixels in the image, then
# break from the loop
if area == area - cv2.countNonZero(image):
break
# return the skeletonized image
return skeleton
示例5: border
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def border(self, alpha, size, kernel_type = 'RECT'):
kdict = {'RECT':cv2.MORPH_RECT, 'ELLIPSE':cv2.MORPH_ELLIPSE,
'CROSS':cv2.MORPH_CROSS}
kernel = cv2.getStructuringElement(kdict[kernel_type], (size, size))
border = cv2.dilate(alpha, kernel, iterations = 1) # - alpha
return border
示例6: border
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def border(self, alpha, size, kernel_type='RECT'):
"""
alpha : alpha layer of the text
size : size of the kernel
kernel_type : one of [rect,ellipse,cross]
@return : alpha layer of the border (color to be added externally).
"""
kdict = {'RECT':cv.MORPH_RECT, 'ELLIPSE':cv.MORPH_ELLIPSE,
'CROSS':cv.MORPH_CROSS}
kernel = cv.getStructuringElement(kdict[kernel_type],(size,size))
border = cv.dilate(alpha,kernel,iterations=1) # - alpha
return border
示例7: build_kernel
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def build_kernel(kernel_type, kernel_size):
"""Creates the specific kernel: MORPH_ELLIPSE, MORPH_CROSS or MORPH_RECT"""
if kernel_type == cv2.MORPH_ELLIPSE:
# We build a elliptical kernel
return cv2.getStructuringElement(cv2.MORPH_ELLIPSE, kernel_size)
elif kernel_type == cv2.MORPH_CROSS:
# We build a cross-shape kernel
return cv2.getStructuringElement(cv2.MORPH_CROSS, kernel_size)
else: # cv2.MORPH_RECT
# We build a rectangular kernel:
return cv2.getStructuringElement(cv2.MORPH_RECT, kernel_size)
# This function erodes the image
示例8: detect_textarea
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def detect_textarea(self, arg):
textarea = []
small = cv2.cvtColor(arg, cv2.COLOR_RGB2GRAY)
height, width, _ = arg.shape
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel)
_, bw = cv2.threshold(
grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(
cv2.MORPH_RECT, (10, 1)) # for historical docs
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
contours, _ = cv2.findContours(
connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask = np.zeros(bw.shape, dtype=np.uint8)
for idx in range(len(contours)):
x, y, w, h = cv2.boundingRect(contours[idx])
# print x,y,w,h
mask[y:y+h, x:x+w] = 0
cv2.drawContours(mask, contours, idx, (255, 255, 255), -1)
r = float(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)
if r > 0.45 and (width*0.9) > w > 15 and (height*0.5) > h > 15:
textarea.append([x, y, x+w-1, y+h-1])
cv2.rectangle(arg, (x, y), (x+w-1, y+h-1), (0, 0, 255), 2)
if len(textarea) > 1:
textarea = self.filter_noisebox(textarea, height, width)
return textarea, arg, height, width
示例9: __init__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def __init__(self, kernel_morph_shape=cv2.MORPH_RECT, kernel_size=2, iterations=1, border=cv2.BORDER_CONSTANT, *args, **kwargs):
self.__kernel_morph_shape = kernel_morph_shape
self.__kernel_size = kernel_size
self.__iterations = iterations
self.__border = border
super(OpencvDilateFilter, self).__init__("", *args, **kwargs)
示例10: getSignatureFromPage
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_RECT [as 別名]
def getSignatureFromPage(img):
imgSize = np.shape(img)
gImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, _ = cv2.threshold(src = gImg, thresh = 0, maxval = 255, type = cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cannyImg = cv2.Canny(image = gImg, threshold1 = 0.5 * threshold, threshold2 = threshold)
# The kernel is wide as most signatures are wide
kernel = cv2.getStructuringElement(shape = cv2.MORPH_RECT, ksize = (30, 1))
cannyImg = cv2.morphologyEx(src = cannyImg, op = cv2.MORPH_CLOSE, kernel = kernel)
# findContours() is a distructive function so a copy is passed as a parameter
_, contours, _ = cv2.findContours(image = cannyImg.copy(), mode = cv2.RETR_TREE, method = cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
print 'Warning: No Signature Found'
return img
maxRect = {
'x': 0,
'y': 0,
'w': 0,
'h': 0
}
maxCorners = 0
for contour in contours:
# Perimeter accuracy
arcPercentage = 0.01
# Contour perimeter
epsilon = cv2.arcLength(curve = contour, closed = True) * arcPercentage
corners = cv2.approxPolyDP(curve = contour, epsilon = epsilon, closed = True)
x, y, w, h = cv2.boundingRect(points = corners)
if len(corners) > maxCorners:
maxCorners = len(corners)
maxRect['x'] = x
maxRect['y'] = y
maxRect['w'] = w
maxRect['h'] = h
if maxRect['w'] <= 1 or maxRect['h'] <= 1:
print 'Warning: No Signature Found'
return img
# Add padding so the signature is more visible
maxRect = addPadding(rect = maxRect, padding = 10, imgSize = imgSize)
return img[maxRect['y'] : maxRect['y'] + maxRect['h'], maxRect['x'] : maxRect['x'] + maxRect['w']]