image.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 
3 import cv2
4 import numpy as np
5 
6 
7 def trova_contorno(t,m,edges_canny,file_name):
8 
9  '''
10  hough su canny, plotto le hough lines su un'immagine bianca, applico la funzione cv2.findContours con retrieval_mode = external per trovare i contorni esterni. Se i contorni esterni sono più di 1, devo riapplicare la hough aumentando la threshold, in modo da rendere unico il contorno esterno.
11  '''
12  hough_contorni = cv2.HoughLinesP(edges_canny,1,np.pi/180,threshold=t,minLineLength=1,maxLineGap=m)
13  if cv2.__version__[0] == '3' :
14  hough_contorni = [i[0]for i in hough_contorni]
15  elif cv2.__version__[0] == '2' :
16  hough_contorni = hough_contorni[0]
17  else :
18  raise EnvironmentError('Opencv Version Error. You should have OpenCv 2.* or 3.*')
19 
20  #creo immagine bianca su cui plottare le hough lines
21  img4 = cv2.imread(file_name,0)
22  ret,bianca = cv2.threshold(img4,255,255,cv2.THRESH_BINARY_INV)
23  bianca2 = bianca.copy()
24  #plotto le hough lines sull'immagine bianca
25  for x1,y1,x2,y2 in hough_contorni:
26  cv2.line(bianca2,(x1,y1),(x2,y2),(0,0,0),2)
27 
28  ret,bianca2 = cv2.threshold(bianca2,253,255,cv2.THRESH_BINARY_INV)
29  if cv2.__version__[0] == '3' :
30  img_copy, contours, hierarchy = cv2.findContours(image = bianca2.copy(), mode = cv2.RETR_EXTERNAL, method = cv2.CHAIN_APPROX_SIMPLE)
31  elif cv2.__version__[0] == '2' :
32  #trovo il contorno esterno
33  contours, hierarchy = cv2.findContours(bianca2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
34  else :
35  raise EnvironmentError('Opencv Version Error. You should have OpenCv 2.* or 3.*')
36 
37  while(len(contours)!=1): #se il contorno esterno trovato non è unico
38  t+=1 #aumento la threshold
39 
40  # COntrollo che le Hough funzionino bene compatibilmente alla versione di OpenCV
41  hough_contorni = cv2.HoughLinesP(edges_canny,1,np.pi/180,threshold=t,minLineLength=1,maxLineGap=m)
42  if cv2.__version__[0] == '3' :
43  hough_contorni = [i[0]for i in hough_contorni]
44  elif cv2.__version__[0] == '2' :
45  hough_contorni = hough_contorni[0]
46  else :
47  raise EnvironmentError('Opencv Version Error. You should have OpenCv 2.* or 3.*')
48 
49  bianca2 = bianca.copy()
50  for x1,y1,x2,y2 in hough_contorni:
51  cv2.line(bianca2,(x1,y1),(x2,y2),(0,0,0),2)
52  ret,bianca2 = cv2.threshold(bianca2,253,255,cv2.THRESH_BINARY_INV)
53 
54  #Controllo che findcontours funzioni bene con openCvDiverse
55  if cv2.__version__[0] == '3' :
56  img_copy, contours, hierarchy = cv2.findContours(image = bianca2.copy(), mode = cv2.RETR_EXTERNAL, method = cv2.CHAIN_APPROX_SIMPLE)
57  elif cv2.__version__[0] == '2' :
58  #trovo il contorno esterno
59  contours, hierarchy = cv2.findContours(bianca2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
60  else :
61  raise EnvironmentError('Opencv Version Error. You should have OpenCv 2.* or 3.*')
62  return (hough_contorni,contours)
image.trova_contorno
def trova_contorno(t, m, edges_canny, file_name)
Definition: image.py:7


rose2
Author(s): Gabriele Somaschini, Matteo Luperto
autogenerated on Wed Jun 28 2023 02:21:53