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 从OpenCV中的图像中删除选定元素_Python_Opencv_Image Processing_Tesseract - Fatal编程技术网

Python 从OpenCV中的图像中删除选定元素

Python 从OpenCV中的图像中删除选定元素,python,opencv,image-processing,tesseract,Python,Opencv,Image Processing,Tesseract,我有一个带有表格的图像,我想从图像中删除表格结构,以便它可以更有效地使用Tesseract。我使用以下代码在表格(和单个单元格)周围创建了一个边界,以便可以删除它 img =cv2.imread('bfir.jpg') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray,50,150,apertureSize = 3) img1 = np.ones(img.shape, dtype=np.uint8)*255 re

我有一个带有表格的图像,我想从图像中删除表格结构,以便它可以更有效地使用Tesseract。我使用以下代码在表格(和单个单元格)周围创建了一个边界,以便可以删除它

img =cv2.imread('bfir.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
img1 = np.ones(img.shape, dtype=np.uint8)*255
ret,thresh = cv2.threshold(gray,127,255,1)
(_,contours,h) = cv2.findContours(thresh,1,2)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
    if len(approx)==4:
        cv2.drawContours(img1,[cnt],0,(0,255,0),2)
这将在桌子周围绘制绿线,如图所示

接下来,我尝试了
cv2.subtract
方法从图像中减去表格,有点像这样

final_img = cv2.subtract(img1, img)
但这并没有像我预期的那样起作用,它给了我一个灰度图像,表仍然在其中

而我只想在B&W的原始图像与表删除。我是第一次使用OpenCV,所以我不知道我做错了什么,我为这篇长篇大论感到抱歉,但如果有人能帮我解决这个问题,或者告诉我如何移除桌子,我将不胜感激

编辑: 正如所建议的那样,它也可以在一开始简单地用白色绘制轮廓,但我不知道如何在不丢失预处理阶段的其余数据的情况下做到这一点

作为对该问题评论的参考,这是一个代码示例,用于定位矩形并为每个矩形创建新图像,这是一次尝试创建碎纸图片的单个图像。需要更改某些值,才能找到大小合适的矩形

还有一些跟踪图像大小的代码,这些代码由我写的50%和stackoverflow帮助的50%组成

作为对该问题评论的参考,这是一个代码示例,用于定位矩形并为每个矩形创建新图像,这是一次尝试创建碎纸图片的单个图像。需要更改某些值,才能找到大小合适的矩形

还有一些跟踪图像大小的代码,这些代码由我写的50%和stackoverflow帮助的50%组成


您可以尝试覆盖表示边框的单元格。这可以通过创建遮罩图像来完成,然后使用该图像作为覆盖原始图像中像素位置的参考

这可以通过以下方式实现:

mask_image = np.zeros(img.shape[0:2], np.uint8)    
cv2.drawContours(mask_image, contours, -1, color=255, thickness=2)
border_points = np.array(np.where(mask_image == 255)).transpose()
background = [0, 0, 0] # Change this to the colour you want
for point in border_points :
    img[point[0], point[1]] = background

更新:

您可以使用已经为遮罩创建的3通道,但这会稍微使算法复杂化。mask image propose更适合此任务,但我将尝试使其适应您的代码:

# Create your mask image as usual...
border_points = np.array(np.where(img1[:,:,1] == 255)).transpose() # Only look at channel 2
background = [0, 0, 0] # Change this to the colour you want
for point in border_points :
    img[point[0], point[1]] = background

更新按照@RobAu的建议执行(比我以前的方法更快):



请注意,我没有测试此代码。因此,它可能需要进一步修改。

您可以尝试覆盖表示边框的单元格。这可以通过创建遮罩图像来完成,然后使用该图像作为覆盖原始图像中像素位置的参考

这可以通过以下方式实现:

mask_image = np.zeros(img.shape[0:2], np.uint8)    
cv2.drawContours(mask_image, contours, -1, color=255, thickness=2)
border_points = np.array(np.where(mask_image == 255)).transpose()
background = [0, 0, 0] # Change this to the colour you want
for point in border_points :
    img[point[0], point[1]] = background

更新:

您可以使用已经为遮罩创建的3通道,但这会稍微使算法复杂化。mask image propose更适合此任务,但我将尝试使其适应您的代码:

# Create your mask image as usual...
border_points = np.array(np.where(img1[:,:,1] == 255)).transpose() # Only look at channel 2
background = [0, 0, 0] # Change this to the colour you want
for point in border_points :
    img[point[0], point[1]] = background

更新按照@RobAu的建议执行(比我以前的方法更快):



请注意,我没有测试此代码。因此,它可能需要进一步修改。

即使给定的输入图像链接不起作用&因此我显然不知道以下是您要求的内容,我从您的问题中学到了一些东西,当我正在处理,从给定图像中删除表结构行时,我想与未来的读者分享我学到的东西

我按照中提供的步骤删除这些行。 但这只消除了水平线。当我尝试删除垂直线时,结果图像只有垂直线。表中的文本不在那里

然后我遇到了你的问题&在问题中看到了
final\u img=cv2.subtract(img1,img)
。试过了&效果很好

以下是我遵循的步骤:

# Load the image
src = cv.imread(argv[0], cv.IMREAD_COLOR)
# Check if image is loaded fine
if src is None:
    print ('Error opening image: ' + argv[0])
    return -1
# Show source image
cv.imshow("src", src)
# [load_image]
# [gray]
# Transform source image to gray if it is not already
if len(src.shape) != 2:
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
else:
    gray = src
# Show gray image
# show_wait_destroy("gray", gray)
# [gray]
# [bin]
# Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
gray = cv.bitwise_not(gray)
bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                            cv.THRESH_BINARY, 15, -2)
# Show binary image
# show_wait_destroy("binary", bw)
# [bin]
# [init]
# Create the images that will use to extract the horizontal and vertical lines
horizontal = np.copy(bw)
vertical = np.copy(bw)

