Python 使用OpenCV获取文本方向

Python 使用OpenCV获取文本方向,python,opencv,Python,Opencv,我想使用OpenCV或任何图像预处理库获取文本方向(即角度) 图像不包含文本倾斜,但图像本身是倾斜的 例如: 我发现了一个检测文本倾斜的代码,我想修改它来解决我的问题 # load the image from disk image = cv2.imread(args["image"]) # convert the image to grayscale and flip the foreground # and background to ensure foregroun

我想使用OpenCV或任何图像预处理库获取文本方向(即角度)

图像不包含文本倾斜,但图像本身是倾斜的

例如:

我发现了一个检测文本倾斜的代码,我想修改它来解决我的问题

# load the image from disk
image = cv2.imread(args["image"])

# convert the image to grayscale and flip the foreground
# and background to ensure foreground is now "white" and
# the background is "black"
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)

# threshold the image, setting all foreground pixels to
# 255 and all background pixels to 0
thresh = cv2.threshold(gray, 0, 255,
    cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

# grab the (x, y) coordinates of all pixel values that
# are greater than zero, then use these coordinates to
# compute a rotated bounding box that contains all
# coordinates
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]

# the `cv2.minAreaRect` function returns values in the
# range [-90, 0); as the rectangle rotates clockwise the
# returned angle trends to 0 -- in this special case we
# need to add 90 degrees to the angle
if angle < -45:
    angle = -(90 + angle)

# otherwise, just take the inverse of the angle to make
# it positive
else:
    angle = -angle
#从磁盘加载映像
image=cv2.imread(args[“image”])
#将图像转换为灰度并翻转前景
#和背景,以确保前景现在是“白色”和
#背景是“黑色”
灰色=cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)
灰色=cv2。按位\u非(灰色)
#设置图像阈值,将所有前景像素设置为
#255和所有背景像素为0
阈值=cv2。阈值(灰色,0,255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
#获取所有显示的像素值的(x,y)坐标
#大于零,然后使用这些坐标
#计算包含所有元素的旋转边界框
#坐标
coords=np.列\堆栈(np.其中(阈值>0))
角度=cv2.Minareact(坐标)[-1]
#“cv2.minareact”函数返回
#范围[-90,0);当矩形顺时针旋转时
#返回的角度趋势为0——在这种特殊情况下
#需要将角度增加90度
如果角度<-45:
角度=-(90+角度)
#否则,只需取角度的倒数即可
#这是肯定的
其他:
角度=-角度

你想分析什么类型的文本?只有简单的英语,没有图片吗?图像中包含英语单词这个问题已经得到了回答。请看这篇文章: