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

9udxz4iz  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(169)

我正在使用连通分量分析来识别图像中的字符。为此,我使用了cv2.connectedComponentsWithStats()函数。作为输出,它获得了字符,但没有顺序。

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

在得到组件的尺寸后,我正在预览它。但是顺序是随机的。因为这是如何得到与原始图像顺序相同的组件。
实际输出指令

期望字符顺序

wvt8vs2t

wvt8vs2t1#

正如@Cris Luengo提到的,它沿着图像行从左到右,然后从上到下运行。所以它首先看到的是高的字符。你需要根据它们的坐标重新排序它们。
例如,在下面的代码中,我将获得一个示例文本“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)

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

正如你所看到的,它被打印为'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("")

现在ordered_elements由按x轴排序的字符组成。

相关问题