Commit babc6339 authored by Ignacio Francisco Ramirez Paulino's avatar Ignacio Francisco Ramirez Paulino

sdfpoiu

Merge branch 'master' of https://gitlab.fing.edu.uy/nacho/bmf
parents 7e0aea27 82e20275
......@@ -192,13 +192,16 @@ idx_t learn_model_traditional(binary_matrix& X,
E.copy_to(Eprev);
D.copy_to(Dprev);
A.copy_to(Aprev);
// cold restart!
A.clear();
X.copy_to(E);
coefficients_update(E,H,D,A,ma,me);
update_dictionary(E,H,D,A);
hdE = dist(E,Eprev);
hdD = dist(D,Dprev);
hdA = dist(A,Aprev);
reinitialize_unused(E,H,D,A);
} while (hdE > 0);
} while (hdE > 1e-3*E.get_len());
Eprev.destroy();
Aprev.destroy();
Dprev.destroy();
......
......@@ -21,6 +21,7 @@ const char* oname = "res/einstein_patches.pbm";
void parse_args(int argc, char **argv) {
if (argc < 4) {
std::cerr << "USAGE: " << argv[0] << " image.pbm patch_width stride patches.pbm" << std::endl;
std::cerr << "USAGE: " << argv[0] << " image.pbm wxxwy+sx+sy patches.pbm (imagemagick-like syntax, for non-square patches/grids" << std::endl;
exit(1);
}
iname = argv[1];
......
#!/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'
WORD_DIR = u'vatican/word_images/'
EXT='pbm'
#os.chdir(WORD_DIR)
image_list = os.listdir(WORD_DIR)
maxh = 0
cumw = 0
margin = 25
means=[]
means2=[]
medians=[]
modes=[]
heights=[]
for img in image_list:
img = img.strip()
I = np.asarray(Image.open(WORD_DIR + 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(WORD_DIR + 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='1',size=(Y.shape[1],Y.shape[0]),data=np.packbits(Y.astype(np.bool)))
collage = Image.frombytes(mode='L',size=(Y.shape[1],Y.shape[0]),data=255*Y.astype(np.uint8))
collage.save('./collage.pgm')
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