Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- import sys
- import threading
- import matplotlib.pyplot as plt
- import time
- import copy
- from cv2 import VideoWriter, VideoWriter_fourcc
- from scipy.stats import pearsonr
- filename = "car.gif"
- def psnr(img1, img2):
- mse = np.mean((img1 - img2) ** 2)
- if mse == 0:
- return 100
- PIXEL_MAX = 255.0
- return 10 * np.log10(PIXEL_MAX / np.sqrt(mse))
- def rmse(img1, img2):
- return cv2.norm(img1, img2, cv2.NORM_L1)
- def shift(pic, j, i):
- height = pic.shape[0]
- width = pic.shape[1]
- if j >= 0:
- pic = pic[j:, :] # строка столбец
- if i >= 0:
- pic = pic[:, :width - i]
- elif i < 0:
- pic = pic[:, -i:]
- elif j < 0:
- pic = pic[:height + j, :]
- if i >= 0:
- pic = pic[:, :width - i]
- elif i < 0:
- pic = pic[:, -i:]
- return pic
- def fcor(pic1, pic2, mi, mj, l, maxshiftx, maxshifty, startx, starty):
- for j in range(0, maxshiftx):
- for i in range(0, maxshifty):
- l.append((startx + j * mj, starty + i * mi, pearsonr(shift(pic1, -(starty + i * mi), -(startx + j * mj)).ravel(),
- shift(pic2, starty + i * mi, startx + j * mj).ravel())[0]))
- def pearson(pic1, pic2, maxshiftx, maxshifty, startx, starty):
- l = []
- t1 = threading.Thread(target=fcor, args=(
- pic1, pic2, 1, 1, l, maxshiftx, maxshifty, startx, starty))
- t1.start()
- t2 = threading.Thread(target=fcor, args=(
- pic1, pic2, 1, -1, l, maxshiftx, maxshifty, startx, starty))
- t2.start()
- t3 = threading.Thread(target=fcor, args=(
- pic1, pic2, -1, -1, l, maxshiftx, maxshifty, startx, starty))
- t3.start()
- t4 = threading.Thread(target=fcor, args=(
- pic1, pic2, -1, 1, l, maxshiftx, maxshifty, startx, starty))
- t4.start()
- t1.join()
- t2.join()
- t3.join()
- t4.join()
- return l[np.argmax(np.asarray(l)[:, 2])]
- def fcor1(pic1, pic2, mi, mj, l, maxshiftx, maxshifty, startx, starty):
- for j in range(0, maxshiftx + 1):
- for i in range(0, maxshifty + 1):
- l.append((startx + j * mj, starty + i * mi, rmse(shift(pic1, -(starty + i * mi), -(startx + j * mj)).ravel(),
- shift(pic2, starty + i * mi, startx + j * mj).ravel())))
- def trmse(pic1, pic2, maxshiftx, maxshifty, startx, starty):
- l = []
- t1 = threading.Thread(target=fcor1, args=(
- pic1, pic2, 1, 1, l, maxshiftx, maxshifty, startx, starty))
- t1.start()
- t2 = threading.Thread(target=fcor1, args=(
- pic1, pic2, 1, -1, l, maxshiftx, maxshifty, startx, starty))
- t2.start()
- t3 = threading.Thread(target=fcor1, args=(
- pic1, pic2, -1, -1, l, maxshiftx, maxshifty, startx, starty))
- t3.start()
- t4 = threading.Thread(target=fcor1, args=(
- pic1, pic2, -1, 1, l, maxshiftx, maxshifty, startx, starty))
- t4.start()
- t1.join()
- t2.join()
- t3.join()
- t4.join()
- return l[np.argmin(np.asarray(l)[:, 2])]
- vidcap = cv2.VideoCapture(filename)
- success = True
- imgList = []
- while success:
- success, image = vidcap.read()
- if success:
- imgList.append(image)
- print("List done")
- vidcap.release()
- cv2.destroyAllWindows()
- # obraz = (np.around(np.mean(imgList, 0))).astype('uint8')
- obraz = (np.around(np.median(imgList, 0))).astype('uint8')
- #cv2.imshow('Before', obraz)
- # cv2.waitKey(0)
- resvid = []
- pearsonList1 = []
- psnrList1 = []
- pearsonList2 = []
- psnrList2 = []
- tList = []
- xList = []
- yList = []
- shiftx = imgList[0].shape[1] // 60
- shifty = imgList[0].shape[0] // 30
- print(shiftx, shifty)
- count = 0
- for el in imgList:
- #im1 = cv2.absdiff(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(el, cv2.COLOR_BGR2GRAY))
- #cv2.imshow('Before', cv2.bitwise_not(im1))
- # cv2.waitKey(0)
- pearsonList1.append(pearsonr(obraz.ravel(), el.ravel())[0])
- psnrList1.append(psnr(obraz, el))
- t1 = time.time()
- msg = trmse(cv2.resize(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA),
- cv2.resize(cv2.cvtColor(el, cv2.COLOR_BGR2GRAY), (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA), shiftx, shifty, 0, 0)
- msg = pearson(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(
- el, cv2.COLOR_BGR2GRAY), 2, 2, msg[0] * 4, msg[1] * 4)
- t2 = time.time()
- print(len(imgList) - count, msg[1], msg[0], msg[2], t2-t1)
- tList.append(t2 - t1)
- xList.append(np.abs(msg[1]))
- yList.append(np.abs(msg[0]))
- tmp = shift(el, msg[1], msg[0]) # y x
- res = copy.copy(obraz)
- if msg[1] >= 0:
- if msg[0] >= 0:
- res[0:tmp.shape[0], msg[0]:msg[0] + tmp.shape[1]] = tmp
- elif msg[0] < 0:
- res[0:tmp.shape[0], 0:tmp.shape[1]] = tmp
- elif msg[1] < 0:
- if msg[0] >= 0:
- res[-msg[1]:-msg[1] + tmp.shape[0],
- msg[0]:msg[0] + tmp.shape[1]] = tmp
- elif msg[0] < 0:
- res[-msg[1]:-msg[1] + tmp.shape[0], 0:tmp.shape[1]] = tmp
- pearsonList2.append(np.corrcoef(obraz.ravel(), res.ravel())[0, 1])
- psnrList2.append(psnr(obraz, res))
- resvid.append(cv2.resize(res, (0, 0), fx=1, fy=1))
- #im1 = cv2.absdiff(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(el, cv2.COLOR_BGR2GRAY))
- #cv2.imshow('After', cv2.bitwise_not(im1))
- # cv2.waitKey(0)
- count += 1
- print("Pearson before", np.mean(pearsonList1))
- print("ITF before", np.mean(psnrList1))
- print("Pearson after", np.mean(pearsonList2))
- print("ITF after", np.mean(psnrList2))
- print("Average frametime", np.mean(tList))
- print("shiftX", np.max(xList))
- print("shiftY", np.max(yList))
- plt.title("pearson")
- plt.plot(pearsonList1, label="Before")
- plt.plot(pearsonList2, label="After")
- plt.legend()
- plt.show()
- plt.title("ITF")
- plt.plot(psnrList1, label="Before")
- plt.plot(psnrList2, label="After")
- plt.legend()
- plt.show()
- width = imgList[0].shape[1]
- height = imgList[0].shape[0]
- FPS = 25
- seconds = 12
- fourcc = VideoWriter_fourcc(*'MP42')
- video = VideoWriter("resVID2newoptim.avi", fourcc, float(FPS), (width, height))
- for el in resvid:
- video.write(el)
- video.release
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement