Related
I'm working on images, let's say that i have a row of the image matrix that has the values:
[1, 2, 3, 4, 5, 6, 7]
I want to resize this image using interpolation so that the row becomes something like:
[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7]
Could someone tell me what is this interpolation technique called, and how can i possibly use it? I tried PIL.Image.resize resampling filters but they don't give me the results i'm looking for.
Thank you in advance!
This doesn't look like an interpolation but rather a repetition.
You can use a custom repeater and numpy.repeat:
a = np.array([1, 2, 3, 4, 5, 6, 7])
MAX, r = divmod(a.shape[0], 2)
rep = np.arange(1, MAX+r+1).astype(int)
rep = np.r_[rep[r:][::-1], rep]
out = np.repeat(a, rep)
output: array([1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7])
N-dimensional
a = np.arange(20).reshape(4, 5)
def custom_repeat(arr, axis=0):
MAX, r = divmod(arr.shape[axis], 2)
rep = np.arange(1, MAX+r+1).astype(int)
rep = np.r_[rep[r:][::-1], rep]
return np.repeat(arr, rep, axis=axis)
custom_repeat(custom_repeat(a, axis=0), axis=1)
output:
array([[ 0, 0, 0, 1, 1, 2, 3, 3, 4, 4, 4],
[ 0, 0, 0, 1, 1, 2, 3, 3, 4, 4, 4],
[ 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9],
[10, 10, 10, 11, 11, 12, 13, 13, 14, 14, 14],
[15, 15, 15, 16, 16, 17, 18, 18, 19, 19, 19],
[15, 15, 15, 16, 16, 17, 18, 18, 19, 19, 19]])
Maybe this is what you're looking for
def my_procedure(current_list: list, index: int) -> list:
index_copy = index
table = []
status_add = True
for i in current_list:
for _ in range(index):
table.append(i)
if status_add:
index -= 1
else:
index += 1
if index == 1 or index == index_copy:
status_add = not status_add
return table
x = [1, 2, 3, 4, 5, 6, 7]
print(my_procedure(x, 4))
Possibly is pincushion distortion
My inputs are like this, i tried to make starting and ending points to control the routing from a point a --> (special scenario of my case: routing is from location 'a' to point 'a')
I try to get a routing with capacity , distance and time windows constraints, at this level, if i execute the code, I visualise the error bellow:
''TypeError: list indices must be integers or slices, not list ''
data['time_matrix'] = [
[0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7],
[6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14],
[9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9],
[8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16],
[7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14],
[3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8],
[6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5],
[2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10],
[3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6],
[2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5],
[6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4],
[6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10],
[4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8],
[4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6],
[5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2],
[9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9],
[7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0],
]
data['time_windows'] = [
(0, 5), # depot
(7, 12), # 1
(10, 15), # 2
(16, 18), # 3
(10, 13), # 4
(0, 5), # 5
(5, 10), # 6
(0, 4), # 7
(5, 10), # 8
(0, 3), # 9
(10, 16), # 10
(10, 15), # 11
(0, 5), # 12
(5, 10), # 13
(7, 8), # 14
(10, 15), # 15
(11, 15), # 16
]
data['num_vehicles'] = 4
data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
data['vehicle_capacities'] = [15, 15, 15, 15]
data['depot'] = [ 0, 0, 0, 0]
data['ends']= [ 5, 5, 5, 5]
My code is :
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
# Add time window constraints for each location except depot.
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == data['depot']:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
And when i execute the code it gives me this :
<ipython-input-10-8bb55ac15980> in main()
47 index = routing.Start(vehicle_id)
48 time_dimension.CumulVar(index).SetRange(
---> 49 data['time_windows'][depot_idx][0],
50 data['time_windows'][depot_idx][1])
51
TypeError: list indices must be integers or slices, not list
Can anyone please tell me where and what it is the problem, because I tried to make "depot_idx" as arrays but in vain ?
You're trying to access a list item by giving another list (depot_idx is a list):
depot_idx = data['depot'] = [ 0, 0, 0, 0]
For accessing items in a list you need to use integers or slices that are representing the indexes you want to access.
In your case you need to pass an integer because your trying to access then the first element of the item (index 0):
data['time_windows'][YOUR_INTEGER][0]
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
Here, depot_idx is a list.
You mismatch index and node index
so this should work:
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
start_index = routing.Start(vehicle_id) # solver index space
start_node = depot_idx[vehicle_id] # your index space or
# start_node = manager.IndexToNode(start_index)
time_dimension.CumulVar(start_index).SetRange(
data['time_windows'][start_node][0],
data['time_windows'][start_node][1])
side note: Here you have manager.IndexToNode(start_index) == start_node BUT the opposite is undefined aka you can't use manager.NodeToIndex(start_node) since the result is ambiguous (i.e. not a single integer) actually the result should be [routing.Start(v) for v in range(data['num_vehicles'])] but since API should return an integer NodeToIndex() is undefined for start/end nodes...
I have a matrix like the following:
A = array([[12, 6, 14, 8, 4, 1],
[18, 13, 8, 10, 9, 19],
[ 8, 15, 6, 5, 6, 18],
[ 3, 0, 2, 14, 13, 12],
[ 4, 4, 5, 19, 0, 14],
[16, 8, 7, 7, 11, 0],
[ 3, 11, 2, 19, 11, 5],
[ 4, 2, 1, 9, 12, 12]])
For each cell I want to select the values in a radius of k=2 closest cells.
For instance if I select the A[3,4] I would like a submatrix like the following
array([[18, 13, 8, 10, 9],
[ 8, 15, 6, 5, 6],
[ 3, 0, 2, 14, 13],
[ 4, 4, 5, 19, 0],
[16, 8, 7, 7, 11]])
I defined the following function
def queen_neighbourhood(Adj, in_row, in_col, k):
j=k
k+=1
neighbourhood = Adj[in_row-j:in_row+k, in_col-j:in_col+k]
return neighbourhood
such as queen_neighbourhood(A, 3, 2, 2) returns
array([[18, 13, 8, 10, 9],
[ 8, 15, 6, 5, 6],
[ 3, 0, 2, 14, 13],
[ 4, 4, 5, 19, 0],
[16, 8, 7, 7, 11]])
However it does not work in borders.
For instance, for the cell [0,0] I would like to have
array([[12, 6, 14],
[18, 13, 8],
[ 8, 15, 16])
but it returns queen_neighbourhood(A, 0, 0, 2)
array([], shape=(0, 0), dtype=int64)
You could avoid negative indices:
neighbourhood = Adj[max(in_row-j, 0) : in_row+k,
max(in_col-j, 0) : in_col+k]
Adding to the previous answer; taking into consideration the extreme values
def queen_neighbourhood(Adj, in_row, in_col, k):
j=k
k+=1
neighbourhood = Adj[max(in_row-j, 0) : min(in_row+k,Adj.shape[0]),
max(in_col-j, 0) : min(in_col+k,Adj.shape[1])]
return(neighbourhood)
You can use numpy roll to ensure you are always dealing with the middle value,
import numpy as np
def queen_neighbourhood(Adj, in_row, in_col, k):
j=k
k+=1
midrow = int(Adj.shape[0]/2.)+1
midcol = int(Adj.shape[1]/2.)+1
Ashift = np.roll(Adj,(in_row-midrow,in_col-midcol),(0,1))
neighbourhood = Ashift[1:k+1, 1:k+1]
return neighbourhood
A = np.array([[18, 13, 8, 10, 9],
[ 8, 15, 6, 5, 6],
[ 3, 0, 2, 14, 13],
[ 4, 4, 5, 19, 0],
[16, 8, 7, 7, 11]])
print(A)
An = queen_neighbourhood(A, 0, 0, 2)
print(An)
which gives,
[[11 16 8]
[ 9 18 13]
[ 6 8 15]]
I have a list(T) of 6500 images(arrays) that I am using for image classification, and I would like to see how increasing the data affects the accuracy.
So, starting from n=2000 images, I am thinking of having a loop that will add 500(n+=500) images at each iteration till it reaches 6500 and therefore compare the accuracy between 2000, 2500, 3000, ... 6500. I have simplified the problem below by having a list of 20 elements.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
My second list (slist) contains the first 9 elements of the first list (lst).
I am trying to add 2 values to slist at each iteration, starting from lst[9:]. I know rather than using append, extend should be used to add multiple values at once. However, I couldn't find a way to do it.
In the following code, one element is added to slist (from lst) at each loop.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = lst[:9]
for i in lst[9:]:
slist.append(i)
How can I add 2 or 3 elements simultaneously at each loop? An example output would be:
[1,2,3,4,5,6,7,8,9,0,1]
[1,2,3,4,5,6,7,8,9,0,1,2,3]
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
You could try using extend:
l = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = l[:9]
for i in l[9:][::2]:
if i == l[9]:
slist.extend(l[9+i: 9+i+1])
else:
slist.extend(l[9+i-1: 9+i+1])
print(slist)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0]
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20]
iter = 9
while True:
print(lst[:iter])
iter+=2
if len(lst) <= iter:
print(lst[:iter])
break
This code does the job
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,21]
slist= lst[:9]
s,f=0,2
while True:
slist.extend(lst[9:][s:f])
print(slist)
s+=2
f+=2
if len(slist) >= len(lst):
break
It prints out:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
This is my first time handling multidimensional arrays and I'm having problems accessing elements. I'm trying to get the red pixels of a picture but just the first 8 elements within the array. Here's the code
import Image
import numpy as np
im = Image.open("C:\Users\Jones\Pictures\1.jpg")
pix = im.load()
r, g, b = np.array(im).T
print r[0:8]
Since you're dealing with images, r is a 2-D array. To get the first 8 pixels in the image, try
r.flatten()[:8]
This will wrap around automatically if the first row has less than 8 pixels.
do you want all rows too? Try this r[:,:8]
only want the first row? Try this r[0,:8]
You can do it like this:
r[0][:8]
Note, however, that this will not work if the first row has less than 8 pixels. To fix that, do this:
from itertools import chain
r = list(chain.from_iterable(r))
r[:8]
or (if you don't want to import an entire module):
r = [val for element in r for val in element]
r[:8]
I think it could be more simple. This example uses a random matrix (this will be your r matrix):
In [7]: from pylab import * # convention
In [8]: r = randint(0,10,(10,10)) # this is your image
In [9]: r
array([[7, 9, 5, 5, 6, 8, 1, 4, 3, 4],
[5, 4, 4, 4, 2, 6, 2, 6, 4, 2],
[1, 4, 9, 9, 2, 6, 1, 9, 0, 6],
[5, 9, 0, 7, 9, 9, 5, 2, 0, 7],
[8, 3, 3, 9, 0, 0, 5, 9, 2, 2],
[5, 3, 7, 8, 8, 1, 6, 3, 2, 0],
[0, 2, 5, 7, 0, 1, 0, 2, 1, 2],
[4, 0, 4, 5, 9, 9, 3, 8, 3, 7],
[4, 6, 9, 9, 5, 9, 3, 0, 5, 1],
[6, 9, 9, 0, 3, 4, 9, 7, 9, 6]])
Then, extract first 8 columns and do something
In [17]: r_8 = r[:,:8] # extract columns
In [18]: r_8
Out[18]:
array([[7, 9, 5, 5, 6, 8, 1, 4],
[5, 4, 4, 4, 2, 6, 2, 6],
[1, 4, 9, 9, 2, 6, 1, 9],
[5, 9, 0, 7, 9, 9, 5, 2],
[8, 3, 3, 9, 0, 0, 5, 9],
[5, 3, 7, 8, 8, 1, 6, 3],
[0, 2, 5, 7, 0, 1, 0, 2],
[4, 0, 4, 5, 9, 9, 3, 8],
[4, 6, 9, 9, 5, 9, 3, 0],
[6, 9, 9, 0, 3, 4, 9, 7]])
In [19]: r_8 = r_8 * 2 # do something
In [20]: r_8
Out[20]:
array([[14, 18, 10, 10, 12, 16, 2, 8],
[10, 8, 8, 8, 4, 12, 4, 12],
[ 2, 8, 18, 18, 4, 12, 2, 18],
[10, 18, 0, 14, 18, 18, 10, 4],
[16, 6, 6, 18, 0, 0, 10, 18],
[10, 6, 14, 16, 16, 2, 12, 6],
[ 0, 4, 10, 14, 0, 2, 0, 4],
[ 8, 0, 8, 10, 18, 18, 6, 16],
[ 8, 12, 18, 18, 10, 18, 6, 0],
[12, 18, 18, 0, 6, 8, 18, 14]])
Now, this is the trick. Replace the first 8 columns in r using hstack:
In [21]: r = hstack((r_8, r[:,8:])) # it replaces the FISRT 8 columns, note the indexing notation
In [22]: r
Out[22]:
array([[14, 18, 10, 10, 12, 16, 2, 8, 3, 4], # it does not touch the last 2 columns
[10, 8, 8, 8, 4, 12, 4, 12, 4, 2],
[ 2, 8, 18, 18, 4, 12, 2, 18, 0, 6],
[10, 18, 0, 14, 18, 18, 10, 4, 0, 7],
[16, 6, 6, 18, 0, 0, 10, 18, 2, 2],
[10, 6, 14, 16, 16, 2, 12, 6, 2, 0],
[ 0, 4, 10, 14, 0, 2, 0, 4, 1, 2],
[ 8, 0, 8, 10, 18, 18, 6, 16, 3, 7],
[ 8, 12, 18, 18, 10, 18, 6, 0, 5, 1],
[12, 18, 18, 0, 6, 8, 18, 14, 9, 6]])
EDIT: as to what DSM pointed out, OP is infact using a numpy array.
i retract my answer as nneonneo's correct