【发布时间】:2019-11-04 00:37:29
【问题描述】:
我有一些轮廓图像,我想对其进行分割,这基本上意味着我想将轮廓图像中的所有字符保存到单个图像中。但是我得到了几个噪声图像以及所需的输出。我想知道如何在不影响所需输出的情况下去除所有噪声图像。
我试图更改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)
我想知道如何只获取正确的字符图像,不包括输出文件夹中的噪声图像。我添加了一些我需要分割成单个字符的输入轮廓图像。
【问题讨论】:
-
I have few images of words which i want to segment into individual character images 的可能重复问题除了其他输入图像之外,这个问题与您之前的问题有很大不同吗?
-
@HansHirse 这个程序完全不同....
-
@Ani,你要获取单个字符还是整个单词?例如,
narasimha或每个单独的字母。 -
@nathancy 我想要单个字符,例如 n、a、r 等...
-
@nathancy 您的代码不适用于少数图像,我想要一个动态代码让我们说如果我有任何新图像在这种情况下我不需要更改原始代码.它应该适用于所有字符大小的图像。
标签: python opencv image-processing contour image-segmentation