我正在做一个关于图像处理的大学课程项目。这是我的原图:
我想在单个文本行图像上加入附近/重叠的边界框,但我不知道如何。到目前为止,我的代码看起来像这样(感谢@HansHirse 的帮助):
import os
import cv2
import numpy as np
from scipy import stats
image = cv2.imread('example.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
#dilation
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
#find contours
ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# https://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/
def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = …
Run Code Online (Sandbox Code Playgroud) 我正在为学校做一个文本分割项目。我需要对二值图像进行水平图像投影。我想要的结果是这样的:
.
我在 Python 中使用 OpenCV。我曾经x_sum = cv2.reduce(img, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
得到总和数组,正如这个问题所建议的:图像的水平和垂直投影和这个问题:OpenCV 中的水平直方图。
我试图通过使用获得水平投影图像cv2.calcHist
,但我得到的只是一条水平线。我的代码如下:
image = cv2.imread(file_name)
x_sum = cv2.reduce(image, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
horizontal_projection=cv2.calcHist(x_sum,[0],None,[256],[0,256])
cv2.imwrite("image2.png", horizontal_projection)
Run Code Online (Sandbox Code Playgroud)
请帮助并告诉我我做错了什么。我需要我的水平投影结果就像 Quora 示例一样。