Skip to content
Advertisement

Create an image using the pixels median fom other images

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:

imgs=['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg']

and then I wrote a for loop like that:

for image in imgs:

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

import cv2
import numpy as np

imgs=['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg'] 
np_images = []

for img in imgs:
    image = cv2.imread(img,1)
    if image is None:
        print(img, "doesnot exist")
    else:
        np_images.append(image)
# assuming the dimensions of all images are the same
np_array = np.array(np_images)
median_image = np.median(np_array,axis=0)
cv2.imwrite("median_image.jpg",median_image)

Edit:

  • np_images is an array to store images read by opencv
  • p_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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement