0

I am trying to remove 5 lines from my image. I tried using the Hough's Transform but none of the results focuses on the lines its always cleaning something on the image. I am quite a novice at image processing. So you could point me to the right direction.

image with 5 lines

this is my code

import cv2
import numpy as np

# Read in image, grayscale, and Otsu's threshold
image = cv2.imread('C:/Users/User/Downloads/WhatsApp Image 2023-07-17 at 7.17.09 PM.jpeg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Create diagonal kernel
kernel = np.array([[0, 0, 1],
                   [0, 1, 0],
                   [1, 0, 0]], dtype=np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)

# Find contours and filter using contour area to remove noise
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area < 500:
        cv2.drawContours(opening, [c], -1, (0, 0, 0), -1)

# Apply Hough Transform to detect lines
lines = cv2.HoughLines(opening, 1, np.pi / 180, threshold=1000)

# Convert lines to white pixels in the image
if lines is not None:
    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(image, (x1, y1), (x2, y2), (255, 255, 255), 2)

# Apply median filter to the image
median_filtered = cv2.medianBlur(image, 5)

# Display the images
cv2.imshow('Original Image', image)
cv2.imshow('Median Filtered Image', median_filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()

Resulting image: Result

Broken_Window
  • 2,037
  • 3
  • 21
  • 47

0 Answers0