Python 从边界框中提取轮廓

Python 从边界框中提取轮廓,python,opencv,bounding-box,Python,Opencv,Bounding Box,我发现了这个关于SWT轮廓的例子: 在我的示例(下面)中,效果非常好,但我还需要从代码中得到一件事:它检测到的矩形(在文本内部)应该一个接一个地提取出来 有了下面的代码,我怎么能做到这一点?我想做一个循环,但我不知道怎么做 import cv2 image = cv2.imread("card.png") gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale _,thresh = cv2.threshold(gray,150,25

我发现了这个关于SWT轮廓的例子:

在我的示例(下面)中,效果非常好,但我还需要从代码中得到一件事:它检测到的矩形(在文本内部)应该一个接一个地提取出来

有了下面的代码,我怎么能做到这一点?我想做一个循环,但我不知道怎么做

import cv2

image = cv2.imread("card.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, 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 contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

# write original image with added contours to disk  
cv2.imwrite("contoured.jpg", image)
导入cv2
image=cv2.imread(“card.png”)
gray=cv2.CVT颜色(图像,cv2.COLOR_BGR2GRAY)#灰度
_,thresh=cv2.阈值(灰色,150255,cv2.thresh_BINARY_INV)#阈值
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
扩张=cv2.扩张(阈值,核,迭代次数=13)#扩张
_,轮廓,层次=cv2.查找轮廓(扩展,cv2.外部翻新,cv2.链约无)#获取轮廓
#对于找到的每个轮廓,在原始图像上围绕其绘制一个矩形
对于等高线中的等高线:
#获取矩形边界轮廓
[x,y,w,h]=cv2.boundingRect(轮廓)
#丢弃过大的区域
如果h>300且w>300:
持续
#丢弃过小的区域

如果h首先使用Morpologic运算确保所有数字格式正确并去除噪声,然后使用findcontour函数分别获取每个数字

首先使用Morpologic运算确保所有数字格式正确并去除噪声,然后使用findcontour函数获取每个数字分别使用此代码执行此任务。它检测图像中的文本/数字区域

import cv2

image = cv2.imread("C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours


idx =0
# for each contour found, draw a rectangle around it on original image
for contour in contours:

    idx += 1

    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    #cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

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

    cv2.imwrite('C:\\Users\\Bob\\Desktop\\' + str(idx) + '.jpg', roi)

    cv2.imshow('img',roi)
    cv2.waitKey(0)
导入cv2
image=cv2.imread(“C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png”)
gray=cv2.CVT颜色(图像,cv2.COLOR_BGR2GRAY)#灰度
_,thresh=cv2.阈值(灰色,150255,cv2.thresh_BINARY_INV)#阈值
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
扩张=cv2.扩张(阈值,核,迭代次数=13)#扩张
_,轮廓,层次=cv2.查找轮廓(扩展,cv2.外部翻新,cv2.链约无)#获取轮廓
idx=0
#对于找到的每个轮廓,在原始图像上围绕其绘制一个矩形
对于等高线中的等高线:
idx+=1
#获取矩形边界轮廓
[x,y,w,h]=cv2.boundingRect(轮廓)
#丢弃过大的区域
如果h>300且w>300:
持续
#丢弃过小的区域

如果h使用此代码执行此操作。它检测图像中的文本/数字区域

import cv2

image = cv2.imread("C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours


idx =0
# for each contour found, draw a rectangle around it on original image
for contour in contours:

    idx += 1

    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    #cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

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

    cv2.imwrite('C:\\Users\\Bob\\Desktop\\' + str(idx) + '.jpg', roi)

    cv2.imshow('img',roi)
    cv2.waitKey(0)
导入cv2
image=cv2.imread(“C:\\Users\\Bob\\Desktop\\PyHw\\images\\test5.png”)
gray=cv2.CVT颜色(图像,cv2.COLOR_BGR2GRAY)#灰度
_,thresh=cv2.阈值(灰色,150255,cv2.thresh_BINARY_INV)#阈值
kernel=cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
扩张=cv2.扩张(阈值,核,迭代次数=13)#扩张
_,轮廓,层次=cv2.查找轮廓(扩展,cv2.外部翻新,cv2.链约无)#获取轮廓
idx=0
#对于找到的每个轮廓,在原始图像上围绕其绘制一个矩形
对于等高线中的等高线:
idx+=1
#获取矩形边界轮廓
[x,y,w,h]=cv2.boundingRect(轮廓)
#丢弃过大的区域
如果h>300且w>300:
持续
#丢弃过小的区域

如果hPost your original image.added original images post your original image.added original images我看一下我的答案。这似乎做得很好:嗨,看看我(自己)的答案。它似乎做得很好: