本文整理匯總了Python中cv2.MORPH_GRADIENT屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.MORPH_GRADIENT屬性的具體用法?Python cv2.MORPH_GRADIENT怎麽用?Python cv2.MORPH_GRADIENT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.MORPH_GRADIENT屬性的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: recognize_text
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_GRADIENT [as 別名]
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(idcard, 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
示例2: morphological_gradient
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_GRADIENT [as 別名]
def morphological_gradient(image, kernel_type, kernel_size):
"""Applies the morfological gradient to the image with the specified kernel type and size"""
kernel = build_kernel(kernel_type, kernel_size)
morph_gradient = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel)
return morph_gradient
# This function closes and opens the image
示例3: detect_textarea
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_GRADIENT [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