October 22, 2024
Chicago 12, Melborne City, USA
python

Can anyone help me out in detection of Tile edges and Corners effectively?


Iam trying to detect Tile corners and edges but there is an issue of light reflection intensity as well as the shadows.Since iam using image processing technique so i want to sort it effectively like this and my code works on some images of tiles but doesnt work on many of them.
[Results on processing1](https://i.sstatic.net/YFhdHjdx.png)
Results on processing2(https://i.sstatic.net/82XqXEPT.png)
[Results on processing3](https://i.sstatic.net/2cPGR0M6.jpg)
I have shared my code below along with desired results i want to achieve like.

Code

# Import necessary libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os

# Function to calculate the distance between two points
def point_distance(x1, y1, x2, y2):
    return np.sqrt((x1 - x2)**2 + (y1 - y2)**2)

# Function to check if a point is on a line segment
def is_point_on_line(x, y, x1, y1, x2, y2, tolerance=10):
    dist1 = point_distance(x, y, x1, y1)
    dist2 = point_distance(x, y, x2, y2)
    line_length = point_distance(x1, y1, x2, y2)

    # Check if the sum of distances from the point to the two ends is approximately equal to the line length
    return abs(dist1 + dist2 - line_length) < tolerance

# Function to find intersections and ensure the point is on both line segments
def find_intersections(lines):
    intersections = []
    for i, line1 in enumerate(lines):
        for line2 in lines[i+1:]:
            x1, y1, x2, y2 = line1[0]
            x3, y3, x4, y4 = line2[0]

            # Calculate the intersection point using line equations
            denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
            if denom == 0:  # Lines are parallel
                continue
            intersect_x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
            intersect_y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom

            # Check if the intersection point is on both line segments
            if is_point_on_line(intersect_x, intersect_y, x1, y1, x2, y2) and \
               is_point_on_line(intersect_x, intersect_y, x3, y3, x4, y4):
                intersections.append((int(intersect_x), int(intersect_y)))

    return intersections

# Load images from the Tiles folder
tiles_folder = "hard"  # Adjust this path if necessary
output_folder = "Processed_Tiles"  # Name of the output folder
os.makedirs(output_folder, exist_ok=True)  # Create output folder if it doesn't exist
images = [f for f in os.listdir(tiles_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]

# Process each image
for image_filename in images:
    image_path = os.path.join(tiles_folder, image_filename)
    image = cv2.imread(image_path)

    # Convert image to RGB for visualization with matplotlib
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Apply Gaussian Blur to reduce noise and improve edge detection
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)

    # Use Canny edge detection
    edges = cv2.Canny(blurred, 50, 200)

    # Apply dilation to thicken edges for better line detection
    dilated = cv2.dilate(edges, None, iterations=1)

    # Detect lines using Hough Line Transform
    lines = cv2.HoughLinesP(dilated, 1, np.pi/180, threshold=100, minLineLength=50, maxLineGap=20)

    # Draw the detected lines on the image
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line[0]
            cv2.line(image_rgb, (x1, y1), (x2, y2), (0, 255, 0), 3)  # Thicker lines

        # Find intersections and draw them
        intersections = find_intersections(lines)
        for point in intersections:
            cv2.circle(image_rgb, point, 5, (255, 0, 0), -1)  # Larger and clearer circles for intersections

    # Convert image back to BGR format for saving
    image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)

    # Save the processed image to the output folder
    output_path = os.path.join(output_folder, f"processed_{image_filename}")
    cv2.imwrite(output_path, image_bgr)

    # Optionally display the result using matplotlib
    plt.figure(figsize=(10, 10))
    plt.imshow(image_rgb)
    plt.title(f'Detected Lines and Intersections in {image_filename}')
    plt.axis('off')
    plt.show()

print("Processing complete. Processed images saved in:", output_folder)

Desired Result:

Desired Output:



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video