python - 为什么要在检测文本之前 reshape MSER 轮廓?

标签 python opencv reshape text-classification mser

我正在使用来自 opencv-python 的 MSER 来检测使用此 stackoverflow question 中的代码的文本.谁能帮助我理解为什么在计算对象的凸包之前将轮廓 p reshape 为 (-1, 1, 2)?

代码如下:

import cv2
import numpy as np

#Create MSER object
mser = cv2.MSER_create()

#Your image path i-e receipt path
img = cv2.imread('/home/rafiullah/PycharmProjects/python-ocr-master/receipts/73.jpg')

#Convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

vis = img.copy()

#detect regions in gray scale image
regions, _ = mser.detectRegions(gray)

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

cv2.polylines(vis, hulls, 1, (0, 255, 0))

cv2.imshow('img', vis)

cv2.waitKey(0)

mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)

for contour in hulls:

    cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)

cv2.imshow("text only", text_only)

cv2.waitKey(0)

最佳答案

整形不整形无所谓

reshape 是不必要的。 cv2.convexHull() 可以采用任一输入格式。下图显示,无论 regions 中的 contours 是否 reshape ,结果都是一样的。

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
hulls1 = [cv2.convexHull(p) for p in regions]

they are the same

这是 p 轮廓在 reshape 时如何变化:

>>> p
array([[305, 382],
       [306, 382],
       [308, 380],
       [309, 380]...


>>> p.reshape(-1, 1, 2)
array([[[305, 382]],    
       [[306, 382]],    
       [[308, 380]],    
       [[309, 380]]...

关于python - 为什么要在检测文本之前 reshape MSER 轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673903/

相关文章:

python - 连接两个一维 NumPy 数组

opencv - OpenCV SVM训练数据集

c++ - 使用opencv减去图像

java - android reshape 所有资源文件字符串

python - "ValueError: A given column is not a column of the dataframe"尝试将分类特征转换为数值时

python - 如何将缺失的行插入到该数据集中?

python - 以正确的方式编写十六进制(在 python 中)

image - 识别音乐符号的程序

python - pandas根据变量值添加变量

r - 如何抑制来自cast()的警告消息