import numpy as np import cv2 import math print("version openCV:"+str( cv2.__version__ )) flags = [i for i in dir(cv2) if i.startswith('COLOR_')] print(flags) couleur=True if not couleur: # Load an color image in grayscale img = cv2.imread('img.jpg',cv2.IMREAD_GRAYSCALE) if img is None: print("probleme chargement image") exit(-1) print(img) print("img.shape:" +str(img.shape)) # Draw a diagonal blue line with thickness of 5 px cv2.line(img,(0,0),(img.shape[1]-1,img.shape[0]-1),255,5) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,'BUT3',(10,500), font, 4,255,2,cv2.LINE_AA ) cv2.imwrite('imggray.png',img) cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows() else: # Load an color image in grayscale img = cv2.imread('img.jpg',cv2.IMREAD_COLOR) if img is None: print("probleme chargement image") exit(-1) print(img) print("img.shape:" +str(img.shape)) # Draw a diagonal blue line with thickness of 5 px cv2.line(img,(0,0),(img.shape[1]-1,img.shape[0]-1),(0,0,255),5) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,'BUT3',(10,500), font, 4,(0,255,0),2,cv2.LINE_AA ) px = img[200,100] # v,u print("px:"+str(px)) img[200,100] = [255,0,255] for up in range(0,255): for vp in range(0,255): img[200+vp,100+up] = [up,0,vp] u0=692 v0=252 larg=60 haut=60 u1=434 v1=218 ROI=img[v0:v0+haut,u0:u0+larg] cv2.imwrite('roi.png',ROI) #copie directe #img[v1:v1+haut,u1:u1+larg]=ROI #copie pixel par pixel en faisant une symétrie par rapport à l'axe vertical (inefficace en python) for u in range(0,larg-1): for v in range(0,haut-1): img[v1+v,u1+u]=ROI[v,larg-u-1] for v1 in range(100,400,100): u1=int(v1*1.5)+220 img[v1:v1+haut,u1:u1+larg]=ROI cv2.imwrite('imgcolor.png',img) # Convert BGR to HSV hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # define range of green color in HSV lower_green = np.array([26,50,50]) upper_green = np.array([46,255,225]) # Threshold the HSV image to get only green colors mask = cv2.inRange(hsv, lower_green, upper_green) mask=cv2.bitwise_not(mask) # Bitwise-AND mask and original image res = cv2.bitwise_and(img,img, mask= mask) cv2.imshow('image',res) cv2.waitKey(0) cv2.destroyAllWindows()