Reading images from adjacent folders - python

I have 4 different path to folders, but each folder are adjacent to each other and they're called faces 1, faces 2, faces 3 and faces 4. I'm using that path to read the faces images from inside and extract features from them and put them inside a feature_vector.
What i need to do is: read each face images folder and store them inside a feature vector, therefore, for each folder faces 1, faces 2, faces 3, i want to store them inside vec1, vec2, vec 3.
Here's what i am doing:
from os import listdir
from PIL import Image as PImage
def loadImages(path):
imagesList = listdir(path)
loadedImages = []
for image in imagesList:
img = PImage.open(path + image)
arr = np.array(img)
loadedImages.append(arr)
return loadedImages #function that loads all images inside the path given loadimages('path')
Thats my function for loading a path.
imgs = loadImages('C:/Dataset/face1')
coord_list = [[45, 100], [80, 45], [65, 50] , [110, 50], [110, 40], [90, 35], [90, 25], [100, 25], [89, 64], [60, 65], [65, 25], [65, 40], [120, 65], [75, 82], [105, 82], [88, 78], [90, 110], [89, 90], [65, 110], [110, 100]]
vec_1 = []
for img in imgs:
img_feats = []
for coords in coord_list:
img_feats.append(img[coords[0], coords[1]])
vec_1.append(img_feats)
Then i grab my array of coordinates and append each pixel from 50 images inside faces 1 to vec_1, which gives me a 50x22 array
What i want to do is to read inside faces 2 and store in vec_2, faces 3 inside vec_3 and faces 4 inside vec_4
I tried it like this, but it doesn't and also it does not look optmized.
imgs2 = loadImages('C:/Dataset/face2')
coord_list = [[45, 100], [80, 45], [65, 50] , [110, 50], [110, 40], [90, 35], [90, 25], [100, 25], [89, 64], [60, 65], [65, 25], [65, 40], [120, 65], [75, 82], [105, 82], [88, 78], [90, 110], [89, 90], [65, 110], [110, 100]]
vec_2 = []
for imgs2 in imgs:
img_feats = []
for coords in coord_list:
img_feats.append(img[coords[0], coords[1]])
feat_vec_2.append(img_feats)
imgs3 = loadImages('C:/Dataset/face3')
coord_list = [[45, 100], [80, 45], [65, 50] , [110, 50], [110, 40], [90, 35], [90, 25], [100, 25], [89, 64], [60, 65], [65, 25], [65, 40], [120, 65], [75, 82], [105, 82], [88, 78], [90, 110], [89, 90], [65, 110], [110, 100]]
vec_3 = []
for imgs3 in imgs:
img_feats = []
for coords in coord_list:
img_feats.append(img[coords[0], coords[1]])
vec_3.append(img_feats)
imgs4 = loadImages('C:/Dataset/face4')
coord_list = [[45, 100], [80, 45], [65, 50] , [110, 50], [110, 40], [90, 35], [90, 25], [100, 25], [89, 64], [60, 65], [65, 25], [65, 40], [120, 65], [75, 82], [105, 82], [88, 78], [90, 110], [89, 90], [65, 110], [110, 100]]
vec_4 = []
for imgs4 in imgs:
img_feats = []
for coords in coord_list:
img_feats.append(img[coords[0], coords[1]])
vec_4.append(img_feats)
The features from vec_1 kept appending to vec_2 and vec_1, vec_2 to vec_3....

For what I see, coord_list is always the same for every set of images, so you could condensate the assignation of all the four vec_n lists into one single for loop, by performing the appropriate string interpolation.
This solution will substitute the four vec_n variables for a dictionary (named vec_dict)containing the feature lists for each image set, under a vec_n key.
import os
base_path = os.path.join('C:', os.sep, 'Dataset')
vec_dict = {}
coord_list = [[45, 100], [80, 45], [65, 50] , [110, 50], [110, 40], [90, 35], [90, 25], [100, 25], [89, 64], [60, 65], [65, 25], [65, 40], [120, 65], [75, 82], [105, 82], [88, 78], [90, 110], [89, 90], [65, 110], [110, 100]]
for i in range(1, 5):
folder_path = os.path.join(base_path, f'face{i}')
images = loadImages(folder_path)
for img in images:
vec_dict.setdefault(f'vec_{i}', []).append([img[coords[0], coords[1]] for coords in coord_list])

Related

How to calculate which Voronoi cells are in specific area in Python?

I have some points which illustrates heads of pedestrians during an experiment in each frame. I need to calculate which Voronoi Cells are in specific area - measurement square:
x_range = (-0.4, 0.4)
y_range = (0.5, 1.3)
So I've adapted a sample of generating Voronoi cells from points + I've added measurement area (blue lines) and walls (black), here is result for frame 0:
And here is part of code (adapted from the sample):
entries_for_frame = get_entries_at_frame(entries, frame)
points = points_from_entries(entries_for_frame)
vor = scipy.spatial.Voronoi(points)
scipy.spatial.voronoi_plot_2d(vor)
plt.show()
As I know in order to calculate which cells are inside the measurement area I need to check which lines of cells are crossing with the measurement square or are inside its.
So according to the documentation of scipy.spatial.Voronoi the interesting attributes are: vertices, which is returning those orange vertices. I need also to have edges of vertices inside the measurement area, the attribute according to the documentation of scipy.spatial.Voronoi is ridge_vertices, but unfortunately it is returning something strange:
[[0, 19], [0, 2], [1, 17], [1, 3], [2, 3], [17, 19], [-1, 22], [-1, 15], [15, 16], [16, 21], [21, 22], [-1, 0], [2, 23], [22, 23], [28, 32], [28, 29], [29, 30], [30, 31], [31, 32], [12, 13], [12, 28], [13, 25], [25, 29], [-1, 24], [-1, 31], [24, 30], [-1, 26], [26, 27], [27, 32], [-1, 33], [19, 20], [20, 34], [33, 34], [35, 36], [-1, 35], [36, 37], [-1, 37], [-1, 4], [4, 5], [5, 35], [6, 37], [6, 7], [7, 36], [38, 39], [38, 40], [39, 41], [40, 41], [-1, 40], [-1, 8], [8, 38], [-1, 9], [9, 10], [10, 41], [10, 43], [39, 42], [42, 43], [52, 53], [52, 57], [53, 54], [54, 55], [55, 56], [56, 57], [13, 52], [25, 57], [48, 49], [48, 54], [49, 55], [9, 50], [24, 56], [49, 50], [17, 59], [18, 61], [18, 20], [59, 61], [11, 46], [11, 60], [18, 47], [46, 47], [60, 61], [58, 63], [58, 60], [59, 62], [62, 63], [26, 64], [27, 65], [64, 65], [21, 67], [23, 68], [67, 68], [42, 45], [43, 69], [44, 45], [44, 72], [69, 72], [50, 70], [69, 70], [48, 71], [70, 71], [4, 76], [5, 75], [75, 76], [33, 77], [76, 77], [34, 78], [77, 78], [47, 79], [78, 79], [80, 82], [80, 81], [81, 83], [82, 84], [83, 84], [14, 53], [14, 80], [71, 82], [72, 84], [14, 51], [51, 87], [81, 85], [85, 87], [88, 90], [88, 89], [89, 93], [90, 91], [91, 92], [92, 93], [44, 88], [83, 89], [85, 86], [86, 93], [11, 91], [58, 92], [94, 95], [94, 97], [95, 96], [96, 98], [97, 99], [98, 99], [12, 94], [51, 95], [65, 97], [101, 104], [101, 102], [102, 103], [103, 105], [104, 105], [15, 101], [16, 104], [64, 102], [99, 103], [66, 67], [66, 105], [1, 106], [3, 107], [106, 107], [68, 108], [107, 108], [8, 73], [45, 109], [73, 110], [109, 110], [111, 115], [111, 113], [112, 113], [112, 114], [114, 115], [46, 74], [74, 111], [79, 113], [75, 112], [7, 114], [116, 117], [116, 118], [117, 120], [118, 119], [119, 121], [120, 121], [96, 118], [98, 100], [100, 116], [87, 119], [86, 121], [63, 120], [122, 127], [122, 123], [123, 124], [124, 125], [125, 126], [126, 127], [100, 127], [117, 122], [62, 123], [106, 124], [108, 125], [66, 126], [128, 129], [128, 130], [129, 132], [130, 131], [131, 133], [132, 134], [133, 134], [90, 128], [109, 129], [74, 130], [110, 132], [115, 131], [6, 133], [73, 134]]
In documentation I don't see how to understand returned numbers. Also in tutorials in explaining how to solve my problem. So my question is: how to calculate which Voronoi cells are inside the measurement area with at least single point?
I believe that your best bet is to use some kind of multiple polygon intersection algorithm using the cell vertices to describe the polygons.
You can whittle down the number of polygons by discarding those whose rightmost vertex is left of the blue rectangle, those whose leftmost vertex is to the right, and so on for up and down. This leaves you with the yellow polygons only.
You can also quickly eliminate (only, in this case you mark them as "intersecting") all those whose center or vertex lies inside the rectangle. This also is very quick.
In this example this is enough to locate all cells.
In other cases (for example, in the figure below, if the bottom-left yellow cell was shifted slightly upwards) you will have cells that have all vertices and the Delaunay center outside the rectangle, and yet one edge crosses it, so there is an intersection. To recognize those, you can exploit the fact that a rectangle is a convex figure, and check whether, among the cells you've left, there is one that contains at least one of the rectangle's corners. This is a slightly more complex check ("whether a point lies inside a convex polygon"), but not too complex since the cell is also convex and can be trivially decomposed in triangles.
The pseudo algorithm would be:
for all Voronoi cells:
get list of vertices.
are they all left/below/above/right of the rectangle?
YES: this cell does not intersect. Continue.
for all the vertices plus the cell center:
is this point inside the rectangle?
YES: we have intersection. Report this cell and continue.
decompose the cell in a list of triangles with vertex in the
Delaunay center, taking ordered vertex pairs.
for each triangle
for each vertex of the rectangle
is the vertex inside the triangle?
YES: we have intersection. Report and continue
this cell does not intersect the rectangle.

How to convert pixels stored in a list into an image with python?

pix = [
[[90, 94, 6], [126, 108, 24], [180, 116, 42], [166, 116, 46], [72, 94, 31]],
[[101, 96, 14], [190, 165, 84], [202, 134, 63], [170, 115, 50], [40, 50, 0]],
[[145, 125, 53], [150, 112, 40], [148, 73, 6], [156, 90, 31], [25, 11, 1]],
[[133, 124, 57], [165, 142, 75], [195, 142, 77], [169, 120, 62], [82, 74, 28]],
[[73, 105, 40], [56, 77, 10], [138, 135, 67], [97, 95, 34], [45, 69, 21]],
]
I have a bunch of pixels stored in the list and now I want to convert it to an image. How can I turn that list into an image? Thank you
Using PIL, you can create an image using an array:
from PIL import Image
import numpy as np
img = Image.fromarray(np.array(pix).astype(np.uint8))
Now, you may look at the image:
img.show()
Good thing is, from now on, you can benefit from all of PIL's toolcase for image processing (resize, thumbnail, filters, ...).
Here's how to do it using OpenCV. By default, OpenCV uses Numpy arrays to display images so you can simply convert the list into a <class 'numpy.ndarray'>.
Result:
import numpy as np
import cv2
pix = [
[[90, 94, 6], [126, 108, 24], [180, 116, 42], [166, 116, 46], [72, 94, 31]],
[[101, 96, 14], [190, 165, 84], [202, 134, 63], [170, 115, 50], [40, 50, 0]],
[[145, 125, 53], [150, 112, 40], [148, 73, 6], [156, 90, 31], [25, 11, 1]],
[[133, 124, 57], [165, 142, 75], [195, 142, 77], [169, 120, 62], [82, 74, 28]],
[[73, 105, 40], [56, 77, 10], [138, 135, 67], [97, 95, 34], [45, 69, 21]],
]
# Convert to ndarray
img = np.array(pix).astype(np.uint8)
# Save image
cv2.imwrite('img.png', img)
# Display image
cv2.imshow('img', img)
cv2.waitKey()
The answer above transforms your list into a PIL Image. If you just want to see the image, you can do this:
import matplotlib.pyplot as plt
plt.imshow(pix)

