如何将图像中的字符和单词分割成轮廓
How to segment characters and words from images into contours
我有一些轮廓图像,我想对其进行分割,这基本上意味着我想将轮廓图像中的所有字符保存到单个图像中。但是我得到了几张噪声图像以及所需的输出。我想知道如何在不影响所需输出的情况下去除所有噪声图像。
我试图改变 w
和 h
的值,这样我就可以最小化噪声并只获取字符作为分段图像。
def imageSegmentation(fldr):
for file in fldr:
for f in os.listdir(file):
im = cv2.imread(file+f)
#print(f)
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
con_img=cv2.drawContours(im, contours, -1, (0,0,0), 1)
#cv2.imshow("Contour_Image",con_img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
newfolder=file+"\contour\"+f+"\"
os.makedirs(newfolder, exist_ok=True)
fname=os.path.splitext(f)[0]
cv2.imwrite((newfolder+fname+".png"),con_img)
#cv2.imshow("con_img",con_img)
#cv2.waitKey()
#cv2.destroyAllWindows()
newfolder2=file+"\seg\"+fname+"\"
os.makedirs(newfolder2,exist_ok=True)
sorted_ctrs = sorted(contours, key=lambda cntr: cv2.boundingRect(cntr)[0])
for i, cntr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(cntr)
# Getting ROI
roi = im[y:y + h, x:x + w]
#roi=~roi
if w > 9 and h > 27:
cv2.imwrite(newfolder2+"{}.png".format(i), roi)
我想知道如何只获取正确的字符图像,排除输出文件夹中的噪声图像。我已经添加了一些我需要分割成单个字符的输入轮廓图像。
如果您想提取单个字符或整个单词,由于您的问题并不完全清楚,这里是同时执行这两种操作的方法。
单个字符
这里的主要思想是
- 将图像转换为灰度和高斯模糊
- 执行canny边缘检测
- 寻找轮廓
- 遍历轮廓并使用最小面积进行过滤
- 获取边界框并提取ROI
Canny 边缘检测使用 cv2.Canny()
现在我们使用 cv2.findContours()
遍历轮廓并使用 cv2.contourArea()
进行过滤,然后绘制边界框
这是您的一些其他输入图像的结果
import cv2
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 100
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('blur', blur)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)
整个单词
现在如果你想提取整个单词,你必须稍微修改一下策略
- 将图像转换为灰度和高斯模糊
- 执行canny边缘检测
- 扩张以获得单个轮廓
- 寻找轮廓
- 遍历轮廓并使用最小面积进行过滤
- 获取边界框并提取ROI
Canny 边缘检测
使用 cv2.dilate()
连接等高线进行扩张
使用轮廓区域查找边界框和过滤
提取的投资回报率
注意:如果您要查找整个单词,您可能需要更改最小面积值,因为它取决于您正在分析的图像。
import cv2
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(canny, kernel, iterations=5)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 5000
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('blur', blur)
cv2.imshow('dilate', dilate)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)
我有一些轮廓图像,我想对其进行分割,这基本上意味着我想将轮廓图像中的所有字符保存到单个图像中。但是我得到了几张噪声图像以及所需的输出。我想知道如何在不影响所需输出的情况下去除所有噪声图像。
我试图改变 w
和 h
的值,这样我就可以最小化噪声并只获取字符作为分段图像。
def imageSegmentation(fldr):
for file in fldr:
for f in os.listdir(file):
im = cv2.imread(file+f)
#print(f)
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
con_img=cv2.drawContours(im, contours, -1, (0,0,0), 1)
#cv2.imshow("Contour_Image",con_img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
newfolder=file+"\contour\"+f+"\"
os.makedirs(newfolder, exist_ok=True)
fname=os.path.splitext(f)[0]
cv2.imwrite((newfolder+fname+".png"),con_img)
#cv2.imshow("con_img",con_img)
#cv2.waitKey()
#cv2.destroyAllWindows()
newfolder2=file+"\seg\"+fname+"\"
os.makedirs(newfolder2,exist_ok=True)
sorted_ctrs = sorted(contours, key=lambda cntr: cv2.boundingRect(cntr)[0])
for i, cntr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(cntr)
# Getting ROI
roi = im[y:y + h, x:x + w]
#roi=~roi
if w > 9 and h > 27:
cv2.imwrite(newfolder2+"{}.png".format(i), roi)
我想知道如何只获取正确的字符图像,排除输出文件夹中的噪声图像。我已经添加了一些我需要分割成单个字符的输入轮廓图像。
如果您想提取单个字符或整个单词,由于您的问题并不完全清楚,这里是同时执行这两种操作的方法。
单个字符
这里的主要思想是
- 将图像转换为灰度和高斯模糊
- 执行canny边缘检测
- 寻找轮廓
- 遍历轮廓并使用最小面积进行过滤
- 获取边界框并提取ROI
Canny 边缘检测使用 cv2.Canny()
现在我们使用 cv2.findContours()
遍历轮廓并使用 cv2.contourArea()
进行过滤,然后绘制边界框
这是您的一些其他输入图像的结果
import cv2
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 100
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('blur', blur)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)
整个单词
现在如果你想提取整个单词,你必须稍微修改一下策略
- 将图像转换为灰度和高斯模糊
- 执行canny边缘检测
- 扩张以获得单个轮廓
- 寻找轮廓
- 遍历轮廓并使用最小面积进行过滤
- 获取边界框并提取ROI
Canny 边缘检测
使用 cv2.dilate()
连接等高线进行扩张
使用轮廓区域查找边界框和过滤
提取的投资回报率
注意:如果您要查找整个单词,您可能需要更改最小面积值,因为它取决于您正在分析的图像。
import cv2
image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
canny = cv2.Canny(blur, 120, 255, 1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
dilate = cv2.dilate(canny, kernel, iterations=5)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 5000
image_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('blur', blur)
cv2.imshow('dilate', dilate)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)