Skip to content

Instantly share code, notes, and snippets.

@hirocarma
Created December 30, 2023 05:32
Show Gist options
  • Save hirocarma/4dc19995512476fe61b2dc7e9ca680b7 to your computer and use it in GitHub Desktop.
Save hirocarma/4dc19995512476fe61b2dc7e9ca680b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import shutil
import numpy as np
import cv2
def main():
IMG1_DIR = sys.argv[1]
IMG2_DIR = sys.argv[2]
OUT_DIR = sys.argv[3]
START_FRAME = 904
EXIT_FRAME = 82031
frame_ctr = 0
if os.path.exists(OUT_DIR):
shutil.rmtree(OUT_DIR)
os.mkdir(OUT_DIR)
files1 = os.listdir(IMG1_DIR)
files1 = sorted(files1)
for file1 in files1:
frame_ctr = frame_ctr + 1
if frame_ctr <= START_FRAME:
continue
if frame_ctr == EXIT_FRAME:
break
img_file1 = IMG1_DIR + '/'+ file1
img_file2 = IMG2_DIR + '/'+ file1
fname1 = file1.split('.')[0]
img1 = cv2.imread(img_file1)
img2 = cv2.imread(img_file2)
diff = cv2.absdiff(img1, img2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
img1_rgb = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
norm_diff = gray_diff / np.max(gray_diff)
diff_img = cv2.addWeighted(gray2, 0.1, gray_diff, 2, 100)
diff_colored = np.zeros_like(img2_rgb)
diff_colored[..., 0] = img2_rgb[..., 0] * norm_diff
diff_colored[..., 1] = img2_rgb[..., 1] * norm_diff
diff_colored[..., 2] = img2_rgb[..., 2] * norm_diff
cv2.imwrite(OUT_DIR + fname1 + '-diff_colored' + '.jpg',diff_colored)
cv2.imwrite(OUT_DIR + fname1 + '-diff_gray' + '.jpg',diff_img)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment