MatPlotLib, OpenCV and PyQT codes added

This commit is contained in:
F. Y. H. 2022-07-25 05:50:30 +02:00
parent a2bb08c189
commit 955bf76f55
4 changed files with 233 additions and 0 deletions

107
MatPlotLib/aglomerat.py Normal file
View file

@ -0,0 +1,107 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 15 23:36:50 2017
@author: fh
Simulation d'aglomération de particules
"""
def matrix_rand(L,C,d=0.10):
from numpy import zeros
from time import sleep
from random import random as rand
M=zeros([L,C],dtype=bool)
for i in range(L):
for j in range(C):
r=rand()
v=r<d
M[i,j]=v
return M
def aglo(M):
from numpy import size,array,zeros
L=size(M[:,0])
C=size(M[0,:])
#Poids.
P=array([[[ 0, 0, 0, 0, 0],#Poids en ligne.
[ 0,-1, 0, 1, 0],
[-1,-2, 0, 2, 1],
[ 0,-1, 0, 1, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 1, 0, 0],#Poids en colonne.
[ 0, 1, 2, 1, 0],
[ 0, 0, 0, 0, 0],
[ 0,-1,-2,-1, 0],
[ 0, 0,-1, 0, 0]]],dtype=int)
#Fenêtre.
F=zeros([2,5,5],dtype=int)
for i in range(L):
for j in range(C):
#Indices périodiques.
SSl=(i-2)%L
Sl=(i-1)%L
l=i%L
Nl=(i+1)%L
NNl=(i+2)%L
OOc=(j-2)%C
Oc=(j-1)%C
c=j%C
Ec=(j+1)%C
EEc=(j+2)%C
#Fenêtre.
#Ligne à l'ouest.
F[0,1,1]=P[0,1,1]*M[Nl ,Oc ]
F[0,2,0]=P[0,2,0]*M[l ,OOc]
F[0,2,1]=P[0,2,1]*M[l ,Oc ]
F[0,3,1]=P[0,3,1]*M[Sl ,Oc ]
#Ligne à l'est.
F[0,1,3]=P[0,1,3]*M[Nl ,Ec ]
F[0,2,3]=P[0,2,3]*M[l ,Ec ]
F[0,2,4]=P[0,2,4]*M[l ,EEc]
F[0,3,3]=P[0,3,3]*M[Sl ,Ec ]
#Colonne au nord.
F[1,0,2]=P[1,0,2]*M[NNl,c ]
F[1,1,1]=P[1,1,1]*M[Nl ,Oc ]
F[1,1,2]=P[1,1,2]*M[Nl ,c ]
F[1,1,3]=P[1,1,3]*M[Nl ,Ec ]
#Colonne au sud.
F[1,3,1]=P[1,3,1]*M[Sl ,Oc ]
F[1,3,2]=P[1,3,2]*M[Sl ,c ]
F[1,3,3]=P[1,3,3]*M[Sl ,Ec ]
F[1,4,2]=P[1,4,2]*M[SSl,c ]
#Force verticale.
Fy=F[0].sum()
#Force horizontale.
Fx=F[1].sum()
if abs(Fy)>abs(Fx) and Fy>0 and (not M[Nl,c ]):#Au nord.
#Centre->Nord.
M[l,c],M[Nl,c ]=False,True
elif abs(Fy)>abs(Fx) and Fy<0 and (not M[Sl,c ]):#Au sud.
#Centre->Sud.
M[l,c],M[Sl,c ]=False,True
elif abs(Fx)>abs(Fy) and Fx>0 and (not M[l ,Ec]):#A l'est.
#Centre->Est.
M[l,c],M[l ,Ec]=False,True
elif abs(Fx)>abs(Fy) and Fx<0 and (not M[l ,Oc]):#A l'ouest.
#Centre->Ouest.
M[l,c],M[l ,Oc]=False,True
return M
def aglo_test():
M=matrix_rand(20,20)
M1=M
for t in range(1):
M1=aglo(M1)
print(M*1)
print(M1*1)
print("Sommes :",M.sum()," et ",M1.sum(),".")
print("M=M1 :\n",(M==M1)*1)
print("Densité : ",M.sum()/M.size)
from matplotlib.pyplot import imshow,figure,pause,title
figure("Graphe")
for t in range(31):
M=matrix_rand(200,200,0.8)
imshow(M,cmap='gray',interpolation='nearest')
ttr="Itération :{:d}".format(t)
ttr+="\nDensité : {:03.1f}%".format(M.sum()*100/M.size)
title(ttr)
pause(0.99)

View file

@ -0,0 +1,16 @@
#!/env/bin python
# -*- coding: utf-8 -*-
"""
Éditeur de Spyder
Script pour faire une nuance de gris sinusoïdeale.
"""
from numpy import array,linspace,sin,ones,pi
import matplotlib.pyplot as plt
import matplotlib.colors as clr
image=ones([1000,1000])
for x in range(1,1000):
image[x][:]=sin(x*2*pi/100)*ones([1000])
plt.figure()
plt.imshow(image,cmap='gray')
plt.title("Sinusoïde de NdG")

View file

@ -0,0 +1,24 @@
#!/bin/env python3
# *-* utf-8 *-*
import numpy as np
import matplotlib.pyplot as plt
import cv2
import time
# Code principal.
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
[xl, yl, cl] = np.shape(frame)
red = np.ones([xl, yl, cl])
green = np.ones([xl, yl, cl])
blue = np.ones([xl, yl, cl])
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray],[0],None,[256],[0,256])
plt.plot(np.linspace(0,255,256),hist.T[0])
plt.imshow('video', hist)
if (cv2.waitKey(1) & 0xFF == ord('q')) :
break
time.sleep(1)
cap.release()
cv2.destroyAllWindows()

86
PyQT/bidule.py Normal file
View file

@ -0,0 +1,86 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 6 14:02:21 2017
@author: fh
"""
import numpy as np
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QGridLayout, QLabel, QLineEdit, QCheckBox, QGroupBox
from PyQt5.QtCore import Qt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
class Bidule(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setWindowTitle("Le bidule")#Titre de boîte
self.resize(250,250)#Taille de boîte
self.boite=QVBoxLayout()#Boîte verticale
self.fig = Figure()#Créer une figure.
self.axes1 = self.fig.add_subplot(121)#Figure1
self.axes2=self.fig.add_subplot(122)#Figure2
self.x1=np.linspace(-np.pi,np.pi)
self.x2=np.linspace(1,100)
self.y1=np.cos(self.x1)**2-np.sin(self.x1)
self.y2=np.exp(-self.x2**2)
self.line1,=self.axes1.plot(self.x1,self.y1)
self.line2,=self.axes2.plot(self.x2,self.y2)
self.canvas=FigureCanvas(self.fig)#Toile portant la figure de Matplotlib.
self.layout.addWidget(self.canvas)
self.setLayout(self.boite)#Placer boîte
self.soustitre=QLabel("Boîte à bouttons")#Sous-titre de sous-boîte
self.soustitre.setMaximumSize(200,15)#Taille de sous-titre
self.boite.addWidget(self.soustitre)#Placer sous-titre
self.sousboite=QGridLayout()#Sous-boîte en matrice pour les bouttons
self.boite.addLayout(self.sousboite)#Placer sous-boîte
self.bouttons=[]#Liste des bouttons vide
self.numeros=[]#Liste des numéros vide
self.appuyer=[]
for l in range(5):
self.bouttons+=[[]]#Lignes de bouttons
self.numeros+=[[]]#Lignes de numéros
self.appuyer+=[[]]
for c in range(5):
self.bouttons[l]+=[QPushButton()]#Colonnes de bouttons
self.numeros[l]+=["{}{}".format(l,c)]#Colonnes de numéros
self.appuyer[l]+=[np.sin]
self.bouttons[l][c].setMaximumSize(30,30)#Taille des bouttons
self.bouttons[l][c].setText(self.numeros[l][c])#Numéros des bouttons
self.sousboite.addWidget(self.bouttons[l][c],l,c)#Placer tous les bouttons
def appui():
if self.bouttons[l][c].clicked!=QPushButton.clicked:
if self.numeros[l][c] == self.bouttons[l][c].text():
self.bouttons[l][c].setText("["+self.numeros[l][c]+"]")
else:
self.bouttons[l][c].setText(self.numeros[l][c])
self.appuyer[l][c]=appui
self.bouttons[l][c].clicked.connect(self.appuyer[l][c])#Connection entre bouttons et "appuyer"
self.show()#Afficher.
'''
def appuyer(self):
n=self.numeros
t=[]
for l in range(len(self.bouttons)):
t+=[[]]
for c in range(len(self.bouttons[0])):
t[l]+=[self.bouttons[l][c].text()]
for l in range(len(self.bouttons)):
for c in range(len(self.bouttons[0])):
if self.bouttons[l][c].clicked==QPushButton.clicked:
if n[l][c]==t[l][c]:
self.bouttons[l][c].setText("["+n[l][c]+"]")
else:
self.bouttons[l][c].setText(n[l][c])
'''
def ouvrirBidule():
app = QApplication.instance()
if not app:
app = QApplication(sys.argv)
objet = Bidule()
objet.show()
app.exec_()
ouvrirBidule()