I have six images and I want to find the median of each pixel and create a new pic from it. I created a list of my images in this manner:
JavaScript
x
2
1
imgs=['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg']
2
and then I wrote a for loop like that:
JavaScript
1
2
1
for image in imgs:
2
I’ve found that there’s a function that finds the median using numpy but I don’t know how to use it. Also, after finding the median how can I create a picture of all the medians in the correct position of the screen? How to convert the image into an array?
Advertisement
Answer
You can use opencv to read images
JavaScript
1
17
17
1
import cv2
2
import numpy as np
3
4
imgs=['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg']
5
np_images = []
6
7
for img in imgs:
8
image = cv2.imread(img,1)
9
if image is None:
10
print(img, "doesnot exist")
11
else:
12
np_images.append(image)
13
# assuming the dimensions of all images are the same
14
np_array = np.array(np_images)
15
median_image = np.median(np_array,axis=0)
16
cv2.imwrite("median_image.jpg",median_image)
17
Edit:
np_images
is an array to store images read by opencvp_value
is an array to store color value of pixel at(row,col)
np.zeros
is a function that produces zero matrix(matrix whose all values are zeros) of given shape.
To speed up the program.