giovedì 15 febbraio 2018

Esperimenti con filtri Canny ed Hough Lines con OpenCV







----------------------------------------------------------------------------------------------





----------------------------------------------------------------------------------------------
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('casa.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# parametri automatici Canny
#ret,th = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#edges = cv2.Canny(img,ret,ret*0.5)

v = np.median(gray)
sigma = 0.3
lower = int(max(0,(1.0-sigma)*v))
upper = int(max(0,(1.0+sigma)*v))

print lower
print upper

edges = cv2.Canny(img,lower,upper)

cv2.imwrite('canny_casa.jpg',edges)


lines = cv2.HoughLines(edges,1.5,np.pi/180,370)
for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))

    cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)

cv2.imwrite('risultato_casa.jpg',img)



Nessun commento:

Posta un commento