-
Notifications
You must be signed in to change notification settings - Fork 2
/
Geometric_partitioning.py
144 lines (86 loc) · 2.96 KB
/
Geometric_partitioning.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
import cv2
import numpy
origin_file = "lena.png"
unknow_file = "1000.png"
block_w = 256
block_h = 256
###############################################################
# Functions that have no external dependence
# draw a black line on the border of image buffer
def draw_border(image_buffer, width, height):
if len(image_buffer) < height:
return
if len(image_buffer[0]) < width:
return
for i in range(0, width):
image_buffer[i][0] = [0,0,0]
image_buffer[i][height-1] = [0,0,0]
for j in range(0, height):
image_buffer[0][j] = [0,0,0]
image_buffer[width-1][j] = [0,0,0]
# Give a image file, return image buffer with aligned size
def image_file_align(origin_file, block_w, block_h):
img = cv2.imread(origin_file)
height, width, channels = img.shape
print type(img[0][0][0])
# Compute aligned_width and aligned_height
aligned_width = ((width + block_w-1) & (~(block_w-1)))
aligned_height = ((height + block_h-1) & (~(block_h-1)))
# Create a new buffer to hold the aligned image
new_img = numpy.zeros(shape=(aligned_width, aligned_height, channels), dtype=numpy.uint8)
print type(new_img[0][0][0])
for i in range(0, width):
for j in range(0, height):
new_img[i][j] = img[i][j]
for i in range(width, aligned_width):
for j in range(height, aligned_height):
new_img[i][j] = [0,0,0]
return new_img, aligned_width, aligned_height
###############################################################
# Given a image file, cropping it
def image_cropping(origin_file):
img = cv2.imread(origin_file)
height, width, channels = img.shape
print height, width, channels
# Crop image to unit blocks
for i in range(0, width, block_w):
for j in range(0, height, block_h):
print i, j
crop_part = img[j:j+block_h, i:i+block_w]
cv2.imshow("crop", crop_part)
cv2.waitKey(0)
# Given a image file, try to crop but not seperate them
def image_draw_cropping(origin_file):
# Generate block_ size aligned image buffer
img, width, height = image_file_align(origin_file, block_w, block_h)
# Crop image to unit blocks
for i in range(0, width, block_w):
for j in range(0, height, block_h):
draw_border(img[j:j+block_h, i:i+block_w], block_w, block_h)
cv2.imshow("crop", img)
cv2.waitKey(0)
image_draw_cropping(unknow_file)
# Given a unknow file, explore all the possibilities
def cropping_explore(unknow_file):
img = cv2.imread(unknow_file)
height, width, channels = img.shape
# remove one row
for i in range(1, block_w):
crop_img = img[0:height, i:width]
cv2.imshow("", crop_img)
cv2.waitKey(0)
#cv2.imwrite("cut.png", crop_img)
# remove one column
for j in range(1, block_h):
crop_img = img[j:height, 0:width]
cv2.imshow("", crop_img)
cv2.waitKey(0)
#cv2.imwrite("cut.png", crop_img)
'''
crop_img = img[200:400, 100:300]
# Crop from x, y, w, h -> 100, 200, 300, 400
# NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
'''
#cv2.imwrite('messigray.png',img)