Minnie
Minnie

Reputation: 67

Is there a way to read first N images , perform operations and read next N images from a folder which has multiple images in Python

I'm working with an Image processing algorithm which involves taking triplet of images to compare errors.Iam confused about the basic step of reading set of 3 images from a folder performing some operation such as feature extraction and reading next set of 3 images to perform operation and so on..

I wrote the code to extract first 3 images from 1500 images. In order to perform feature extraction I have to take triplet of images together as input to the algorithm, extract the features and store into a matrix. When I extract features from 3 images together,I need to get a feature matrix of 3*37 , Assuming I have 37 features.How do I get the feature matrix for all images?


imageDir_meas = #specify your path here
image_path_list = []
valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff"] #specify your vald extensions here
valid_image_extensions = [item.lower() for item in valid_image_extensions]

#create a list all files in directory and
#append files with a vaild extention to image_path_list

for file in os.listdir(imageDir_meas):
    extension = os.path.splitext(file)[1]
     if extension.lower() not in valid_image_extensions:
        continue
    image_path_list.append(os.path.join(imageDir_meas, file))

for imagePath in image_path_list[:3]:
    image = cv2.imread(imagePath)

    # display the image on screen with imshow()
    # after checking that it loaded
    if image is not None:
        cv2.imshow(imagePath, image)
    elif image is None:
        print ("Error loading: " + imagePath)
        #end this loop iteration and move on to next image
        continue
    key = cv2.waitKey(0)
    if key == 27: # escape
        break
cv2.destroyAllWindows()

Upvotes: 0

Views: 935

Answers (1)

nnovember
nnovember

Reputation: 26

n_input = 3 # your algorithm needs 3 images
image_path_list_sub = [image_path_list[x:x+n_input] for x in range(0, len(image_path_list), n_input)]
for imagePath in image_path_list_sub:
    if len(imagePath) == 3:
        img0 = cv2.imread(imagePath[0])
        img1 = cv2.imread(imagePath[1])
        img2 = cv2.imread(imagePath[2])
        # your_algorithm(img0, img1, img2)
    else:
        continue

Upvotes: 1

Related Questions