pense-bête de bruno sanchiz

Accueil > Programmation > python > opencv

opencv

Publié le 19 mars 2023, dernière mise-à-jour le 19 mars 2023, 3 visites, 23644 visites totales.

imports

import cv2
import numpy as np
import matplotlib.pyplot as plt 
import matplotlib

image

img = cv2.imread(’a.jpg’)
cv2.imshow("kk",img)
cv2.waitKey(0)
print(image.shape) #hauteur,largeur,canaux

  • dessiner une ligne :
    img = cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
    plt.imshow(image_line[ :, :, : :-1])
  • dessiner un cercle
    img = cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
  • dessiner un rectangle
    img = cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
  • insérer un texte
    img = cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

functions sur images :

  • ajouter : cv2.add(im1,im2)
  • soustraire : cv2.substract(im1,im2)

RVB <-> BVR

B,V,R =img[x,y]
R,V,B=img[ :, :, : :-1] [x,y]

crop

cropped_region = img[y1:y2,x1:x2]

changement de taille, resize

cv2.resize(img, None, fx = 2, fy = 2)
cv2.resize(img, dsize = (200,400), interpolation = cv2.INTER_AREA)

flip

<-> : cv2.flip(img_eagle, 1)
vert : cv2.flip(img_eagle, 0)
rot 180 : cv2.flip(img_eagle, -1)

aruco detecteurs

for i in range(1000):
	dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_1000)
	parameters =  cv2.aruco.DetectorParameters()
	img=cv2.aruco.generateImageMarker(dictionary,i,200)
	cv2.imwrite("/tmp/aa/f"+str(i)+".jpg",img)
[bruno sanchiz]