Exploring Hough Transform with Floor Detection: My journey

The Process

1. Loading the Image

2. Blurring the Image

# Apply Gaussian Blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
cv2.imwrite('blurred.png', blurred)

3. Detecting Edges

What is the Canny Edge Detector?

# Perform Canny Edge Detection
edges = cv2.Canny(blurred, 50, 150)
cv2.imwrite('edges.png', edges)

4. Detect Lines with Hough Transform

lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100)

line_image = np.copy(image)
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(line_image, (x1, y1), (x2, y2), (0, 255, 0), 1)

cv2.imwrite('HoughLines.png', line_image)

5. Finding Where the Lines Intersect

def get_intersection(line1, line2):
    rho1, theta1 = line1
    rho2, theta2 = line2
    a1, b1 = np.cos(theta1), np.sin(theta1)
    a2, b2 = np.cos(theta2), np.sin(theta2)

    # Solve linear equations to find intersection
    determinant = a1 * b2 - a2 * b1
    if abs(determinant) < 1e-5:
        return None  # Lines are parallel

    x = (b2 * rho1 - b1 * rho2) / determinant
    y = (a1 * rho2 - a2 * rho1) / determinant
    return (x, y)

6. Scoring the Best Floor Boundary

def score_quadrilateral(quadrilateral, edges):
    score = 0
    for point in quadrilateral:
        x, y = point
        if 0 <= int(x) < edges.shape[1] and 0 <= int(y) < edges.shape[0]:
            score += edges[int(y), int(x)]
    return score

7. Displaying the Final Result

if best_quadrilateral is not None:
    best_quadrilateral = np.array(best_quadrilateral, dtype=np.int32)
    cv2.polylines(image, [best_quadrilateral], isClosed=True, color=(255, 0, 0), thickness=2)

cv2.imwrite('detected_floor.png', image)

The Challenge

Looking for Help

Leave a Reply

Your email address will not be published. Required fields are marked *