# getting connected components
numlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S)
#with the stats returns cropping the characters from the mask(img which included all detected components)
identified_character_components =[]
for i in range(0,numlabels):
# skipping 0 due to it outputs the background
if i!=0:
# identified dimensions unpacking
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
a = stats[i, cv2.CC_STAT_AREA]
component_mask = (labels == i).astype("uint8") * 255
box_image = component_mask[y:y+h, x:x+w]
identified_character_components.append((x,box_image)) # adding object pixels and x_axis to sort the order in next steps
cv2_imshow(box_image)
print("")
正如你所看到的,它被打印为'l l h e o',因为它沿着图像行从左到右,然后从上到下排列。所以它首先看到的是高的字符。要重新排序这些识别的字符,现在可以使用identified_character_components,它有x轴和检测到的字符像素。
#function to get the first element
def takeFirstElm(ele):
return ele[0]
#function to order the array using the first element(x-axis)
def reorder_first_index(list):
return sorted(list,key=takeFirstElm)
ordered_elements = reorder_first_index(identified_character_components)
#removing the x-axis from the elements
ordered_character_components=[]
for element in ordered_elements:
ordered_character_components.append(element[1])# appending only the image pixels(removing added index in earlier steps)
# printing the ordered images.
for character in ordered_character_components:
cv2_imshow(character)
print("")
1条答案
按热度按时间wvt8vs2t1#
正如@Cris Luengo提到的,它沿着图像行从左到右,然后从上到下运行。所以它首先看到的是高的字符。你需要根据它们的坐标重新排序它们。
例如,在下面的代码中,我将获得一个示例文本“hello”,将其应用于预处理并获得连接的组件。
正如你所看到的,它被打印为'l l h e o',因为它沿着图像行从左到右,然后从上到下排列。所以它首先看到的是高的字符。要重新排序这些识别的字符,现在可以使用identified_character_components,它有x轴和检测到的字符像素。
现在ordered_elements由按x轴排序的字符组成。