-1

I'm trying to take 5 consecutive pixels from each image of a database, and position them consecutively to create a new image of 250x250px. all images in the database are 250x250px. The Numpy array I'm getting has only 250 items in it, although the database has about 13,000 photos in it. Can someone help me spot the problem?

Current output for 'len(new_img_pxl)' = 250

Illustration

#edit:        
from imutils import paths
import cv2
import numpy as np

# access database
database_path = list(paths.list_images('database'))

#grey scale database
img_gray = []

x = -5
y = 0
r = 0

new_img_pxl = []

# open as grayscale, resize
for img_path in database_path:
    img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
    img_resize = cv2.resize(img, (250, 250))
    img_gray.append(img_resize)


# take five consecutive pixel from each image
for item in img_gray:
    x += 5
    y += 5
    five_pix = item[[r][x:y]]
    for pix in five_pix:
        new_img_pxl.append(pix)
    if y == 250:
        r += 1
        x = -5
        y = 0



# convert to array
new_img_pxl_array = np.array(new_img_pxl)
reshape_new_img = new_img_pxl_array.reshape(25,10)



# Convert the pixels into an array using numpy
array = np.array(reshape_new_img, dtype=np.uint8)
new_img_output = cv2.imwrite('new_output_save/001.png',reshape_new_img)
Guykowen
  • 1
  • 1
  • 1
    Mmmm... there are a few issues to think about here. 1) Why are you unnecessarily mixing **OpenCV** and **PIL** - you will confuse yourself. Just use one. 2) If you only need the images as greyscale, don't open them in colour and convert them, just open them as greyscale and conserve RAM, i.e. `grey = cv2.imread(..., cv2.IMREAD_GRAYSCALE)` 3) Why are you making a list of 13,000 images in memory??? That's 800MB of RAM. Open 1 image, get the 5 pixels you want and then move on to the next image. – Mark Setchell Nov 03 '21 at 09:21
  • Thank you, Mark, I'm new to python and now I understand that I didn't use efficient methods. Your comments were helpful. I changed the code as recommended. – Guykowen Nov 07 '21 at 19:04
  • As for the 3d recommendation, would that mean combining the two loops? I didn't manage to succeed with that one. – Guykowen Nov 07 '21 at 19:12
  • You are still making a massive list of all the images at this line `img_gray.append(img_resize)` – Mark Setchell Nov 07 '21 at 20:48
  • Thank you, Mark – Guykowen Nov 08 '21 at 19:52

1 Answers1

0

your bug is in the second loop.

for item in img_gray:

for every image (i) in the list img_gray you do:

for a in item:

for each row (j) in the image (i), extract 5 pixels and append them to new_img_pxl.

the first bug is that you don't take just 5 pixels from each image, you take 5 pixels from each row of each image.

your 2nd bug is that after extracting 250 pixels the values of the variables x and y are higher than 250 (the length of a row). As a result, when you try to access the pixels [250:255] and so on you get 'None'.

If I understand your intentions, then the way you should have implemented this is as follows:

r = 0
# As Mark Setchell suggested, you might want to change iterating 
# over a list of images to iterating over the list of paths
# for img_path in database_path:
for item in img_gray:
    # As Mark Setchell suggested, you might wat to load and
    # process your image here, overwriting the past image and         
    # having the memory released
    x += 5
    y += 5
    # when you finish a row jump to the next?
    if x==250:
        x = 0
        y = 5
        r+=1
    # not sure what you wanna do when you get to the end of the image. 
    # roll back to the start?
    if r==249 && x==250:
        r = 0
        x = 0
        y = 5
    five_pix = a[r, x:y]
    for pix in five_pix:
        new_img_pxl.append(pix)
yann ziselman
  • 1,952
  • 5
  • 21
  • Yann, thanks for the help! I tried out the suggested code and didn't get a satisfying result. I think I'm missing out on something. I'm added to the post an illustration to try better explain my desired result. – Guykowen Nov 07 '21 at 19:33
  • @Guykowen your illustration does not include 250x250 images. What do you mean by 'not getting a satisfying result'? It is not clear, which pixels you're taking from the source images. Would you kindly describe what's exactly wrong with the results you got and address the questions in my answer? – yann ziselman Nov 08 '21 at 09:31