Advertisement
O_Egor

Численные методы (лаба номер unkown)

Jan 18th, 2022 (edited)
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import sys
  4. import threading
  5. import matplotlib.pyplot as plt
  6. import time
  7. import copy
  8. from cv2 import VideoWriter, VideoWriter_fourcc
  9. from scipy.stats import pearsonr
  10.  
  11. filename = "car.gif"
  12.  
  13.  
  14. def psnr(img1, img2):
  15.     mse = np.mean((img1 - img2) ** 2)
  16.     if mse == 0:
  17.         return 100
  18.     PIXEL_MAX = 255.0
  19.     return 10 * np.log10(PIXEL_MAX / np.sqrt(mse))
  20.  
  21.  
  22. def rmse(img1, img2):
  23.     return cv2.norm(img1, img2, cv2.NORM_L1)
  24.  
  25.  
  26. def shift(pic, j, i):
  27.     height = pic.shape[0]
  28.     width = pic.shape[1]
  29.  
  30.     if j >= 0:
  31.         pic = pic[j:, :]  # строка столбец
  32.  
  33.         if i >= 0:
  34.             pic = pic[:, :width - i]
  35.         elif i < 0:
  36.             pic = pic[:, -i:]
  37.     elif j < 0:
  38.         pic = pic[:height + j, :]
  39.  
  40.         if i >= 0:
  41.             pic = pic[:, :width - i]
  42.         elif i < 0:
  43.             pic = pic[:, -i:]
  44.  
  45.     return pic
  46.  
  47.  
  48. def fcor(pic1, pic2, mi, mj, l, maxshiftx, maxshifty, startx, starty):
  49.     for j in range(0, maxshiftx):
  50.         for i in range(0, maxshifty):
  51.             l.append((startx + j * mj, starty + i * mi, pearsonr(shift(pic1, -(starty + i * mi), -(startx + j * mj)).ravel(),
  52.                                                                  shift(pic2, starty + i * mi, startx + j * mj).ravel())[0]))
  53.  
  54.  
  55. def pearson(pic1, pic2, maxshiftx, maxshifty, startx, starty):
  56.     l = []
  57.     t1 = threading.Thread(target=fcor, args=(
  58.         pic1, pic2, 1, 1, l, maxshiftx, maxshifty, startx, starty))
  59.     t1.start()
  60.  
  61.     t2 = threading.Thread(target=fcor, args=(
  62.         pic1, pic2, 1, -1, l, maxshiftx, maxshifty, startx, starty))
  63.     t2.start()
  64.  
  65.     t3 = threading.Thread(target=fcor, args=(
  66.         pic1, pic2, -1, -1, l, maxshiftx, maxshifty, startx, starty))
  67.     t3.start()
  68.  
  69.     t4 = threading.Thread(target=fcor, args=(
  70.         pic1, pic2, -1, 1, l, maxshiftx, maxshifty, startx, starty))
  71.     t4.start()
  72.  
  73.     t1.join()
  74.     t2.join()
  75.     t3.join()
  76.     t4.join()
  77.  
  78.     return l[np.argmax(np.asarray(l)[:, 2])]
  79.  
  80.  
  81. def fcor1(pic1, pic2, mi, mj, l, maxshiftx, maxshifty, startx, starty):
  82.     for j in range(0, maxshiftx + 1):
  83.         for i in range(0, maxshifty + 1):
  84.             l.append((startx + j * mj, starty + i * mi, rmse(shift(pic1, -(starty + i * mi), -(startx + j * mj)).ravel(),
  85.                                                              shift(pic2, starty + i * mi, startx + j * mj).ravel())))
  86.  
  87.  
  88. def trmse(pic1, pic2, maxshiftx, maxshifty, startx, starty):
  89.     l = []
  90.     t1 = threading.Thread(target=fcor1, args=(
  91.         pic1, pic2, 1, 1, l, maxshiftx, maxshifty, startx, starty))
  92.     t1.start()
  93.  
  94.     t2 = threading.Thread(target=fcor1, args=(
  95.         pic1, pic2, 1, -1, l, maxshiftx, maxshifty, startx, starty))
  96.     t2.start()
  97.  
  98.     t3 = threading.Thread(target=fcor1, args=(
  99.         pic1, pic2, -1, -1, l, maxshiftx, maxshifty, startx, starty))
  100.     t3.start()
  101.  
  102.     t4 = threading.Thread(target=fcor1, args=(
  103.         pic1, pic2, -1, 1, l, maxshiftx, maxshifty, startx, starty))
  104.     t4.start()
  105.  
  106.     t1.join()
  107.     t2.join()
  108.     t3.join()
  109.     t4.join()
  110.  
  111.     return l[np.argmin(np.asarray(l)[:, 2])]
  112.  
  113.  
  114. vidcap = cv2.VideoCapture(filename)
  115. success = True
  116. imgList = []
  117.  
  118. while success:
  119.     success, image = vidcap.read()
  120.     if success:
  121.         imgList.append(image)
  122.  
  123. print("List done")
  124. vidcap.release()
  125. cv2.destroyAllWindows()
  126.  
  127. # obraz = (np.around(np.mean(imgList, 0))).astype('uint8')
  128. obraz = (np.around(np.median(imgList, 0))).astype('uint8')
  129. #cv2.imshow('Before', obraz)
  130. # cv2.waitKey(0)
  131. resvid = []
  132. pearsonList1 = []
  133. psnrList1 = []
  134. pearsonList2 = []
  135. psnrList2 = []
  136. tList = []
  137. xList = []
  138. yList = []
  139. shiftx = imgList[0].shape[1] // 60
  140. shifty = imgList[0].shape[0] // 30
  141. print(shiftx, shifty)
  142. count = 0
  143.  
  144. for el in imgList:
  145.     #im1 = cv2.absdiff(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(el, cv2.COLOR_BGR2GRAY))
  146.     #cv2.imshow('Before', cv2.bitwise_not(im1))
  147.     # cv2.waitKey(0)
  148.  
  149.     pearsonList1.append(pearsonr(obraz.ravel(), el.ravel())[0])
  150.     psnrList1.append(psnr(obraz, el))
  151.     t1 = time.time()
  152.     msg = trmse(cv2.resize(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA),
  153.                 cv2.resize(cv2.cvtColor(el, cv2.COLOR_BGR2GRAY), (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA), shiftx, shifty, 0, 0)
  154.     msg = pearson(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(
  155.         el, cv2.COLOR_BGR2GRAY), 2, 2, msg[0] * 4, msg[1] * 4)
  156.     t2 = time.time()
  157.     print(len(imgList) - count, msg[1], msg[0], msg[2], t2-t1)
  158.     tList.append(t2 - t1)
  159.     xList.append(np.abs(msg[1]))
  160.     yList.append(np.abs(msg[0]))
  161.     tmp = shift(el, msg[1], msg[0])  # y x
  162.     res = copy.copy(obraz)
  163.     if msg[1] >= 0:
  164.         if msg[0] >= 0:
  165.             res[0:tmp.shape[0], msg[0]:msg[0] + tmp.shape[1]] = tmp
  166.         elif msg[0] < 0:
  167.             res[0:tmp.shape[0], 0:tmp.shape[1]] = tmp
  168.     elif msg[1] < 0:
  169.         if msg[0] >= 0:
  170.             res[-msg[1]:-msg[1] + tmp.shape[0],
  171.                 msg[0]:msg[0] + tmp.shape[1]] = tmp
  172.         elif msg[0] < 0:
  173.             res[-msg[1]:-msg[1] + tmp.shape[0], 0:tmp.shape[1]] = tmp
  174.     pearsonList2.append(np.corrcoef(obraz.ravel(), res.ravel())[0, 1])
  175.     psnrList2.append(psnr(obraz, res))
  176.     resvid.append(cv2.resize(res, (0, 0), fx=1, fy=1))
  177.  
  178.     #im1 = cv2.absdiff(cv2.cvtColor(obraz, cv2.COLOR_BGR2GRAY), cv2.cvtColor(el, cv2.COLOR_BGR2GRAY))
  179.     #cv2.imshow('After', cv2.bitwise_not(im1))
  180.     # cv2.waitKey(0)
  181.  
  182.     count += 1
  183.  
  184. print("Pearson before", np.mean(pearsonList1))
  185. print("ITF before", np.mean(psnrList1))
  186. print("Pearson after", np.mean(pearsonList2))
  187. print("ITF after", np.mean(psnrList2))
  188. print("Average frametime", np.mean(tList))
  189. print("shiftX", np.max(xList))
  190. print("shiftY", np.max(yList))
  191.  
  192. plt.title("pearson")
  193. plt.plot(pearsonList1, label="Before")
  194. plt.plot(pearsonList2, label="After")
  195. plt.legend()
  196. plt.show()
  197.  
  198. plt.title("ITF")
  199. plt.plot(psnrList1, label="Before")
  200. plt.plot(psnrList2, label="After")
  201. plt.legend()
  202. plt.show()
  203.  
  204. width = imgList[0].shape[1]
  205. height = imgList[0].shape[0]
  206. FPS = 25
  207. seconds = 12
  208. fourcc = VideoWriter_fourcc(*'MP42')
  209. video = VideoWriter("resVID2newoptim.avi", fourcc, float(FPS), (width, height))
  210.  
  211. for el in resvid:
  212.     video.write(el)
  213. video.release
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement