-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_process.py
1512 lines (1044 loc) · 50.1 KB
/
image_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 2 15:46:36 2015
@author: tomish
"""
import os
import os.path
import cv2
import numpy as np
import numpy.ma as ma # For masking
import scipy as sp
import scipy.spatial.distance
from scipy.ndimage.filters import gaussian_filter1d
from scipy import interpolate
from scipy.stats import norm
from sklearn.decomposition import PCA
from sklearn.neighbors import NearestNeighbors
import skimage.morphology as morp
from skimage.filters import rank
from PIL import Image
from mahotas.features import zernike_moments
import pyshtools as shtools # Need to install this!!
from image_set import ImgSet
import fourier_descriptors as fd
import spherical_harmonics as sh
import zernike
# Added for Dario stuff NOT SURE I LIKE THIS! :(
#from hsc_wrapper import HSC_wrapper
#import hsc_wrapper as hsc #uncomment this when needed but really should refactor (ALOT!)
class ImgProcess:
NONE = 1
FOURIER_DESCRIPTOR = 2
ZERNIKE_MOMENTS = 3
HU_MOMENTS = 4
PRINCIPLE_COMPONENT_ANALYSIS = 5
IMAGE_CHANGE = 5
IMAGE_CHANGED = 7 #Boolean
RESIZE = 8
cache_results = True # For now just always cache results
#refactor by doing dict lookup rather than enum
processes = {}
processes[2] = 'fd'
processes[3] = 'zernike'
processes[4] = 'loghu'
processes[5] = 'pca'
processes[6] = 'im_change'
processes[7] = 'im_changed'
processes[8] = 'resize'
@classmethod
def process(cls, img_set, process_type):
# Just return if no processing needed
if process_type == cls.NONE:
return img_set
# See if we already did this process before
processed_name = cls.get_processed_name(img_set, process_type)
cache_file = cls.get_cache_file(img_set.data_folder, processed_name)
loaded_set = ImgSet.load_from_cache(cache_file)
if loaded_set is not None:
print "Loading cache file: " + cache_file
return loaded_set
else: # If not previously... calculate
(M, h, w) = cls.process_img_set(img_set, process_type)
# If we resized also include the new size
if cls.processes[process_type] == 'resize':
name = '%s_%s%s' % (img_set.name, cls.processes[process_type], w)
else:
name = '%s_%s' % (img_set.name, cls.processes[process_type])
processed_set = ImgSet(name=name, M=M, w=w, h=h)
# Cache results
if cls.cache_results:
processed_set.save()
return processed_set
@classmethod
def process_img_set(cls, img_set, process_type):
#if process_type == 1:
# print "Unprocessed image set"
# return img_set.M
if process_type == cls.FOURIER_DESCRIPTOR:
print "Calculating Fourier descriptors"
return cls.get_fds(img_set)
elif process_type == cls.ZERNIKE_MOMENTS:
return cls.get_zms(img_set)
elif process_type == cls.HU_MOMENTS:
print "Calculating Hu Moments"
return (cls.get_moments(img_set, 2), 7, 1)
elif process_type == cls.PRINCIPLE_COMPONENT_ANALYSIS:
return cls.do_pca(img_set.M.T)
elif process_type == cls.IMAGE_CHANGE:
return cls.pixel_changes(img_set.M.T)
elif process_type == cls.IMAGE_CHANGED:
return cls.pixel_changed(img_set.M)
elif process_type == cls.RESIZE:
return cls.resize(img_set)
#TODO: Use cumsum(range)?
@classmethod
def triangular_num(cls, n):
"""Helper function: 1+2+3+4....+n"""
return sum([i for i in range(n+1)])
@classmethod
def get_processed_name(cls, img_set, process):
return '%s_%s' % (img_set.name, cls.processes[process])
@classmethod
def get_cache_file(cls, data_folder, processed_name):
return '%s/%s.npz' % (data_folder, processed_name)
#@classmethod
#def file_exists(cls, cache_file):
# if os.path.isfile(cache_file):
# return cache_file
# else:
# return None
@classmethod
def resize(cls, img_set, w=30, interp='nearest'):
"""Resize images"""
M_resized = np.zeros([w**2, len(img_set)])
for i, img in enumerate(img_set):
img_resized = sp.misc.imresize(img, (w,w), interp)
M_resized[:,i] = np.reshape(img_resized, -1)
return (M_resized, w, w)
@classmethod
def find_contours(cls, I):
""" Finds the contours of largest object"""
contours = []
_, contours, hierarchy = cv2.findContours(
I.copy(),
cv2.RETR_LIST, # I think this is the fastest
cv2.CHAIN_APPROX_NONE,
contours)
# Return the contour of the largest object
contour_sizes = [c.size for c in contours]
contour = contours[contour_sizes.index(max(contour_sizes))]
return contour
@classmethod
def get_moments(cls, img_set, method=1):
""" Gets Hu Moments for a set of images"""
num_moments = 7
moments = np.empty([num_moments, len(img_set)])
for i, img in enumerate(img_set):
moments[:,i] = cls.calc_moment(img)
# Methods copied from opencv.modules.imgproc.src.matchcontours.cpp
if method == 1:
return 1./(np.sign(moments) * np.log10(np.abs(moments)))
else:
return np.sign(moments) * np.log10(np.abs(moments))
@classmethod
def get_fds(cls, img_set, num_descriptors=100):
"""gets FD for a set of images. Returns the magnitude"""
fd = np.empty([num_descriptors, len(img_set)], dtype=complex)
for i, img in enumerate(img_set):
fd[:,i] = cls.calc_fd(img, num_descriptors)
return (np.absolute(fd), num_descriptors, 1)
@classmethod
def get_zms(cls, img_set, polynomial=10):
radius = img_set.w / 2.0 # Can this be bigger than the image?
cm = (img_set.w/2.0-0.5, img_set.h/2.0-0.5) # Centre of mass
num_moments = cls.triangular_num((polynomial+2) / 2) + cls.triangular_num((polynomial+1) / 2) # times something?
zms = np.empty([num_moments, len(img_set)], dtype=complex)
for i, img in enumerate(img_set):
zms[:,i] = zernike_moments(img, radius, polynomial, cm=cm)
return (zms, num_moments, 1)
@classmethod
def match_shapes(cls, hu1, hu2):
#hu1_mod = 1 ./ (np.sign(hu1) * np.log10(np.abs(hu1))
#hu2_mod = 1 ./ (np.sign(hu1) * np.log10(np.abs(hu1))
#return np.abs(hu2_mod - hu1_mod)
pass
@classmethod
def biggest_area(cls, I):
X = np.zeros(I.shape, dtype='uint8')
contours = cls.find_contours(I)
return cv2.drawContours(X, [contours], -1, 255, -1 )
@classmethod
def calc_fd(cls, I, num_descriptors=100):
"""Calculate Fourier descriptors of a binary image"""
contours = cls.find_contours(I)
descriptors = fd.find_descriptors(contours)
return descriptors[0:num_descriptors]
@classmethod
def calc_moment(cls, I):
"""Calculate the Hu moments of a binary image"""
# Get different answers depending on input to moments
contours = cls.find_contours(I)
moments = cv2.HuMoments(cv2.moments(contours)).flatten()
#moments = cv2.HuMoments(cv2.moments(I)).flatten()
return moments
@staticmethod
def do_pca(X, n_components=1000):
pca = PCA()
return pca.transform(X)
@staticmethod
def pixel_changes(X):
#return np.logical_xor(X[1:], X[:-1])
return (X[1:].astype('i2') - X[:-1].astype('i2')).T
@staticmethod
def pixel_changed(X):
return np.logical_xor(X[1:], X[:-1])
"""ABOVE HERE IS LEGACY STUFF"""
def sad(I1, I2):
"""Returns the summed absolute difference between two grayscale images."""
# Need to use signed ints to get difference to work
return np.sum(np.abs(I1 - I2))
def wraparound_sad(I1, I2, x_res=None):
"""Returns SAD between two grayscale images, at various rotations."""
w = I1.shape[1]
if x_res is None:
x_res = w
rotations = np.rint(np.linspace(0, w, num=x_res, endpoint=False)).astype('uint32')
return np.array([sad(I1, np.roll(I2, r, axis=1)) for r in rotations])
def calc_distance_sad_slow(S1, S2, x_res=None):
"""Compare two image sets. Rotate each image to get best match"""
D = np.empty([len(S1), len(S2)])
#Need to convert to int16 to get the negative stuff working
for i, I1 in enumerate(S1):
for j, I2 in enumerate(S2):
D[i,j] = np.min(wraparound_sad(I1.astype('int16'), I2.astype('int16'), x_res=x_res))
return D
def calc_distance_sad(S1, S2, x_res=1, metric='cityblock'):
"""Not that fast, but helluva lot faster than the other one"""
D = np.empty([len(S1), len(S2)])
h = S1.h
w = S1.w
x = np.arange(0, h*w, h*x_res)
y = np.arange(h*w)
xv, yv = np.meshgrid(x,y)
idxs = np.mod(xv + yv, h*w)
for i in range(len(S1)):
D[i,:] = np.min(sp.spatial.distance.cdist(S1.M[:,i][idxs].T, S2.M.T, metric=metric), axis=0)
return D
def local_contrast_enhance(I):
"""Local histogram equalization"""
amp_factor = (2**16) / np.max(I)
# Suggested kernel by milford (centre surround type thing)
#kernel = np.ones([50, 50], dtype='uint8')
#kernel[12:18, 12:18] = 0
kernel = np.ones([1, 30], dtype='uint8')
return rank.equalize((I * amp_factor).astype('u2'), selem=kernel)
def rotate_image(I, theta):
""" Rotates an image by theta degrees, while maintaining same scale and size."""
rows, cols = I.shape
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), theta, 1)
return cv2.warpAffine(I, M, (cols, rows))
def resize_image(I, w=30, interp='nearest'):
"""Resize image"""
return sp.misc.imresize(img, (w,w), interp)
def binarize(I):
"""Converts an image to B&W using Otsu's method"""
# If colour, then convert to grayscale
if len(I.shape) > 2:
I = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
# Find threshold
threshold, BW = cv2.threshold(I,
0,
255,
cv2.THRESH_OTSU)
return BW
def binarize_na(I):
"""Dario's method to binarize"""
threshold, BW = cv2.threshold(I,
0,
255,
cv2.THRESH_OTSU)
# fit sky to a gaussian and chop after 2 standard deviations
#print BW
#print np.max(BW)
#print np.min(BW)
#print BW.shape
#print I.shape
#print I[BW.reshape(I.shape)==255].shape
mu, std = norm.fit(I[BW.reshape(I.shape)==255])
t = mu - 2*std
return (I>t).astype('int') * 255
def binarize_ws(I, perc=0, max_hist_res=1000):
"""Binarise an image using the watershed method described in (Stone 2014)"""
upper_p = np.percentile(I, 100-perc)
lower_p = np.percentile(I, perc)
values = np.unique(I)
values_truncated = values[np.logical_and(values > lower_p, values < upper_p)]
values_range = np.max(values_truncated) - np.min(values_truncated)
if (values_range > max_hist_res):
hist_res = max_hist_res
else:
hist_res = values_range + 1
lower = values_truncated[0].astype('float')
upper = values_truncated[-1].astype('float')
h, null = np.histogram(I, bins=hist_res, range=(lower,upper+1))
h_gf = gaussian_filter1d(h, sigma=hist_res/20.0)
hist_values = np.unique(h_gf)
i = 0
l_idxs = np.array([-1,-1,-1]) # Left hand slope
while i < hist_values.size and l_idxs.size is not 2:
a = h_gf > hist_values[i]
diffs = np.diff(np.hstack([0, a]))
l_idxs = np.where(diffs == 1)[0] # For some reason where returns a tuple
i += 1
if l_idxs.size < 2:
t = np.max(values)
else:
r_idxs = np.where(diffs == -1)[0] # Find right slope
t = lower + (l_idxs[1] + r_idxs[0]) / 2.0 #assumes 8 bit GS image
BW = (I>t).astype('int') * 255
return BW
def mask_image(I, mask):
return ma.masked_array(I, mask=mask)
def circular_mask(width, height, radius, centre=None):
"""Creates a black circular mask on white background.
Args:
width (int): The mask width.
height (int): The mask height.
radius (int): The radius of the cirle
centre (int, optional): The centre of the circle.
This might be useful because the parabolic mirror is
offset from actual image centre.
"""
if not centre:
centre = (width / 2, height / 2)
mask = np.ones((height, width), np.uint8) * 255
cv2.circle(mask, centre, radius, 0, -1)
return mask
def donut_mask(width, height, r_inner, r_outer, centre=None):
""" Creates a black donut shaped mask.
For use when there is a blind spot in centre of panoramic mirror.
"""
if not centre:
centre = width / 2, height / 2
mask = circular_mask(width, height, r_outer, centre)
cv2.circle(mask, centre, r_inner, 255, -1)
return mask
def crop_to_square(I):
""" Currently assumes a grayscale landscape image!!!"""
# Todo: include RGB images (3 dimensions)
h = I.shape[0]
w = I.shape[1]
pixels_to_crop = (w - h) / 2
return I[:, pixels_to_crop:w - pixels_to_crop]
# Refactor all these. they are redundant!
def segment(I, mask=None):
"""Binarises a masked or unmasked image"""
if mask is not None:
I_masked = mask_image(I, mask)
I_seg = np.zeros(I.shape, dtype=np.uint8)
I_seg[mask==0] = binarize(I_masked.compressed()).flatten()
else:
I_seg = binarize(I)
return I_seg
def segment_na(I, mask=None):
"""Binarises a masked or unmasked image"""
if mask is not None:
I_masked = mask_image(I, mask)
I_seg = np.zeros(I.shape, dtype=np.uint8)
I_seg[mask==0] = binarize_na(I_masked.compressed()).flatten()
else:
I_seg = binarize_na(I)
return I_seg
def segment_ws(I, mask=None):
"""Binarises a masked or unmasked image"""
if mask is not None:
I_masked = mask_image(I, mask)
I_seg = np.zeros(I.shape, dtype=np.uint8)
I_seg[mask==0] = binarize_ws(I_masked.compressed()).flatten()
else:
I_seg = binarize_ws(I)
return I_seg
def find_contours(I):
""" Finds the contours of largest object"""
contours_list = []
# CAUTION This is a temporary fix, as syntax is different for OpenCV 2 and 3
# After OpenCV is out of beta we can insist on using cv2 3+
if int(cv2.__version__[0]) > 2:
_, contours_list, hierarchy = cv2.findContours(I.copy(),
cv2.RETR_LIST, # I think this is the fastest
cv2.CHAIN_APPROX_NONE,
contours_list)
else:
contours_list, hierarchy = cv2.findContours(I.copy(),
cv2.RETR_LIST, # I think this is the fastest
cv2.CHAIN_APPROX_NONE,
contours_list)
return contours_list
def find_largest_contours(I):
""" Finds the contours of largest object"""
contours_list = find_contours(I)
contour_sizes = [c.size for c in contours_list]
contours_list = contours_list[contour_sizes.index(max(contour_sizes))]
contours = contours_list[:, 0, :] # Reformat by removing third dimension
return contours
def fill_contours(im_size, contours):
"""Fill in contours to make a binary mask"""
blank = np.zeros(im_size, dtype='uint8')
C = cv2.drawContours(blank, [contours], -1, 255, -1) # Contour width of 1 width to fill.
return C
def biggest_area(I):
"""Returns the biggest filled shape in a B&W image."""
contours = find_largest_contours(I)
#X = np.zeros(I.shape, dtype='uint8')
#return cv2.drawContours(X, [contours], -1, 255, -1 )
# This is changed to work in opencv 2. Check it still works for opencv 3!!!!
X = np.zeros(I.shape, dtype='uint8')
cv2.drawContours(X, [contours], -1, 255, -1 )
return X
def find_moment(I):
"""Calculate the Hu moments of a binary image"""
# Get different answers depending on input to moments
contours = find_contours(I)
#TODO: check numpy ipython notebook to see what flatten does
moments = cv2.HuMoments(cv2.moments(contours)).flatten()
#moments = cv2.HuMoments(cv2.moments(I)).flatten()
return moments
def calc_distance(set1, set2, metric='euclidean', cache_results=True):
"""Calculates pairwise distance between all images of two ImgSets."""
cache_file = '%s/%s_%s_%s.npz' % (set1.data_folder,
set1.name,
set2.name,
metric)
if os.path.isfile(cache_file):
print "Loading cache file: %s" % cache_file
data = np.load(cache_file)
Z = data['Z']
else:
Z = sp.spatial.distance.cdist(set1.M.T, set2.M.T, metric)
if cache_results:
np.savez_compressed(cache_file, Z=Z)
return Z
def rgb_to_gray_enhance_blue(I):
"""Convert an image to grayscale using formula from Shabayek 2012"""
return
class ImgProc(object):
def __init__(self, img_set, cache_results=True):
self.img_set = img_set
self.cache_results = cache_results
cache_file = self.get_cache_file_name()
loaded_set = ImgSet.load_from_cache(cache_file)
if loaded_set is not None:
print "Loading cache file:", cache_file
self.processed = loaded_set
else:
(M, h, w) = self.process_img_set()
processed_set = ImgSet(name=str(self), M=M, w=w, h=h, data_folder=img_set.data_folder)
if self.cache_results:
print "Saving to :", cache_file
processed_set.save()
self.processed = processed_set
def process_img_set(self):
print "Just returning the original set"
return (self.img_set.M, self.img_set.h, self.img_set.w)
def get_cache_file_name(self):
data_folder = self.img_set.data_folder
return os.path.join(data_folder, str(self) + '.npz')
def __str__(self):
return '%s_%s' % (self.img_set.name, self.name)
class SkySegProc(ImgProc):
"""Segment an image into sky and ground.
Currently just using Otsu's method.
TODO: add some other thresholding methods to boost performance.
Attributes:
name (str): The tag that will be used to save output.
mask (bool): To mask out areas of the image that shouldn't be
used for calculation of the historgram for segmentation
single_contour (bool): Sometimes we only want a single contour outline,
so we can calculate things like Fourier Descriptors.
"""
def __init__(self, img_set, cache_results=True, mask=None, inner_mask=None, single_contour=False, method='otsu', threshold=None):
#Todo: some kind of check to see if the mask if the right size (setter)
self.name = 'skyseg'
self.mask = mask
self.inner_mask = inner_mask
self.method = method
self.threshold = threshold
self.single_contour = single_contour
if threshold is not None:
self.name += 'th'
self.name += str(threshold)
self.segmenter = self.segment_by_threshold
else:
if method == 'otsu':
self.name += 'otsu'
self.segmenter = segment
elif method == 'na':
self.name += 'na'
self.segmenter = segment_na
else:
self.name += 'ws'
self.segmenter = segment_ws
if single_contour:
self.name += 'sc'
super(SkySegProc, self).__init__(img_set, cache_results)
# TODO: Tidy this up. added as last minute fix to set manual thresholds.
def segment_by_threshold(self, I, mask):
"""Binarises a masked or unmasked image"""
if mask is not None:
I_masked = mask_image(I, mask)
I_seg = np.zeros(I.shape, dtype=np.uint8)
I_seg[mask==0] = (I_masked.compressed() > self.threshold).astype('uint8').flatten()
else:
I_seg = (I > self.threshold).astype('uint8')
return I_seg * 255
def process_img_set(self):
"""Performs sky segmentation."""
print "Extracting skyline"
M = np.empty([self.img_set.h * self.img_set.w, len(self.img_set)], dtype='uint8')
for i, img in enumerate(self.img_set):
I_seg = self.segmenter(img, self.mask)
if self.inner_mask is not None:
I_seg[self.inner_mask==255] = 255
if self.single_contour:
I_seg = biggest_area(I_seg)
M[:,i] = I_seg.flatten()
return (M, self.img_set.h, self.img_set.w)
class CropProc(ImgProc):
"""Crop an image to square.
TODO: add some other thresholding methods to boost performance.
Attributes:
name (str): The tag that will be used to save output.
"""
def __init__(self, img_set, cache_results=True):
self.name = 'crop'
super(CropProc, self).__init__(img_set, cache_results)
def process_img_set(self):
"""Crops all images to square."""
print "Cropping to square"
dtype = self.img_set.M.dtype
M = np.empty([self.img_set.h * self.img_set.h, len(self.img_set)], dtype=dtype)
for i, img in enumerate(self.img_set):
I_cropped = crop_to_square(img)
M[:,i] = I_cropped.flatten()
return (M, self.img_set.h, self.img_set.h)
class ResizeProc(ImgProc):
"""Resize image by percentage.
Attributes:
name (str): The tag that will be used to save output.
"""
def __init__(self, img_set, fraction=1.0, cache_results=True):
self.name = 'resize' + str(fraction)
self.fraction = fraction
super(ResizeProc, self).__init__(img_set, cache_results)
def process_img_set(self):
"""Resize all images to square."""
print "Resizing to", self.fraction, "of size"
dtype = self.img_set.M.dtype
M = np.empty([self.fraction * self.img_set.h * self.fraction * self.img_set.w,
len(self.img_set)], dtype=dtype)
for i, img in enumerate(self.img_set):
I_resized = sp.misc.imresize(img, self.fraction)
M[:,i] = I_resized.flatten()
return (M, self.fraction*self.img_set.h, self.fraction*self.img_set.w)
class HistProc(ImgProc):
"""Give a intensity histogram.
Attributes:
name (str): The tag that will be used to save output.
"""
def __init__(self, img_set, cache_results=True):
self.name = 'hist'
super(HistProc, self).__init__(img_set, cache_results)
def process_img_set(self):
"""Crops all images to square."""
print "Cropping to square"
num_bins = 256
M = np.empty([num_bins, len(self.img_set)])
for i, img in enumerate(self.img_set):
hist, bins = np.histogram(img.ravel(), num_bins, [0, num_bins])
M[:,i] = hist
return (M, num_bins, 1)
class SHProc(ImgProc):
"""Process an image set to Spherical Harmonics.
This process assumed a B&W sky thresholded image.
Attributes:
name (str): The tag that will be used to save output.
n_max (int): The number of Coefficients to use.
"""
def __init__(self, img_set, cache_results=True, n_max=100):
self.n_max = n_max
self.name = "sh%s" % n_max
# First create theta and phi matching current image projection
w_img = img_set.w
(theta_masked, phi_masked, self.mask) = sh.generate_spherical_coords(w_img)
# Now create theta and phi for desired projection
self.w_r = w_img * 2 # The width of the reconstruction
phi_grid, theta_grid = np.mgrid[0:np.pi:self.w_r * 1j, 0:2 * np.pi:self.w_r * 1j]
# Use nearest neighbours to remap
self.idx_mapping = sh.create_mapping(theta_masked, phi_masked, theta_grid, phi_grid)
super(SHProc, self).__init__(img_set, cache_results)
def process_img_set(self):
"""gets Spherical Harmonics for a set of images. Returns the magnitude"""
print "Extracting Spherical Harmonics"
z = np.empty([self.n_max, len(self.img_set)], dtype=float)
inverted_mask = np.invert(self.mask)
for i, img in enumerate(self.img_set):
img_vector = img[inverted_mask].reshape(-1)
values = img_vector[self.idx_mapping]
img_grid = values.reshape(self.w_r, self.w_r)
rcoeffs = shtools.SHExpandDH(img_grid, lmax_calc=self.n_max)
z[:,i] = np.linalg.norm(np.linalg.norm(rcoeffs, axis=0), axis=1)[0:self.n_max]
#Attempt with power spectrum
#z[:,i] = shtools.SHPowerSpectrum(rcoeffs)[0:self.n_max]
#z[:,i] = shtools.SHPowerSpectrumDensity(rcoeffs)[0:self.n_max]
return (z, self.n_max, 1)
class DarioDownSample(ImgProc):
def __init__(self, img_set, X, Y, cache_results=True, contin=3,
n_points=2000, n_bands=12, silent=True):
self.n_bands = n_bands
self.name = "dariods%s" % n_bands
rot_par = 1 # 0:XYZ, 1:ZYZ, 2:ZYZ_FAST, 3:XYZ_FAST 4:XYZ_FREE
resolution_deg = 6.0 # Steps for compass
tilt_max_deg = 30.0 # Maximum tilt to check
resolution = np.deg2rad(resolution_deg)
tilt_max = np.deg2rad(tilt_max_deg)
rot_dict = hsc.create_rot_par(rot_par, resolution, tilt_max)
self.hsc_wrapped = hsc.HSC_wrapper(contin,
rot_dict,
n_points,
n_bands,
0,
silent)
(self.num_angles, theta_sample, phi_sample) = self.hsc_wrapped.get_angles()
# Bring in the matlab data here for remapping nicely
indices = np.vstack([np.floor(np.rad2deg(theta_sample)).astype('uint'),
np.floor(np.rad2deg(phi_sample)).astype('uint')]).T
self.xs = X[indices[:,0], indices[:,1]]
self.ys = Y[indices[:,0], indices[:,1]]
super(DarioDownSample, self).__init__(img_set, cache_results)
def panorama_to_surf(self, I):
surf_img_np = I[np.floor(self.ys).astype('uint'),
np.floor(self.xs).astype('uint')]
surf_img_np = surf_img_np / 255.0 - 0.5
return surf_img_np.tolist()
def process_img_set(self):
z = np.empty([self.num_angles, len(self.img_set)], dtype=float)
for i, img in enumerate(self.img_set):
surf_img = self.panorama_to_surf(img)
z[:,i] = np.array(surf_img)
return (z, self.num_angles, 1)
class SHProcDarioAS(ImgProc):
""" Dario's implementation of Spherical Harmonics. Works with panoramic images. Need to refactor and add disk shaped ones too"""
def __init__(self, img_set, X, Y, cache_results=True, contin=3,
n_points=2000, n_bands=12, silent=True):
self.n_bands = n_bands
self.name = "shdas%s" % n_bands
rot_par = 1 # 0:XYZ, 1:ZYZ, 2:ZYZ_FAST, 3:XYZ_FAST 4:XYZ_FREE
resolution_deg = 6.0 # Steps for compass
tilt_max_deg = 30.0 # Maximum tilt to check
resolution = np.deg2rad(resolution_deg)
tilt_max = np.deg2rad(tilt_max_deg)
rot_dict = hsc.create_rot_par(rot_par, resolution, tilt_max)
self.hsc_wrapped = hsc.HSC_wrapper(contin,
rot_dict,
n_points,
n_bands,
0,
silent)
(num_angles, theta_sample, phi_sample) = self.hsc_wrapped.get_angles()
points = (np.arange(X.shape[0]), np.arange(X.shape[1]))
indices = np.vstack([np.rad2deg(theta_sample),
np.rad2deg(phi_sample)]).T
self.xs = interpolate.interpn(points, X, indices)
self.ys = interpolate.interpn(points, Y, indices)
# OLD CODE
#indices = np.vstack([np.floor(np.rad2deg(theta_sample)).astype('uint'),
# np.floor(np.rad2deg(phi_sample)).astype('uint')]).T
#self.xs = X[indices[:,0], indices[:,1]]
#self.ys = Y[indices[:,0], indices[:,1]]
# for interpn
self.points = (np.arange(img_set.h), np.arange(img_set.w))
self.xi = np.vstack([self.ys, self.xs]).T # for interpn
super(SHProcDarioAS, self).__init__(img_set, cache_results)
def panorama_to_surf(self, I):
#surf_img_np = I[np.floor(self.ys).astype('uint'),
# np.floor(self.xs).astype('uint')]
surf_img_np = interpolate.interpn(self.points, I, self.xi)
surf_img_np = surf_img_np / 255.0 - 0.5
return surf_img_np.tolist()
def process_img_set(self):
z = np.empty([self.n_bands, len(self.img_set)], dtype=float)
for i, img in enumerate(self.img_set):
surf_img = self.panorama_to_surf(img)
#print surf_img
self.hsc_wrapped.set_image(0, surf_img)
z[:,i] = self.hsc_wrapped.calc_AS(0)
return (z, self.n_bands, 1)
class SHProcDarioMixed(ImgProc):
""" Dario's implementation of Spherical Harmonics. Works with panoramic images. Need to refactor and add disk shaped ones too"""
def __init__(self, img_set, X, Y, cache_results=True, contin=3,
n_points=2000, n_abands=120, n_bbands=3, silent=True):
self.n_abands = n_abands
self.n_bbands = n_bbands
self.name = "shdm%sa%sb" % (n_abands, n_bbands)
rot_par = 1 # 0:XYZ, 1:ZYZ, 2:ZYZ_FAST, 3:XYZ_FAST 4:XYZ_FREE
resolution_deg = 6.0 # Steps for compass
tilt_max_deg = 30.0 # Maximum tilt to check
resolution = np.deg2rad(resolution_deg)
tilt_max = np.deg2rad(tilt_max_deg)
rot_dict = hsc.create_rot_par(rot_par, resolution, tilt_max)
self.hsc_wrapped = hsc.HSC_wrapper(contin,
rot_dict,
n_points,
n_abands,
n_bbands,
silent)
(num_angles, theta_sample, phi_sample) = self.hsc_wrapped.get_angles()
# Bring in the matlab data here for remapping nicely
indices = np.vstack([np.floor(np.rad2deg(theta_sample)).astype('uint'),
np.floor(np.rad2deg(phi_sample)).astype('uint')]).T