OpenCV and PyQT codes added

This commit is contained in:
F. Y. H. 2022-07-25 09:21:49 +02:00
parent 955bf76f55
commit 835c2dc55f
5 changed files with 207 additions and 0 deletions

43
OpenCV/OpenCV.py Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 18:20:31 2019
@author: fh
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
L=np.size(frame[:,0,0])
C=np.size(frame[0,:,0])
col=np.size(frame[0,0,:])
print("Lignes : ",L)
print("Colonnes : ",C)
print("Canaux : ",col)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
sobel = cv2.Sobel(gray,cv2.CV_8U,1,0,ksize=3)
hist = cv2.calcHist([gray],[0],None,[256],[0,256])
# Display the resulting frame
cv2.imshow('Coloré',frame)
cv2.imshow('Gris',gray)
cv2.imshow("Sobel",sobel)
cv2.destroyAllWindows()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
print("Lignes : ",np.size(hist[:,0]))
print("Colonnes : ",np.size(hist[0,:]))
print("Canaux : ",np.size(hist[0,0]))
plt.plot(np.linspace(10,255,246),hist[10:,0])
plt.title("Histogramme total\nTaille d'image : {}x{}".format(L,C))
plt.xlim([10,255])
plt.show()

64
OpenCV/rgbgrey.py Normal file
View file

@ -0,0 +1,64 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 15:46:23 2019
@author: fh
"""
import numpy as np
import matplotlib.pyplot as plt
import cv2
print("Que la fête commence !")
# Capture vidéo.
cap = cv2.VideoCapture(0)
plt.figure("Fenetre", figsize=[20,15], facecolor='grey')
# Début de boucle des images.
for i in range(20) :
if not cap.isOpened():
print("Erreur : N'a trouvé ni caméra, ni fichier vidéo.")
break
# Code de sortie et images de caméra.
ret, frame = cap.read()
if not ret :
print("Erreur : On ne peut pas capturer d'images.")
# Image convertie en NdG. Une moyenne des couleurs?
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Affichage
# Gris.
plt.subplot(221)
plt.title("Gris")
plt.ylabel("X")
plt.xlabel("Y")
plt.imshow(gray, cmap='Greys_r')
# Cyan
plt.subplot(222)
plt.title("Cyan")
plt.ylabel("X")
plt.xlabel("Y")
plt.imshow(frame[:,:,0], cmap='Greys_r')
# Vert
plt.subplot(223)
plt.title("Vert")
plt.ylabel("X")
plt.xlabel("Y")
plt.imshow(frame[:,:,1], cmap='Greys_r')
# Magenta
plt.subplot(224)
plt.title("Magenta")
plt.ylabel("X")
plt.xlabel("Y")
plt.imshow(frame[:,:,2], cmap='Greys_r')
# Pause.
plt.pause(0.05)
plt.clf()
# Attente d'une touche pour terminer.
if plt.waitforbuttonpress(timeout=0.1) :
break
if cv2.waitKey(1) & 0xFF == ord('q') :
break
# Fin de capture.
plt.close('all')
cap.release()
# Fin des fenêtres.
cv2.destroyAllWindows()

30
OpenCV/videoModel.py Normal file
View file

@ -0,0 +1,30 @@
#!/bin/env python3
# *-* utf-8 *-*
import numpy as np
import cv2
import time
# Code principal.
ttime = 0.0
cap = cv2.VideoCapture(0)
while(True):
t0 = time.process_time()
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
x=np.shape(frame)[0]
y=np.shape(frame)[1]
c=np.shape(frame)[2]
txt0 = "Dims : {0}x{1}x{2}".format(x,y,c)
txt1 = "Temps : {0}s".format(ttime)
txt2 = "Date : " + time.strftime("%H:%M\'%S\"")
font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
cv2.putText(gray, txt0, (10,410), font, 1,(255,255,255) ,2 ,cv2.LINE_AA)
cv2.putText(gray, txt1, (10,440), font, 1,(255,255,255) ,2 ,cv2.LINE_AA)
cv2.putText(gray, txt2, (10,470), font, 1,(255,255,255) ,2 ,cv2.LINE_AA)
cv2.imshow('video', gray)
if (cv2.waitKey(1) & 0xFF == ord('q')) :
break
t1 = time.process_time()
ttime = t1 - t0
time.sleep(1.0)
cap.release()
cv2.destroyAllWindows()

36
OpenCV/videoSeuil.py Normal file
View file

@ -0,0 +1,36 @@
#!/bin/env python3
# *-* utf-8 *-*
import numpy as np
import cv2
# Fonction de seuillage.
def seuilRouge(gris, seuil=127):
x=np.shape(frame)[0]
y=np.shape(frame)[1]
rouge = np.zeros([x,y,3])
for i in range(0,x) :
for j in range(0,y) :
if not gris[i][j]<=seuil :
rouge[i][j][2] = 200
return rouge
def seuilAlt(gris, seuil=127):
rouge, arbre = cv2.findContours(gris, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return rouge
# Code principal.
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
x=np.shape(frame)[0]
y=np.shape(frame)[1]
c=np.shape(frame)[2]
red = seuilAlt(gray,24)
txt0 = "Dims : {0}x{1}x{2}".format(x,y,c)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(gray, txt0, (10,450), font, 1,(255,255,255) ,2 ,cv2.LINE_AA)
cv2.drawContours(gray, red, -1, [0,0,200])
cv2.imshow('video', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

34
PyQT/core.py Normal file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 15:25:01 2018
@author: fh
"""
from PyQt5.QtWidgets import QWidget, QApplication, QGridLayout, QPushButton
class Fenestre(QWidget):# Fenêtre finale affichée.
def __init__(self):# Constructeur.
QWidget.__init__(self)# Import des propriétés de QWidget.
self.setWindowTitle("Cases Voisines")# Insertion de titre de fenêtre.
self.resize(200, 200)#Taille de la fenêtre : x et y.
self.layout = QGridLayout()# Grille de bouttons.
self.layout.setColumnStretch(80, 80)
self.layout.setRowStretch(80, 80)
for i in range(5):# Par lignes.
for j in range(5):# Par colonnes.
self.buttonCase = QPushButton("x")# Nouveau boutton.
self.buttonCase.setMaximumSize(40, 40)# Dimensions du boutton.
self.layout.addWidget(self.buttonCase, i, j)# Insertion du boutton.
self.setLayout(self.layout)# Insertion de la grille.
self.showNormal()# Afficher.
def execution():# Exécuter une fois.
import sys
app = QApplication.instance()
if not app:
app = QApplication(sys.argv)
fen = Fenestre()
fen.show()
app.exec_()