gpt4 book ai didi

python - 如何在 Python 中使用 OpenCV 概括轮廓手写字符?

转载 作者:行者123 更新时间:2023-12-04 13:07:23 26 4
gpt4 key购买 nike

我尝试从图像中检测和裁剪手写字符。一些字符可以被识别并包含在一个矩形中,但对于其他字符,相同的参数不起作用。我怎样才能泛化它?

Raw Image

import cv2
import numpy as np
import matplotlib.pyplot as plt

im = cv2.imread('mission.png',0)
img_blured = cv2.GaussianBlur(im,(5,5),7)
closing = cv2.morphologyEx(img_blured, cv2.MORPH_CLOSE, (31,31))
thresh = 195
ret, bw_img = cv2.threshold(closing, thresh, 255, cv2.THRESH_BINARY)


_,contours, hierarchy = cv2.findContours(bw_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),3)
i=0
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)

if w>150 and h>150:

cv2.imwrite(str(i)+".jpg",bw_img[y:y+h,x:x+w])
i=i+1
plt.imshow(im)
plt.show()
cv2.imwrite("output.png",im)

Processed Image

最佳答案

也许这可以帮到你:

# Import preprocessors
import os
import cv2
import numpy as np

# Read image
dir = os.path.abspath(os.path.dirname(__file__))
im = cv2.imread(dir+'/nvCXT.png')

# Add padding around the original image
pad = 5
h, w = im.shape[:2]
im2 = ~(np.ones((h+pad*2, w+pad*2, 3), dtype=np.uint8))
im2[pad:pad+h, pad:pad+w] = im[:]
im = im2

# Blur it to remove noise
im = cv2.GaussianBlur(im, (5, 5), 5)

# Gray and B/W version
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
bw = cv2.threshold(im, 200, 255, cv2.THRESH_BINARY)[1]

# Find contours and sort them by position
cnts, _ = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnts.sort(key=lambda x:cv2.boundingRect(x)[0])

# Find and save blocks
s1, s2 = w/2, w/10
i = 0
x2, y2, w2, h2 = 0, 0, 0, 0
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
if (w+h < s1 and w+h > s2) and (i==0 or (x2+w2) < x):
i += 1
cv2.imwrite(dir+'/_'+str(i)+".jpg", im[y:y+h, x:x+w])
cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 2)
x2, y2, w2, h2 = x, y, w, h

# Save the processed images
cv2.imwrite(dir+'/out.png', im)
cv2.imwrite(dir+'/out_bw.png', bw)

enter image description here

enter image description here

enter image description here

enter image description here

关于python - 如何在 Python 中使用 OpenCV 概括轮廓手写字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68762740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com