1

I have 2 folders containing multiple images files following the same filename convention. I'd like to go through each images from each folder together and perform a pixel difference for the corresponding filename. I have the code for pixel difference of one image, I need the algorithm to return the processed difference-image for each operation while looping over both folders at the same time according to the filename, then write this image in a third folder.

I have this Folder structure:

**first camera**        **second camera**
20-06-01-0000.jpg       20-06-01-0000.png
20-06-02-0000.jpg       20-06-02-0000.png
20-06-03-0000.jpg       20-06-03-0000.png
...                     ...
20-06-30-0000.jpg       20-06-30-0000.png

Pixel difference:

import cv2

img1 = cv2.imread('/firstcamera/20-06-26-00000.jpg')
img2 = cv2.imread('/secondcamera/20-06-26-00000.png')
difference = cv2.subtract(img1,img2)
cv2.imwrite('d-20-06-26-00000.png', difference)

I want this Folder structure:

**first camera**     **second camera**      **difference**
20-06-01-0000.jpg    20-06-01-0000.png      d-20-06-01-0000.png     
20-06-02-0000.jpg    20-06-02-0000.png      d-20-06-02-0000.png
20-06-03-0000.jpg    20-06-03-0000.png      d-20-06-03-0000.png
...                  ...                    ...
20-06-30-0000.jpg    20-06-30-0000.png      d-20-06-30-0000.png

I know I can use os.listdir() and enumerate() but I can get it to work properly. Please help, thanks!

med09
  • 11
  • 1
  • Are you trying to compare the quality of compression between JPG and PNG formats? How about using your web browser to try to find your answer first before asking it? For example https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory – Palo Jul 23 '20 at 21:24
  • I checked the browser and no. Trying to perform the pixel subtraction for each pair of images that have the same date format as filename but belong to different folders, while walking through each file in these folders. e.g: For date 20-06-30, I want to calculate image difference for this pair and write this new image in a new folder. I have 875 images that need to be processed. – med09 Jul 23 '20 at 21:40

1 Answers1

1

If you can guarantee that the names are effectively the same you can just replace the extension.

import cv2
import os

FIRST_CAM = 'first-camera'
SECOND_CAM = 'second-camera'
DIFF_FOLDER = 'difference'

for img_jpg in os.listdir(FIRST_CAM):
    img1 = cv2.imread(os.path.join(FIRST_CAM, img_jpg))
    img_png = img_jpg[:-3] + "png"
    img2 = cv2.imread(os.path.join(SECOND_CAM, img_png))
    difference = cv2.subtract(img1, img2)
    img_diff = 'd-' + img_png
    # if it doesn't exist, create the DIFF_FOLDER folder.
    os.makedirs(DIFF_FOLDER, exist_ok=True)
    cv2.imwrite(os.path.join(DIFF_FOLDER, img_diff), difference)

As a complement, if you want to add one more level of segmentation you can verify if the cv2.subtract has none/any difference with np.all from numpy library and place the files in separate folders.

...
import numpy as np
...
...
    difference = cv2.subtract(img1, img2)
    img_diff = 'd-' + img_png
    diff_level = "NO_DIFF" if np.all(difference == 0) else "SOME_DIFF"
    os.makedirs(os.path.join(DIFF_FOLDER, diff_level), exist_ok=True)
    cv2.imwrite(os.path.join(DIFF_FOLDER, diff_level, img_diff), difference)
...
n1colas.m
  • 3,863
  • 4
  • 15
  • 28