How do i save an arrays in an array in python? - python

I would like to store 25 arrays in a 5x5 array in python.
Currently, I am trying to slice an image using openCV into 25 pieces using nested for loops.
I am having difficulty storing the cropped images in the slices array
board = cv.imread("King Domino dataset/Cropped and perspective corrected boards/1.jpg",1)
tileDimW = int(board.shape[0]/5)
tileDimH = int(board.shape[1]/5)
slices = np.array([5,5])
slice = np.array([tileDimH,tileDimW])
for h in range(5):
for w in range(5):
slice = board[tileDimH*h:tileDimH*(h+1),tileDimW*w:tileDimW*(w+1)]
slices[h,w] = slice
I get the error message:
"IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed" in the final line

Update:
To address what I am guessing is your actual question (see my comment above).
Assuming your board is an array with the shape (X, Y, ...). If you want split that up into 25 tiles shaped (X/5, Y/5, ...), you can simply do the following:
Split it into 5 "vertical" tiles once along axis 1 (horizontally or column-wise) giving you an array with the shape (5, X, Y/5, ...), i.e. with each tile having the shape (X, Y/5, ...).
Split that array into 5 again along axis 1, which effectively means splitting each of the 5 tiles along their respective axis 0 (vertically or row-wise). Each of those tiles we got from step 1 will then have the shape (5, X/5, Y/5, ...), meaning each sub-tile will be split into 5 tiles of shape (X/5, Y/5, ...).
Say we have array a with the shape (10, 15):
a = np.arange(150).reshape(10, 15)
print(a)
[[ 0 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]]
Step 1, using numpy.hsplit, which is equivalent to numpy.split with axis=1:
a1 = np.array(np.hsplit(a, 5))
print(a1)
[[[ 0 1 2]
[ 15 16 17]
...
[120 121 122]
[135 136 137]]
[[ 3 4 5]
[ 18 19 20]
...
[123 124 125]
[138 139 140]]
[[ 6 7 8]
[ 21 22 23]
...
[126 127 128]
[141 142 143]]
[[ 9 10 11]
[ 24 25 26]
...
[129 130 131]
[144 145 146]]
[[ 12 13 14]
[ 27 28 29]
...
[132 133 134]
[147 148 149]]]
Step 2:
a2 = np.array(np.hsplit(a1, 5))
print(a2)
[[[[ 0 1 2]
[ 15 16 17]]
...
[[ 12 13 14]
[ 27 28 29]]]
[[[ 30 31 32]
[ 45 46 47]]
...
[[ 42 43 44]
[ 57 58 59]]]
...
...
...
[[[120 121 122]
[135 136 137]]
...
[[132 133 134]
[147 148 149]]]]
Thus you can achieve the final result in one line like so:
b = np.array(np.hsplit(np.array(np.hsplit(a, 5)), 5))
print(b.shape)
(5, 5, 2, 3)
Then you have 5 x 5 tiles with the shape (2, 3).
Thus, you should be able to achieve what you want, by doing this:
slices = np.array(np.hsplit(np.array(np.hsplit(board, 5)), 5))
Avoid for-loops as much as possible, if you are already working with numpy arrays. Almost always there is a numpy-solution that is orders of magnitude faster (and probably more concise at that).
Hope this helps.
Original answer:
Given an array a with the shape (25, X, Y, Z, ...) you can simply reshape it to (5, 5, X, Y, Z, ...) like this:
a.reshape((5, 5) + a.shape[1:])
For example given a = np.arange(25*2).reshape((25, 2)), the array looks like this:
array([[ 0, 1],
[ 2, 3],
...
[46, 47],
[48, 49]])
And after the reshaping it looks like this:
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9]],
...
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49]]])

Related

Is it possible to extend "im2col" and "col2im" to N-D images?

"Im2col" has already been implemented, Implement MATLAB's im2col 'sliding' in Python, efficiently for 2-D images in Python. I was wondering whether it is possible to extend this to arbitrary N-D images? Many applications involve high-dimensional data (e.g. convolutions, filtering, max pooling, etc.).
So the purpose of this question was really just to post my solution to this problem publicly. I could not seem to find such a solution on Google, so I decided to take a stab at it myself. Turns out the implementation is actually quite simple to extend from "Approach #2" in the post referenced in my question!
Efficient Implementation of N-D "im2col"
def im2col(im, win, strides = 1):
# Dimensions
ext_shp = tuple(np.subtract(im.shape, win) + 1)
shp = tuple(win) + ext_shp
strd = im.strides*2
win_len = np.prod(win)
try:
len(strides)
except:
strides = [strides]*im.ndim
strides = [min(i, s) for i, s in zip(im.shape, strides)]
# Stack all possible patches as an N-D array using a strided view followed by reshaping
col = np.lib.stride_tricks.as_strided(im, shape = shp, strides = strd).reshape(win_len, -1).reshape(-1, *ext_shp)
# Extract patches with stride and reshape into columns
slcs = tuple([slice(None, None, None)] + [slice(None, None, s) for s in strides])
col = col[slcs].reshape(win_len, -1)
return col
Efficient Implementation of N-D "col2im"
def col2im(col, im_shp, win, strides = 1):
# Dimensions
try:
len(strides)
except:
strides = [strides]*len(im_shp)
strides = [min(i, s) for i, s in zip(im_shp, strides)]
# Reshape columns into image
if col.ndim > 1:
im = col.reshape((-1, ) + tuple(np.subtract(im_shp, win)//np.array(strides) + 1))[0]
else:
im = col.reshape(tuple(np.subtract(im_shp, win)//np.array(strides) + 1))
return im
Verification That It Works
Let's define an arbitrary 3-D input:
x = np.arange(216).reshape(6, 6, 6)
print(x)
[[[ 0 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]]]
Let's extract all the patches with a non-uniform window and equal stride:
y = im2col(x, [1, 3, 2], strides = [1, 3, 2])
print(y.T) # transposed for ease of visualization
[[ 0 1 6 7 12 13]
[ 2 3 8 9 14 15]
[ 4 5 10 11 16 17]
[ 18 19 24 25 30 31]
[ 20 21 26 27 32 33]
[ 22 23 28 29 34 35]
[ 36 37 42 43 48 49]
[ 38 39 44 45 50 51]
[ 40 41 46 47 52 53]
[ 54 55 60 61 66 67]
[ 56 57 62 63 68 69]
[ 58 59 64 65 70 71]
[ 72 73 78 79 84 85]
[ 74 75 80 81 86 87]
[ 76 77 82 83 88 89]
[ 90 91 96 97 102 103]
[ 92 93 98 99 104 105]
[ 94 95 100 101 106 107]
[108 109 114 115 120 121]
[110 111 116 117 122 123]
[112 113 118 119 124 125]
[126 127 132 133 138 139]
[128 129 134 135 140 141]
[130 131 136 137 142 143]
[144 145 150 151 156 157]
[146 147 152 153 158 159]
[148 149 154 155 160 161]
[162 163 168 169 174 175]
[164 165 170 171 176 177]
[166 167 172 173 178 179]
[180 181 186 187 192 193]
[182 183 188 189 194 195]
[184 185 190 191 196 197]
[198 199 204 205 210 211]
[200 201 206 207 212 213]
[202 203 208 209 214 215]]
Let's convert this back to a (downsampled) image:
z = col2im(y, x.shape, [1, 3, 2], strides = [1, 3, 2])
print(z)
[[[ 0 2 4]
[ 18 20 22]]
[[ 36 38 40]
[ 54 56 58]]
[[ 72 74 76]
[ 90 92 94]]
[[108 110 112]
[126 128 130]]
[[144 146 148]
[162 164 166]]
[[180 182 184]
[198 200 202]]]
As you can see, the final output is indeed the downsampled image that we expect (you can easily check this by going value by value). The dimensionality and strides I chose were purely illustrative. There's no reason why the window size has to be the same as your stride or that you can't go higher than 3 dimensions.
Applications
If you want to use this practically, all you have to do is intercept the output of im2col before turning it back into an image. For example, if you want to do pooling, you could take the mean or the maximum across the 0th axis. If you want to do a convolution, you just need to multiply this by your flattened convolutional filter.
There may be more efficient alternatives to this already implemented under the hood of Tensorflow, etc. that are faster than "im2col." This is not meant to be the MOST efficient implementation. And of course, you could possibly optimize my code further by eliminating the intermediate reshaping step in "im2col," but it wasn't immediately obvious to me so I just left it at that. If you have a better solution, let me know. Anyways, hope this helps someone else looking for the same answer!

Python + OpenCV - Reading the image file name

I have the following code snippet:
img = cv2.imread('1.jpg')
When I print img, I get the result shown below. How can I return the 1.jpg part only?
[[[140 149 139]
[153 162 152]
[155 165 153]
...,
[ 44 20 8]
[ 46 22 10]
[ 46 22 10]]
[[151 160 150]
[156 165 155]
[152 162 150]
...,
[ 47 23 11]
[ 48 24 12]
[ 45 21 9]]
[[155 164 154]
[152 161 151]
[146 156 144]
...,
[ 47 23 11]
[ 49 25 13]
[ 49 25 13]]
...,
[[ 28 16 6]
[ 33 21 11]
[ 32 20 10]
...,
[144 131 105]
[150 137 111]
[151 138 112]]
[[ 33 18 9]
[ 34 19 10]
[ 34 20 8]
...,
[144 135 108]
[143 134 107]
[148 139 112]]
[[ 31 16 7]
[ 31 16 7]
[ 35 21 9]
...,
[145 141 112]
[137 133 105]
[143 139 111]]]
Thanks.
I believe cv2.imread returns a numpy array. So, there is no way to store the name unless you use a custom class.
class MyImage:
def __init__(self, img_name):
self.img = cv2.imread(img_name)
self.__name = img_name
def __str__(self):
return self.__name
Then, you can use this as:
>>> x = MyImage('1.jpg')
>>> str(x)
1.jpg
>>> x.img # the numpy array

How can I select n items and skip m from ndarray in python?

Let's say I have an ndarray with 100 elements, and I want to select the first 4 elements, skip 6 and go ahead like this (in other words, select the first 4 elements every 10 elements).
I tried with python slicing with step but I think it's not working in my case. How can I do that? I'm using Pandas and numpy, can they help? I searched around but I have found nothing like that kind of slicing. Thanks!
You could use NumPy slicing to solve your case.
For a 1D array case -
A.reshape(-1,10)[:,:4].reshape(-1)
This can be extended to a 2D array case with the selection to be made along the first axis -
A.reshape(-1,10,A.shape[1])[:,:4].reshape(-1,A.shape[1])
You could reshape the array to a 10x10, then use slicing to pick the first 4 elements of each row. Then flatten the reshaped, sliced array:
In [46]: print a
[ 0 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]
In [47]: print a.reshape((10,-1))[:,:4].flatten()
[ 0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43 50 51 52 53 60
61 62 63 70 71 72 73 80 81 82 83 90 91 92 93]
Use % 10:
print [i for i in range(100) if i % 10 in (0, 1, 2, 3)]
[0, 1, 2, 3, 10, 11, 12, 13, 20, 21, 22, 23, 30, 31, 32, 33, 40, 41, 42, 43, 50, 51, 52, 53, 60, 61, 62, 63, 70, 71, 72, 73, 80, 81, 82, 83, 90, 91, 92, 93]
shorter_arr = arr[np.arange(len(arr))%10 < 4]
In the example in OP, the input array is divisible by m+n. If it's not, then you could use the below function take_n_skip_m. It expands on #Divakar's answer by padding the input array to make it reshapeable into a proper 2D matrix; slice, flatten and slice again to get the desired outcome:
def take_n_skip_m(arr, n=4, m=6):
# in case len(arr) is not divisible by (n+m), get the remainder
remainder = len(arr) % (n+m)
# will pad arr with (n+m-remainder) 0s at the back
pad_size = (0, n+m-remainder)
# pad arr; reshape to create 2D array; take first n of each row; flatten 2D->1D
sliced_arr = np.pad(arr, pad_size).reshape(-1, n+m)[:, :n].flatten()
# remove any remaining padding constant if there is any (which depends on whether remainder >= n or not)
return sliced_arr if remainder >= n else sliced_arr[:remainder-n]
Examples:
>>> out = take_n_skip_m(np.arange(20), n=5, m=4)
>>> print(out)
[ 0 1 2 3 4 9 10 11 12 13 18 19]
>>> out = take_n_skip_m(np.arange(20), n=5, m=6)
>>> print(out)
[ 0 1 2 3 4 11 12 13 14 15]

Array reshape not mapping correctly to numpy meshgrid

I have a long 121 element array where the data is stored in ascending order and I want to reshape to an 11x11 matrix and so I use the NumPy reshape command
Z = data.attributevalue[2,time,axial,:]
Z = np.reshape(Z, (int(math.sqrt(datacount)), int(math.sqrt(datacount))))
The data should be oriented in a Cartesian plane and I create the mesh grid with the following
x = np.arange(1.75, 12.5, 1)
y = np.arange(1.75, 12.5, 1)
X,Y = np.meshgrid(x, y)
The issue is that rows of Z are in the wrong order so the data in the last row of the matrix should be in the first and vice-versa. I want to rearrange so the rows are filled in the proper manner. The starting array Z is assembled in the following arrangement [datapoint #1, datapoint #2 ...., datapoint #N]. Datapoint #1 should be in the top left and the last point in the bottom right. Is there a simple way of accomplishing this or do I have to make a function to changed the order of the rows?
my plot statement is the following
surf = self.ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
linewidth=1, antialiased=True)
***UPDATE****
I tried populating the initial array backwards and still no luck. I changed the orientation of the axis to the following
y = np.arrange(12.5,1,-1)
This flipped the data but my axis label is wrong so it is not a real solution to my issue. Any ideas?
It is possible that your original array does not look like a 1x121 array. The following code block shows how you reshape an array from 1x121 to 11x11.
import numpy as np
A = np.arange(1,122)
print A
print A.reshape((11,11))
Gives:
[ 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]
[[ 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]]

How to visualise the number of dimensions in an array for python numpy

I am experimenting the different dimensions one can have in an array using ndim().
x=np.arange(0,100,1).reshape(1,20,5)
The shape is:
[[[ 0 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]]]
After, print x.ndim shows the array dimension is 3
I cannot visualize why the dimension is 3.
How does the shapes of respective arrays look like with dimensions 0,1,2,3,4,5......?
A simply way to count dimension is counting [ in the output. One [ for one dimension. Here you have three [s, therefore you have 3 dimension. Since one of the dimension is 1, you may be mislead. Here is another example:
x=np.arange(0,24,1).reshape(2,2,6)
Then, x is
array([[[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11]],
[[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]])
Now, it is clear that x is a 3 dimension array.

Categories

Resources