I'm working on a project which I needed to detect the roof.For the moment, I'm detecting the angled lines. After some attempt, I was able to come up with a solution to detect a roof. But when I'm testing it with various type of roof plane(complex one) it isn't accurate to do the detection.
This is the code which I used,
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('frontElevation.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,80,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,100)
count =0
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))
plt.scatter(x1, y1)
plt.scatter(x2, y2)
if( 20 < 180*theta/np.pi < 88):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)
if (160 > 180 * theta / np.pi > 93):
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255),3)
plt.scatter(x1, y1)
plt.scatter(x2, y2)
cv2.imwrite('detectedFront.jpg',img)
This is the simple roof plan i used and the result obtained for that,
But when I use a complex(real) plan what I get output,
I tried to another code and that gave me a pleasant output. Below I've attached the code and output.
import cv2.cv as cv
import numpy as np
import math
im=cv.LoadImage('h1.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)
pi = math.pi #Pi value
dst = cv.CreateImage(cv.GetSize(im), 8, 1)
cv.Canny(im, dst, 200, 200)
cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY)
#---- Probabilistic ----
color_dst_proba = cv.CreateImage(cv.GetSize(im), 8, 3)
cv.CvtColor(im, color_dst_proba, cv.CV_GRAY2BGR) # idem
rho=1
theta=pi/180
thresh = 100
minLength= 120 # Values can be changed approximately to fit your image edges
maxGap= 50
lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_PROBABILISTIC, rho, theta, thresh, minLength, maxGap)
for line in lines:
cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 3, 8)
cv.ShowImage("Hough Probabilistic", color_dst_proba)
cv.WaitKey(0)
cv.SaveImage("output.jpg",color_dst_proba)
Result,
This has horizontal and vertical lines too. I need only the angled lines for my purpose. Can anyone help me on this issue? Thank you in advance!