def bande_seule(im_flash, s_sombre, s_bande): #s_bande=95 c bon im_res = sans_sombre(im_flash, s_sombre) im_res = myutil.myseuil(im_res, s_bande) el_ferm = strel.build('carre', 6) el_recon = strel.build('diamant', 1) im_res = morpho.fermeture_recon(im_res, el_ferm, el_recon) return im_res
def nettoyageImage(image, aireZoneMax, largeurMax): # On transforme l'image en niveau de gris imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # On transforme l'image en binaire en appliquant un threshold ret, thresh = cv2.threshold(imgray, 127, 255, cv2.THRESH_BINARY) # On recupere les composantes connexes de notre image (technique de labelisation) output = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S) # On recupere les labels de nos zones labels = output[1] # On recupere les statistiques associees a chaque zone stats = output[2] imgNettoyee = image taille = image.shape # gestion de la largeur elementOuverture = strel.build('carre', largeurMax, None) elementReconstruction = strel.build('carre', largeurMax, None) imgNettoyee = utils.ouvertureReconstruction(imgNettoyee, elementOuverture, elementReconstruction) imgNettoyee = utils.fermetureReconstruction(imgNettoyee, elementOuverture, elementReconstruction) # On itere sur nos pixels # Pour chaque pixel, on recupere la zone associee afin de connaitre l'aire et la largeur de la bounding box # Si c'est trop petit, alors on remplace la couleur par une couleur precedente for x in range(0, taille[0]): for y in range(0, taille[1]): zone = labels[x][y] if stats[zone, cv2.CC_STAT_AREA] < aireZoneMax or stats[zone, cv2.cv2.CC_STAT_WIDTH] < largeurMax: imgNettoyee[x, y] = imgNettoyee[x - 5, y-5] return imgNettoyee
def traceContour(imgInitiale, imgNettoyee): # On applique un gradient avec un gamma8 structElement = strel.build('diamant', 1, 0) imageNiveauGris = cv2.cvtColor(imgNettoyee, cv2.COLOR_BGR2GRAY) imgAvecGradient = utils.gradient(imageNiveauGris, structElement) # Puis on applique un seuil seuil = 1 imgSeuilee = utils.appliquerSeuil(imgAvecGradient, seuil) # On reconstitue une image couleur a partir de l'image en niveau de gris imgContour = imgInitiale index = imgSeuilee[:] == 255 imgContour[:] = (0, 0, 0) imgContour[index] = (255, 255, 255) elementOuverture = strel.build('disque', 1, None) elementReconstruction = strel.build('carre', 1, None) imgContour = utils.ouvertureReconstruction(imgContour, elementOuverture, elementReconstruction) imgContour = utils.fermetureReconstruction(imgContour, elementOuverture, elementReconstruction) return imgContour
cv2.THRESH_BINARY) #choice of threshold for the black zone ret_gray, t_gray = cv2.threshold( im_gray_area, 127, 255, cv2.THRESH_BINARY) #choice of threshold for the gray zone ret_white, t_white = cv2.threshold( im_white_area, 127, 255, cv2.THRESH_BINARY) #choice of threshold for the white zone cv2.imshow('t_black', t_black) #image display #cv2.imwrite('3_foraging_thresh_black_aera.jpg',t_black)#save image cv2.imshow('t_gray', t_gray) #image display #cv2.imwrite('4_foraging_thresh_gray_aera.jpg',t_gray)#save image cv2.imshow('t_write', t_white) #image display #cv2.imwrite('5_foraging_thresh_write_aera.jpg',t_white)#save image #construction of a structuring element intended for the gray area el = strel.build('carre', 1, 0) op = morpho.myopen(t_gray, el) #contour selection contours, hierarchy = cv2.findContours( t_black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #selection of contours for the black area contours_1, hierarchy_1 = cv2.findContours( op, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #selection of contours for the gray area contours_2, hierarchy_2 = cv2.findContours( t_white, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #selection of contours for the white area print("shapes found : ", len(contours)) #print the numbers of outlines #it is necessary to filter the contours via a parameter,
def ajouterIndicationCouleursZoneErosionSuccessives(imgContour, imgNettoyee, rayonDisqueCouleur): taille = imgNettoyee.shape gamma8 = strel.build('carre', 1, None) imgFinale = np.zeros(taille, np.float) imgFinale = np.uint8(imgFinale) imgFinale = imgFinale + imgContour imgFinale += imgContour structElementCercle = strel.build_as_list('disque', rayonDisqueCouleur, None) m = np.copy(imgContour) imgContour[0, :] = 0 imgContour[m.shape[0] - 1, :] = 0 imgContour[:, 0] = 0 imgContour[:, m.shape[1] - 1] = 0 # Iteration sur les pixels de imgContour for x in range(0, taille[0]): for y in range(0, taille[1]): if np.any(imgContour[x, y]) != 0: # On cherche a isoler la zone m[:] = 0 m[x, y] = 255 imgReconInf = utils.reconstructionInferieure(imgContour, m, gamma8) # Erosion successive jusqu'a faire disparaitre la zone continuer = True i = 1 while continuer: structElement = strel.build('disque', i, None) imgErodee = utils.erode(imgReconInf, structElement) # Quand la zone a disparu, alors on prend l'erosion successive precedente afin de placer notre cercle de couleur if np.amax(imgErodee) == 0: structElement = strel.build('disque', i - 1, None) imgErodee = utils.erode(imgReconInf, structElement) # On reboucle afin de chercher un pixel de la zone isolee restant apres les erosions successives for x in range(0, taille[0]): for y in range(0, taille[1]): if np.any(imgErodee[x, y]) != 0: pixel = (x, y) break # Boucle permettant de creer notre cercle de couleur a partir de l'element structurant for (i, j) in structElementCercle: if pixel[0] < (taille[1] - rayonDisqueCouleur) and pixel[1] < (taille[0] - rayonDisqueCouleur): imgFinale[pixel[0] + i, j + pixel[1]] = imgNettoyee[pixel[0], pixel[1]] continuer = False i = i + 1 # on enleve la zone isolee de image contour afin de ne pas traiter plusieurs fois les zones imgContour = imgContour - imgReconInf return imgFinale
#mask 1 & 2 obj = cv2.imread('mask1.jpg', 0) cv2.imshow('mask1', obj) # display cv2.imwrite('3_watershed.jpg', obj) #save image comp = cv2.imread('mask2.jpg', 0) cv2.imshow('mask2', comp) # display cv2.imwrite('4_watershed.jpg', comp) #save image r = np.zeros(im.shape, im.dtype) r = r + np.uint8(obj / 255 * 10) + np.uint8(comp / 255 * 20) cv2.imshow('r', r) # display cv2.imwrite('5_watershed.jpg', r) #save image #structural element g8 = strel.build('carre', 1) grad = morpho.mygrad(im, g8) cv2.imshow('grad', r) # display cv2.imwrite('6_watershed.jpg', grad) #save image #list with 256 lists inside l = [[] for i in range(0, 256)] #add at list #li.append #soustraction of a element of the list #li.pop(0) #indice of the element to soustract #count of the number of elements in all lists of l nb_el_liste = 0
#creation of a table that will create an empty image #it has the same shape as the original image tab = np.ones(im.shape, np.uint8) * 255 cv2.imshow('tab', tab) cv2.imwrite('1_Detct_black_aera.jpg', tab) #loading duplicate gray tint image im_2 = cv2.imread('image_2.jpg', 0) """ #rectangle black #el = strel.build('carre',2,0) #triangle black """ #creation of a structuraling element el = strel.build('diamant', 2, 0) #use of the morphological gradient technique grad = morpho.mygrad(im, el) cv2.imshow('grad', grad) cv2.imwrite('2_Detct_black_aera.jpg', grad) grad3 = morpho.mygrad(im, el) grad4 = grad3 * 10 cv2.imshow('grad4', grad4) cv2.imwrite('3_Detct_black_aera.jpg', grad4) ##the idea is to filter the pixels and their data a new value if they are included in ranges of values. for i in range(0, im_2.shape[0]): for j in range(0, im_2.shape[1]): if (grad[i, j] >= 0) & (grad[i, j] < 10):