Python 提取边界框并将其保存为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13887863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:51:13  来源:igfitidea点击:

Extract bounding box and save it as an image

pythonimageopencvimage-processingbounding-box

提问by Edgar Andrés Margffoy Tuay

Suppose you have the following image:

假设您有以下图像:

Example:

例子:

Now I want to extract each of the independent letters into individual images. Currently, I've recovered the contours and then drew a bounding box, in this case for the character a:

现在我想将每个独立的字母提取到单独的图像中。目前,我已经恢复了轮廓,然后绘制了一个边界框,在这种情况下为字符a

Bounding box for the character 'a'

字符“a”的边界框

After this, I want to extract each of the boxes (in this case for the letter a) and save it to an image file.

在此之后,我想提取每个框(在本例中为字母a)并将其保存到图像文件中。

Expected result:

预期结果:

Result

结果

Here's my code so far:

到目前为止,这是我的代码:

import numpy as np
import cv2

im = cv2.imread('abcd.png')
im[im == 255] = 1
im[im == 0] = 255
im[im == 1] = 0
im2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(im2,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for i in range(0, len(contours)):
    if (i % 2 == 0):
       cnt = contours[i]
       #mask = np.zeros(im2.shape,np.uint8)
       #cv2.drawContours(mask,[cnt],0,255,-1)
       x,y,w,h = cv2.boundingRect(cnt)
       cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
       cv2.imshow('Features', im)
       cv2.imwrite(str(i)+'.png', im)

cv2.destroyAllWindows()

Thanks in advance.

提前致谢。

采纳答案by Andrey Kamaev

The following will give you a single letter

下面给你一个字母

letter = im[y:y+h,x:x+w]

回答by nathancy

Here's an approach:

这是一种方法:

  • Convert image to grayscale
  • Otsu's threshold to obtain a binary image
  • Find contours
  • Iterate through contours and extract ROI using Numpy slicing
  • 将图像转换为灰度
  • 获得二值图像的大津阈值
  • 查找轮廓
  • 迭代轮廓并使用 Numpy 切片提取 ROI


After finding contours, we use cv2.boundingRect()to obtain the bounding rectangle coordinates for each letter.

找到轮廓后,我们使用cv2.boundingRect()获取每个字母的边界矩形坐标。

x,y,w,h = cv2.boundingRect(c)

To extract the ROI, we use Numpy slicing

为了提取 ROI,我们使用 Numpy 切片

ROI = image[y:y+h, x:x+w]

Since we have the bounding rectangle coordinates, we can draw the green bounding boxes

由于我们有边界矩形坐标,我们可以绘制绿色边界框

cv2.rectangle(copy,(x,y),(x+w,y+h),(36,255,12),2)

Here's the detected letters

这是检测到的字母

enter image description here

在此处输入图片说明

Here's each saved letter ROI

这是每个保存的字母 ROI

enter image description here

在此处输入图片说明

import cv2

image = cv2.imread('1.png')
copy = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

ROI_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
    cv2.rectangle(copy,(x,y),(x+w,y+h),(36,255,12),2)
    ROI_number += 1

cv2.imshow('thresh', thresh)
cv2.imshow('copy', copy)
cv2.waitKey()