Nine3KiD
Nine3KiD

Reputation: 713

Simple images will detect but complex images wont get detected by HoughLine modified code

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,

enter image description here enter image description here

But when I use a complex(real) plan what I get output,

enter image description here enter image description here

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,

enter image description here

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!

Upvotes: 4

Views: 360

Answers (1)

Micka
Micka

Reputation: 20160

In your first version you have this part of code:

for rho,theta in lines[0]:
    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)

in your second version you just have

for line in lines:
    cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0), 3,8)

try to draw the lines in the same way you did in the first code sample. Since you are using the probabilistic HoughLine function, you need to compute the angle yourself.

One answer how to do that is given in How to calculate the angle between a line and the horizontal axis?

I'm not that experienced in python, but it should be something like:

for line in lines:
    dy = line[1].y - line[0].y
    dx = line[1].x - line[0].x
    angle = atan2(dy,dx) * 180/np.pi
    if(20 < angle < 88)
        cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0),3,8)
    if(160 > angle > 93)
        cv.Line(color_dst_proba, line[0], line[1], cv.CV_RGB(255, 0, 0),3,8)

If that doesnt work, try https://stackoverflow.com/a/32963819/2393191 to compute the angle.

Upvotes: 2

Related Questions