<jupyter_start><jupyter_text>INF6804 Vision par ordinateurPolytechnique MontréalFlot optique par deux méthodes différentes<jupyter_code>import numpy as np
import cv2
import os
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Lecture des images en entrée. (Images provenant PETS 2006, http://www.cvg.reading.ac.uk/PETS2006/data.html)<jupyter_code>image_name = 'in000235.jpg'
if not os.path.exists(image_name):
  !gdown https://raw.githubusercontent.com/gabilodeau/INF6804/master/images/in000235.jpg
  !gdown https://raw.githubusercontent.com/gabilodeau/INF6804/master/images/in000240.jpg
image1 = (cv2.imread('in000235.jpg',cv2.IMREAD_GRAYSCALE))
plt.imshow(image1, cmap = plt.get_cmap('gray'))
plt.show()
image2 = (cv2.imread('in000240.jpg',cv2.IMREAD_GRAYSCALE))
plt.imshow(image2, cmap = plt.get_cmap('gray'))
plt.show()<jupyter_output><empty_output><jupyter_text>Par la méthode de Lucas et Kanade. Flot optique épars.<jupyter_code># Extraction de coins. On calculera le flux optique pour les coins.
corners1 = cv2.goodFeaturesToTrack(image1, maxCorners = 2000, qualityLevel = 0.1, minDistance = 3, blockSize = 5 )

temp = np.int32(corners1)
img = np.copy(image1)
for i in range(0, len(corners1)):
    cv2.circle(img,(temp[i,0,0],temp[i,0,1]),3,255,-1)

plt.imshow(img, cmap = plt.get_cmap('gray'))
plt.show()<jupyter_output><empty_output><jupyter_text>On cherche le déplacement des coins.<jupyter_code>corners2, st, err = cv2.calcOpticalFlowPyrLK(image1, image2, corners1, None, winSize  = (15,15), maxLevel = 0, 
                                       criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))<jupyter_output><empty_output><jupyter_text>Affichage des déplacements. On obtient un vecteur de déplacement par coins.<jupyter_code>fig1 = plt.figure(figsize = (10,10))
temp1 = np.int32(corners1)
temp2 = np.int32(corners2)
img = np.copy(image1)
for i in range(0, len(corners1)):
    cv2.line(img, (temp1[i,0,0],temp1[i,0,1]),(temp2[i,0,0],temp1[i,0,1]), 255, 2)
plt.imshow(img, cmap = plt.get_cmap('gray'))
plt.show()<jupyter_output><empty_output><jupyter_text>Par la méthode de Farneback. Flot optique dense.<jupyter_code># Pour tous les pixels de l'image
flot = cv2.calcOpticalFlowFarneback(image1,image2, None, 0.5, 3, 15, 3, 5, 1.2, 0)<jupyter_output><empty_output><jupyter_text>Affichage du déplacement de tous les pixels de l'image. Donc, un vecteur de déplacement par pixel.<jupyter_code>fig2 = plt.figure(figsize = (10,10))
hsv = np.zeros((len(image1),len(image1[0]),3))
hsv[...,1] = 255
mag, ang = cv2.cartToPolar(flot[...,0], flot[...,1])
hsv[...,0] = ang*180/np.pi/2
hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
bgr = cv2.cvtColor(np.uint8(hsv),cv2.COLOR_HSV2BGR)
plt.imshow(bgr)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15219721329046088, 'fraction_numerical': 0.0646659521257592, 'mean_word_length': 6.017543859649122, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '415840', 'license': 'MIT', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1159, 'n_words': 201, 'path': 'OpticalFlow.ipynb', 'repo_name': 'gabilodeau/INF6804', 'size': '2799'}