简体   繁体   English

如何在openCV中从左到右应用连通分量分析

[英]How to apply Connected Component Analysis left to right order in openCV

i am using connected component analysis to recognize characters from the image.我正在使用连通分量分析来识别图像中的字符。 for that i am using cv2.connectedComponentsWithStats() function.为此,我正在使用 cv2.connectedComponentsWithStats() 函数。 As the output it is getting the characters but without a order.作为输出,它正在获取字符但没有顺序。

num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(img, 8, cv2.CV_32S)

after getting the component dimensions i am previewing it.获得组件尺寸后,我正在预览它。 but the order is randomized.但顺序是随机的。 As it is how to get the components same as in original image order.因为它是如何获得与原始图像顺序相同的组件。

actual output order实际输出顺序

expected character order预期的字符顺序

As @Cris Luengo mentioned, It runs along image rows, left to right, then top to bottom.正如@Cris Luengo 提到的,它沿着图像行运行,从左到右,然后从上到下。 So it sees first the characters that are taller first.所以它首先看到较高的字符。 You need to reorder them based on their coordinates.您需要根据它们的坐标对它们重新排序。

For example, in the below code, I will get a sample text 'hello,' apply it to preprocess and get connected components.例如,在下面的代码中,我将获得一个示例文本“hello”,将其应用于预处理并获取连接的组件。

# import the necessary packages
import cv2
from google.colab.patches import cv2_imshow

img = cv2.imread('img.png')
img_bw=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2_imshow(img_bw)

bw_img

# applies thresh using Otu's method
thresh = cv2.threshold(img_bw, 0, 255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2_imshow(thresh)

脱粒

# 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("")

确定的组件

As you can see, it is printed as 'llheo' since it runs along image rows, left to right, then top to bottom.如您所见,它被打印为“llheo”,因为它沿着图像行从左到右,然后从上到下排列。 So it sees first the characters that are taller first.所以它首先看到较高的字符。 To reorder these identified characters, now it is possible to use the identified_character_components, which has the x-axis and detected character pixels.要重新排序这些识别出的字符,现在可以使用 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("")

有序输出 img

Now ordered_elements consist of the ordered characters by the x-axis.现在 ordered_elements 由按 x 轴排序的字符组成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM