ppp

parent 4105670a
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
WORD_DIR = u'/datos/data/vaticano/ya_binarizadas_y_segmentadas/data/ground_truth/word_images'
os.chdir(WORD_DIR)
image_list = os.listdir(WORD_DIR)
maxh = 0
cumw = 0
margin = 50
means=[]
means2=[]
medians=[]
modes=[]
heights=[]
for img in image_list:
img = img.strip()
I = np.asarray(Image.open(img)) == False
height = I.shape[0]
cumw = cumw + I.shape[1] + margin
if height > maxh:
maxh = height
total = np.sum(I)
profile = np.sum(I,axis=1)/total
psum = np.cumsum(profile)
median = np.flatnonzero(psum >= 1/2)[0]
x = np.arange(height)
mean = np.dot(profile,x)
mode = np.argmax(profile)
if mode > height/2:
mode = np.argmax(profile[:int(height/2)])
print('img=',img,'sum=',total,'height=',height,'base=',mode,'mean',int(mean),'median=',median)
heights.append(height)
modes.append(mode)
medians.append(median)
means.append(mean)
profile2 = profile > np.mean(profile)
mean2 = np.dot(profile2/np.sum(profile2),x)
means2.append(mean2)
nimg = len(medians)
print('nimg',nimg,'maxh=',maxh)
minh = 0
center = maxh/2
X = np.zeros((2*maxh,nimg))
Y = np.zeros((2*maxh,cumw))
center = maxh
i = 0
offset = 0
for img in image_list:
I = np.asarray(Image.open(img)) == False
shift1 = center - int(means[i])
shift2 = center - int(modes[i])
shift3 = center - int(medians[i])
shift4 = center - int(means2[i])
shift = int(np.median((shift1,shift2,shift3,shift4)))
# print(shift1,shift2,shift3,shift4,shift)
total = np.sum(I)
X[shift:(shift+heights[i]),i] = np.sum(I,axis=1)/total
Y[shift:(shift+I.shape[0]),offset:(offset+I.shape[1])] = I
offset = offset + I.shape[1] + margin
i = i + 1
#plt.figure(1,figsize=(20,10))
#plt.imshow(X)
#plt.colorbar()
#plt.figure(2,figsize=(10,10))
#plt.plot(X)
#plt.grid()
profile = np.sum(Y,axis=1)
nz = np.flatnonzero(profile)
bot = nz[-1]
top = nz[0]
Y = Y[top:bot,:]
#plt.savefig('Y.png')
collage = Image.frombytes(mode='L',size=(Y.shape[1],Y.shape[0]),data=255*Y.astype(np.uint8))
collage.save('../collage.pbm',compression='tiff_ccitt')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment