Removing Horizontal Lines in image (OpenCV, Python, Matplotlib)

Removing Horizontal Lines in image (OpenCV, Python, Matplotlib)

To remove horizontal lines from an image using OpenCV, Python, and Matplotlib, you can follow these steps:

  1. Import the required libraries.
  2. Read the image using OpenCV.
  3. Convert the image to grayscale.
  4. Apply image processing techniques to detect and remove horizontal lines.
  5. Display the modified image using Matplotlib.

Here's a sample code snippet to help you get started:

import cv2 import numpy as np import matplotlib.pyplot as plt # Read the image using OpenCV image_path = 'your_image_path.jpg' image = cv2.imread(image_path) # Convert the image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to reduce noise and enhance line detection blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Apply horizontal line detection using HoughLinesP min_line_length = 100 # Minimum length of the line to consider max_line_gap = 10 # Maximum gap between line segments to consider lines = cv2.HoughLinesP(blurred, 1, np.pi/180, threshold=50, minLineLength=min_line_length, maxLineGap=max_line_gap) # Create a mask to draw the lines on line_mask = np.zeros_like(image) # Draw detected lines on the mask if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(line_mask, (x1, y1), (x2, y2), (255, 255, 255), 2) # Draw white lines on the mask # Invert the mask line_mask_inverted = cv2.bitwise_not(line_mask) # Remove horizontal lines from the original image using the inverted mask image_without_lines = cv2.bitwise_and(image, image, mask=line_mask_inverted) # Display the original and modified images using Matplotlib plt.subplot(1, 2, 1) plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.title("Original Image") plt.subplot(1, 2, 2) plt.imshow(cv2.cvtColor(image_without_lines, cv2.COLOR_BGR2RGB)) plt.title("Image without Horizontal Lines") plt.show() 

In this code, we first read the image using OpenCV and convert it to grayscale. Then, we apply Gaussian blur to reduce noise and enhance the line detection process. We use the Hough Line Transform (cv2.HoughLinesP) to detect horizontal lines in the image. Detected lines are drawn on a mask, and we invert this mask. Finally, we use the inverted mask to remove horizontal lines from the original image using the cv2.bitwise_and operation.

Remember to replace 'your_image_path.jpg' with the actual path to your image file.

Examples

  1. How to remove horizontal lines from an image in OpenCV?

    • This query aims to remove horizontal lines from an image using OpenCV.
    import cv2 import numpy as np image = cv2.imread('image_with_lines.jpg', cv2.IMREAD_GRAYSCALE) # Use a kernel to detect horizontal lines kernel = np.ones((1, 15), np.uint8) eroded = cv2.erode(image, kernel, iterations=1) dilated = cv2.dilate(eroded, kernel, iterations=1) result = cv2.subtract(image, dilated) cv2.imwrite('image_without_lines.jpg', result) 
  2. Python: Removing horizontal lines from a scanned document using OpenCV

    • This query explores removing horizontal lines from a scanned document with OpenCV.
    import cv2 import numpy as np image = cv2.imread('scanned_doc.jpg', cv2.IMREAD_GRAYSCALE) # Threshold to get binary image _, binary = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY_INV) # Use a horizontal kernel to detect lines horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 1)) detected_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel) # Subtract the detected lines from the binary image image_without_lines = cv2.subtract(binary, detected_lines) cv2.imwrite('doc_without_lines.jpg', image_without_lines) 
  3. How to remove horizontal lines in an image using Python and OpenCV?

    • This query demonstrates a simple way to remove horizontal lines in an image with Python and OpenCV.
    import cv2 import numpy as np image = cv2.imread('horizontal_lines.jpg', cv2.IMREAD_GRAYSCALE) # Define a horizontal kernel kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 1)) morph = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) image_no_lines = cv2.subtract(image, morph) cv2.imwrite('result.jpg', image_no_lines) 
  4. Python OpenCV code to remove horizontal lines in an image

    • This query describes removing horizontal lines with code examples using OpenCV in Python.
    import cv2 import numpy as np image = cv2.imread('sample_image.jpg', cv2.IMREAD_GRAYSCALE) horizontal_kernel = np.ones((1, 100), np.uint8) # Detect horizontal lines horizontal_lines = cv2.morphologyEx(image, cv2.MORPH_OPEN, horizontal_kernel) # Subtract the horizontal lines from the original image image_clean = cv2.subtract(image, horizontal_lines) cv2.imwrite('cleaned_image.jpg', image_clean) 
  5. OpenCV: Removing horizontal lines from an image for text extraction

    • This query explores removing horizontal lines to enhance text extraction from an image.
    import cv2 import numpy as np image = cv2.imread('text_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply thresholding to get binary image _, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) # Detect and remove horizontal lines horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 1)) horizontal_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel) result = cv2.subtract(binary, horizontal_lines) # Save the result cv2.imwrite('text_no_lines.jpg', result) 
  6. Remove horizontal lines in an image using OpenCV and Matplotlib

    • This query describes removing horizontal lines and visualizing the result with Matplotlib.
    import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('image_with_lines.jpg', cv2.IMREAD_GRAYSCALE) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50, 1)) # Detect horizontal lines horizontal_lines = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) # Remove horizontal lines image_clean = cv2.subtract(image, horizontal_lines) # Show the original and cleaned image with Matplotlib plt.subplot(1, 2, 1) plt.title('Original Image') plt.imshow(image, cmap='gray') plt.subplot(1, 2, 2) plt.title('Image without Horizontal Lines') plt.imshow(image_clean, cmap='gray') plt.show() 
  7. Remove horizontal lines in OpenCV for table data extraction

    • This query examines removing horizontal lines to extract table data.
    import cv2 import numpy as np image = cv2.imread('table_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply threshold to create a binary image _, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) # Use a horizontal kernel to remove horizontal lines horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (60, 1)) horizontal_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel) image_clean = cv2.subtract(binary, horizontal_lines) cv2.imwrite('table_no_lines.jpg', image_clean) 
  8. Remove horizontal lines in OpenCV to process scanned diagrams

    • This query explores removing horizontal lines to process diagrams or technical drawings.
    import cv2 import numpy as np image = cv2.imread('diagram.jpg', cv2.IMREAD_GRAYSCALE) # Use a large horizontal kernel horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (100, 1)) horizontal_lines = cv2.morphologyEx(image, cv2.MORPH_OPEN, horizontal_kernel) cleaned_image = cv2.subtract(image, horizontal_lines) cv2.imwrite('cleaned_diagram.jpg', cleaned_image) 
  9. Removing horizontal lines from images for OCR using OpenCV

    • This query focuses on removing horizontal lines to improve Optical Character Recognition (OCR).
    import cv2 import numpy as np image = cv2.imread('ocr_image.jpg', cv2.IMREAD_GRAYSCALE) # Apply adaptive thresholding for binary image binary = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40, 1)) horizontal_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel) cleaned_image = cv2.subtract(binary, horizontal_lines) cv2.imwrite('ocr_cleaned.jpg', cleaned_image) 
  10. Remove horizontal lines in OpenCV to isolate graphical elements


More Tags

hindi laravel-artisan control-flow pandas mobile-browser backup fieldset ringtone emoji android-keypad

More Python Questions

More Transportation Calculators

More Internet Calculators

More Date and Time Calculators

More Mortgage and Real Estate Calculators