# [horiz]
# [vert]
# Specify size on vertical axis
rows = vertical.shape[0]
verticalsize = rows / 10
# Create structure element for extracting vertical lines through morphology operations
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
# Apply morphology operations
vertical = cv.erode(vertical, verticalStructure)
vertical = cv.dilate(vertical, verticalStructure)

# [init]
# [horiz]
# Specify size on horizontal axis
cols = horizontal.shape[1]
horizontal_size = cols / 30
# Create structure element for extracting horizontal lines through morphology operations
horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
# Apply morphology operations
horizontal = cv.erode(horizontal, horizontalStructure)
horizontal = cv.dilate(horizontal, horizontalStructure)
lines_removed = cv.subtract(gray, vertical + horizontal)

show_wait_destroy("lines_removed", ~lines_removed)
输入:

输出:

我从来源处更改的内容很少:

  • verticalsize=rows/10
    ,在这里,我不理解数字
    10
    的意义。在文件中,使用了30个。我以10分取得了更好的成绩。我猜,分区数越少,结构元素就越大&在这里,因为我们的目标是直线,减少分区数是有效的
  • 在文档中,垂直线在水平线之后处理。我颠倒了顺序
  • 我将参数交换为
    cv2.substract()
    。我使用了
    cv2.detract(img,img1)

  • 尽管如此,给定的输入图像链接不起作用&因此我显然不知道以下是您要求的内容,我从您的问题中学到了一些东西,当我正在处理从给定图像中删除表格结构行时,我想与未来的读者分享我学到的东西

    我按照中提供的步骤删除这些行。 但这只消除了水平线。当我尝试删除垂直线时,结果图像只有垂直线。表中的文本不在那里

    然后我遇到了你的问题&在问题中看到了
    final\u img=cv2.subtract(img1,img)
    。试过了&效果很好

    以下是我遵循的步骤:

    # Load the image
    src = cv.imread(argv[0], cv.IMREAD_COLOR)
    # Check if image is loaded fine
    if src is None:
        print ('Error opening image: ' + argv[0])
        return -1
    # Show source image
    cv.imshow("src", src)
    # [load_image]
    # [gray]
    # Transform source image to gray if it is not already
    if len(src.shape) != 2:
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    else:
        gray = src
    # Show gray image
    # show_wait_destroy("gray", gray)
    # [gray]
    # [bin]
    # Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
    gray = cv.bitwise_not(gray)
    bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
                                cv.THRESH_BINARY, 15, -2)
    # Show binary image
    # show_wait_destroy("binary", bw)
    # [bin]
    # [init]
    # Create the images that will use to extract the horizontal and vertical lines
    horizontal = np.copy(bw)
    vertical = np.copy(bw)
    
    # [horiz]
    # [vert]
    # Specify size on vertical axis
    rows = vertical.shape[0]
    verticalsize = rows / 10
    # Create structure element for extracting vertical lines through morphology operations
    verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
    # Apply morphology operations
    vertical = cv.erode(vertical, verticalStructure)
    vertical = cv.dilate(vertical, verticalStructure)
    
    # [init]
    # [horiz]
    # Specify size on horizontal axis
    cols = horizontal.shape[1]
    horizontal_size = cols / 30
    # Create structure element for extracting horizontal lines through morphology operations
    horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
    # Apply morphology operations
    horizontal = cv.erode(horizontal, horizontalStructure)
    horizontal = cv.dilate(horizontal, horizontalStructure)
    lines_removed = cv.subtract(gray, vertical + horizontal)
    
    show_wait_destroy("lines_removed", ~lines_removed)
    
    输入:

    输出:

    我从来源处更改的内容很少:

  • verticalsize=rows/10
    ,在这里,我不理解数字
    10
    的意义。在文件中,使用了30个。我以10分取得了更好的成绩。我猜,分区数越少,结构元素就越大&在这里,因为我们的目标是直线,减少分区数是有效的
  • 在文档中,垂直线是proc