Python 2d array

I want to make 2D array, which is 50X75.
Computer has to make random coordinates inside the array, about 15 to 20 coordinates.
What should I do TT
I stopped with the first step, making 50X75 2D array, so help meTT
You can generate 2D array using random runmbers
from random import randint
coordinates = [[randint(1, 100), randint(1, 100)] for i in range(20)]
Output: [[81, 52], [12, 79], [24, 90], [93, 53], [98, 17], [40, 44], [31, 1], [1, 40], [8, 34], [81, 31], [87, 50], [45, 72], [86, 70], [43, 78], [64, 80], [85, 76], [28, 43], [81, 78], [80, 55], [82, 58]]
A 50 x 75 2D array can be made using a np.reshape function. Here is an example, hope this helps.
import numpy as np
np.arange(3750).reshape(50, 75) # the array has 50 rows and 75 cols
array([[ 0, 1, 2, ..., 72, 73, 74],
[ 75, 76, 77, ..., 147, 148, 149],
[ 150, 151, 152, ..., 222, 223, 224],
...,
[3525, 3526, 3527, ..., 3597, 3598, 3599],
[3600, 3601, 3602, ..., 3672, 3673, 3674],
[3675, 3676, 3677, ..., 3747, 3748, 3749]])

Python numpy 2D array minimum values

In the array:
np.random.randint(100, size=(10, 2))
array([[ 8, 31],
[96, 97],
[26, 31],
[81, 70],
[47, 97],
[95, 84],
[11, 93],
[31, 77],
[25, 45],
[79, 22]])
I´d like to obtain [8, 22], the minimum values of each column.
How can I get it?
I'm just putting #gtlambert's comment into an answer, since it's probably the best choice. Use the array.min function
x = array([[ 8, 31],
[96, 97],
[26, 31],
[81, 70],
[47, 97],
[95, 84],
[11, 93],
[31, 77],
[25, 45],
[79, 22]])
In [6]: x.min(axis=0)
Out[6]: array([ 8, 22])
Probably not the most efficient, but...
left = np.array([])
right = np.array([])
for n in aaa:
left = np.append(left,n[0])
right = np.append(right,n[1])
sol = [np.min(left), np.min(right)]

Find maximum value of minimum elements in tuple

If I have a list
[[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
How would I go about finding the sublist with the maximum minimum element?
ie, in the case above, it would be index[3] - [75,156] because it's minimum value is greater than the minimum value of all other elements.
It should be as simple as:
max(list_of_iterables, key=min)
i.e.:
>>> lst = [[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
[[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
>>> max(lst, key=min)
[75, 156]
The max (and min) functions work by walking through an iterable and comparing each element in the iterable picking out the biggest (or smallest for min) element. The catch is that the thing compared is the result of the key function applied to the individual element. By default, the key function is just the identity function -- but it can be anything you want. In this case, my key function is min which picks out the minimum value of the sub-list. We then compare the sublists based on their minimum value and pick out the max which is exactly what your question asked for.
You can use sorted function.
>>> lst = [[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
[[209, 34], [50, 170], [197, 32], [75, 156], [176, 51], [54, 141], [205, 19], [35, 173]]
>>> sorted(lst, key=min, reverse=True)
[[75, 156],
[54, 141],
[176, 51],
[50, 170],
[35, 173],
[209, 34],
[197, 32],
[205, 19]]
key=min means it will use min function when sorting the list.
Then you can find the index of the value with index method. Like:
>>> lst.index([75, 156])
3

Categories