Я хочу обрезать линии изображения, содержащего определенные цвета.
У меня уже есть следующие строки кода, чтобы получить определенные цвета. Это цвета, содержащиеся в изображении обводки карандашом.
# we get the dominant colors
img = cv2.imread('stroke.png')
height, width, dim = img.shape
# We take only the center of the image
img = img[int(height/4):int(3*height/4), int(width/4):int(3*width/4), :]
height, width, dim = img.shape
img_vec = np.reshape(img, [height * width, dim] )
kmeans = KMeans(n_clusters=3)
kmeans.fit( img_vec )
# count cluster pixels, order clusters by cluster size
unique_l, counts_l = np.unique(kmeans.labels_, return_counts=True)
sort_ix = np.argsort(counts_l)
sort_ix = sort_ix[::-1]
fig = plt.figure()
ax = fig.add_subplot(111)
x_from = 0.05
# colors are cluster_center in kmeans.cluster_centers_[sort_ix] I think
Затем я хочу проанализировать все строки изображений, которые у меня есть, и обрезать вместе линии, которые имеют непрерывный штрих карандашом на полях. То есть линии, где есть хотя бы один пиксель с одним из цветов примера stroke.png
, исключая белый (что я еще не реализовал). И, наконец, извлеките текст из этих строк.
### Attempt to get the colors of the stroke example
# we get the dominant colors
img = cv2.imread('strike.png')
height, width, dim = img.shape
# We take only the center of the image
img = img[int(height/4):int(3*height/4), int(width/4):int(3*width/4), :]
height, width, dim = img.shape
img_vec = np.reshape(img, [height * width, dim] )
kmeans = KMeans(n_clusters=2)
kmeans.fit( img_vec )
# count cluster pixels, order clusters by cluster size
unique_l, counts_l = np.unique(kmeans.labels_, return_counts=True)
sort_ix = np.argsort(counts_l)
sort_ix = sort_ix[::-1]
fig = plt.figure()
ax = fig.add_subplot(111)
x_from = 0.05
cluster_center = kmeans.cluster_centers_[sort_ix][1]
# plt.show()
### End of attempt
for file_name in file_names:
print("we wrote : ",file_name)
# load the image and convert it to grayscale
image = cv2.imread(file_name)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# check to see if we should apply thresholding to preprocess the
# image
if args["preprocess"] == "thresh":
gray = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# make a check to see if median blurring should be done to remove
# noise
elif args["preprocess"] == "blur":
gray = cv2.medianBlur(gray, 3)
# write the grayscale image to disk as a temporary file so we can
# apply OCR to it
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
# Here we should split the images in parts. Those who have strokes
# We asked for a stroke example so we have its color
# While we find pixels with the same color we store its line
im = Image.open(filename)
(width, height)= im.size
for x in range(width):
for y in range(height):
rgb_im = im.convert('RGB')
red, green, blue = rgb_im.getpixel((1, 1))
# We test if the pixel has the same color as the second cluster # We should rather test if it is "alike"
# It means that we found a line were there is some paper stroke
if np.array_equal([red,green,blue],cluster_center):
# if it is the case we store the width as starting point while we find pixels
# and we break the loop to go to another line
if start == -1:
start = x
selecting_area = True
break
# if it already started we break the loop to go to another line
if selecting_area == True:
break
# if no pixel in a line had the same color as the second cluster but selecting already started
# we crop the image and go to another line
# it means that there is no more paper stroke
if selecting_area == True:
text_box = (0, start, width, x)
# Crop Image
area = im.crop(text_box)
area.show()
selecting_area = False
break
# load the image as a PIL/Pillow image, apply OCR, and then delete
# the temporary file
text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
#print(text)
with open('resume.txt', 'a+') as f:
print('***:', text, file=f)
Таким образом, до сих пор, , если я могу получить цвета, которые я хочу использовать для обрезки изображения, тест, который я разработал для меня, чтобы узнать, какая часть изображения должна быть обрезана, кажется, не заканчивается Можете ли вы помочь мне достичь этого?
Приложение
Другая идея, разработанная этой статьи , заключается в группировке штрихов и распознавании текста по отдельности, но я не знаю ни одного группового алгоритма, который бы помог мне сделать эту работу.
Пример изображения для обработки:


Полный проект, аннотированный текстовый обобщитель, можно найти на Github здесь .