Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于OCR的图像增强_Python_Opencv_Ocr - Fatal编程技术网

Python 用于OCR的图像增强

Python 用于OCR的图像增强,python,opencv,ocr,Python,Opencv,Ocr,[这是一张示例图像] 我想裁剪出几个其他类似彩色图像的标题文本,比如OCR。为了更好地识别标题文本,预处理图像最有效的步骤是什么。可能是您可以先尝试检测文本,然后从检测到的区域获取最大行索引并剪切它。使用opencv检测文本有多种方法。你可以试试 可能您可以先尝试检测文本,然后从检测到的区域获取最大行索引并剪切它。使用opencv检测文本有多种方法。你可以试试 注意 对于所有想要复制代码并在其他项目中使用它的人:你必须调整和适应它(特别是阈值/内核/迭代次数值)。 此版本在用户提供的映像上工

[这是一张示例图像]


我想裁剪出几个其他类似彩色图像的标题文本,比如OCR。为了更好地识别标题文本,预处理图像最有效的步骤是什么。

可能是您可以先尝试检测文本,然后从检测到的区域获取最大行索引并剪切它。使用opencv检测文本有多种方法。你可以试试

可能您可以先尝试检测文本,然后从检测到的区域获取最大行索引并剪切它。使用opencv检测文本有多种方法。你可以试试

注意

对于所有想要复制代码并在其他项目中使用它的人:你必须调整和适应它(特别是阈值/内核/迭代次数值)。 此版本在用户提供的映像上工作得最好

import cv2

image = cv2.imread("image.jpg")
image_c = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # grayscale
cv2.imshow('gray', gray)
cv2.waitKey(0)

_, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # threshold
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

dilated = cv2.dilate(thresh, kernel, iterations=13)  # dilate
cv2.imshow('dilated', dilated)
cv2.waitKey(0)

image, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # get contours

# for each contour found, draw a rectangle around it on original image
for i, contour in enumerate(contours):
    # get rectangle bounding contour
    x, y, w, h = cv2.boundingRect(contour)

    roi = image_c[y:y + h, x:x + w]

    if 50 < h < 100 or 200 < w < 420:  # these values are specific for this example

        # draw rectangle around contour on original image
        rect = cv2.rectangle(image_c, (x, y), (x + w, y + h), (255, 255, 255), 1)
        cv2.imshow('rectangles', rect)
        cv2.waitKey(0)

        cv2.imwrite('extracted{}.png'.format(i), roi)


# write original image with added contours to disk - change values above to (255,0,255) to see clearly the contours
cv2.imwrite("contoured.jpg", image_c)
导入cv2
image=cv2.imread(“image.jpg”)
image\u c=image.copy()
gray=cv2.CVT颜色(图像,cv2.COLOR_BGR2GRAY)#灰度
cv2.imshow(“灰色”,灰色)
cv2.等待键(0)
_,thresh=cv2.阈值(灰色,50255,cv2.thresh_BINARY_INV | cv2.thresh_OTSU)#阈值
cv2.imshow('thresh',thresh)
cv2.等待键(0)
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
扩张=cv2.扩张(阈值,核,迭代次数=13)#扩张
cv2.imshow(“扩张的”,扩张的)
cv2.等待键(0)
图像、轮廓、层次=cv2.查找轮廓(放大,cv2.RETR_外部,cv2.CHAIN_近似值_无)#获取轮廓
#对于找到的每个轮廓,在原始图像上围绕其绘制一个矩形
对于i,枚举中的等高线(等高线):
#获取矩形边界轮廓
x、 y,w,h=cv2.boundingRect(轮廓)
roi=图像_c[y:y+h,x:x+w]
如果50

注意

对于所有想要复制代码并在其他项目中使用它的人:你必须调整和适应它(特别是阈值/内核/迭代次数值)。 此版本在用户提供的映像上工作得最好

import cv2

image = cv2.imread("image.jpg")
image_c = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # grayscale
cv2.imshow('gray', gray)
cv2.waitKey(0)

_, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # threshold
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

dilated = cv2.dilate(thresh, kernel, iterations=13)  # dilate
cv2.imshow('dilated', dilated)
cv2.waitKey(0)

image, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # get contours

# for each contour found, draw a rectangle around it on original image
for i, contour in enumerate(contours):
    # get rectangle bounding contour
    x, y, w, h = cv2.boundingRect(contour)

    roi = image_c[y:y + h, x:x + w]

    if 50 < h < 100 or 200 < w < 420:  # these values are specific for this example

        # draw rectangle around contour on original image
        rect = cv2.rectangle(image_c, (x, y), (x + w, y + h), (255, 255, 255), 1)
        cv2.imshow('rectangles', rect)
        cv2.waitKey(0)

        cv2.imwrite('extracted{}.png'.format(i), roi)


# write original image with added contours to disk - change values above to (255,0,255) to see clearly the contours
cv2.imwrite("contoured.jpg", image_c)
导入cv2
image=cv2.imread(“image.jpg”)
image\u c=image.copy()
gray=cv2.CVT颜色(图像,cv2.COLOR_BGR2GRAY)#灰度
cv2.imshow(“灰色”,灰色)
cv2.等待键(0)
_,thresh=cv2.阈值(灰色,50255,cv2.thresh_BINARY_INV | cv2.thresh_OTSU)#阈值
cv2.imshow('thresh',thresh)
cv2.等待键(0)
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
扩张=cv2.扩张(阈值,核,迭代次数=13)#扩张
cv2.imshow(“扩张的”,扩张的)
cv2.等待键(0)
图像、轮廓、层次=cv2.查找轮廓(放大,cv2.RETR_外部,cv2.CHAIN_近似值_无)#获取轮廓
#对于找到的每个轮廓,在原始图像上围绕其绘制一个矩形
对于i,枚举中的等高线(等高线):
#获取矩形边界轮廓
x、 y,w,h=cv2.boundingRect(轮廓)
roi=图像_c[y:y+h,x:x+w]
如果50
标题文本是什么?只是文本的前4行还是最后4行?如果是前4个,哪一个?仅2-3-4?顶部1-2-3-4标题文本是什么?只是文本的前4行还是最后4行?如果是前4个,哪一个?我不明白为什么这个答案还没有被接受!我不明白为什么这个答案还没有被接受!