python - Masking horizontal and vertical lines with Open CV

Python - Masking horizontal and vertical lines with Open CV

If you want to mask horizontal and vertical lines in an image using OpenCV, you can use morphological operations. Here's an example that demonstrates how to achieve this:

import cv2 import numpy as np def mask_lines(image_path): # Read the image image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Apply GaussianBlur to reduce noise blurred = cv2.GaussianBlur(image, (5, 5), 0) # Apply adaptive thresholding to obtain a binary image _, thresholded = cv2.threshold(blurred, 128, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # Create a horizontal kernel for morphological operations horizontal_kernel = np.ones((1, 50), np.uint8) # Apply morphological operations to detect horizontal lines horizontal_lines = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, horizontal_kernel, iterations=2) horizontal_mask = cv2.dilate(horizontal_lines, np.ones((1, 10), np.uint8), iterations=2) # Create a vertical kernel for morphological operations vertical_kernel = np.ones((50, 1), np.uint8) # Apply morphological operations to detect vertical lines vertical_lines = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, vertical_kernel, iterations=2) vertical_mask = cv2.dilate(vertical_lines, np.ones((10, 1), np.uint8), iterations=2) # Combine the horizontal and vertical masks combined_mask = cv2.bitwise_or(horizontal_mask, vertical_mask) # Apply the mask to the original image result = cv2.bitwise_and(image, image, mask=combined_mask) # Display the results cv2.imshow('Original Image', image) cv2.imshow('Combined Mask', combined_mask) cv2.imshow('Result', result) cv2.waitKey(0) cv2.destroyAllWindows() # Example usage image_path = 'path/to/your/image.jpg' mask_lines(image_path) 

In this example:

  1. The image is read and converted to grayscale.
  2. Gaussian blur is applied to reduce noise.
  3. Adaptive thresholding is used to obtain a binary image.
  4. Two morphological operations (openings) are performed with horizontal and vertical kernels to detect horizontal and vertical lines separately.
  5. Dilations are applied to enhance the detected lines.
  6. Horizontal and vertical masks are combined using the bitwise OR operation.
  7. The combined mask is applied to the original image.

Replace 'path/to/your/image.jpg' with the actual path to your image file. This example uses a simple approach and might need adjustments based on your specific use case and the characteristics of your images.

Examples

  1. How to mask horizontal lines in an image using OpenCV in Python?

    import cv2 import numpy as np def mask_horizontal_lines(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) kernel = np.ones((1, 5), np.uint8) mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) result = cv2.bitwise_and(image, image, mask=mask) return result 
  2. Python code to mask vertical lines in an image using OpenCV.

    import cv2 import numpy as np def mask_vertical_lines(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) kernel = np.ones((5, 1), np.uint8) mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) result = cv2.bitwise_and(image, image, mask=mask) return result 
  3. OpenCV code to mask both horizontal and vertical lines in an image.

    import cv2 import numpy as np def mask_horizontal_and_vertical_lines(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) # Mask horizontal lines horizontal_kernel = np.ones((1, 5), np.uint8) horizontal_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel) # Mask vertical lines vertical_kernel = np.ones((5, 1), np.uint8) vertical_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel) # Combine masks mask = cv2.bitwise_or(horizontal_mask, vertical_mask) result = cv2.bitwise_and(image, image, mask=mask) return result 
  4. Python script to detect and mask horizontal lines in OpenCV.

    import cv2 import numpy as np def detect_and_mask_horizontal_lines(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10) mask = np.zeros_like(gray) for line in lines: x1, y1, x2, y2 = line[0] cv2.line(mask, (x1, y1), (x2, y2), 255, 2) result = cv2.bitwise_and(image, image, mask=mask) return result 
  5. OpenCV code to identify and mask vertical lines in an image.

    import cv2 import numpy as np def detect_and_mask_vertical_lines(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10) mask = np.zeros_like(gray) for line in lines: x1, y1, x2, y2 = line[0] cv2.line(mask, (x1, y1), (x2, y2), 255, 2) result = cv2.bitwise_and(image, image, mask=mask) return result 
  6. Python code to mask lines in an image with specified orientation using OpenCV.

    import cv2 import numpy as np def mask_lines_with_orientation(image, orientation='horizontal'): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY) if orientation == 'horizontal': kernel = np.ones((1, 5), np.uint8) elif orientation == 'vertical': kernel = np.ones((5, 1), np.uint8) else: raise ValueError("Invalid orientation. Choose 'horizontal' or 'vertical'.") mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) result = cv2.bitwise_and(image, image, mask=mask) return result 
  7. OpenCV code to mask lines with specific angle range in an image.

    import cv2 import numpy as np def mask_lines_with_angle_range(image, angle_range=(0, 45)): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=100) mask = np.zeros_like(gray) for line in lines: rho, theta = line[0] angle = np.degrees(theta) if angle_range[0] <= angle <= angle_range[1]: a, b = np.cos(theta), np.sin(theta) x0, y0 = a * rho, b * rho x1, y1 = int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)) x2, y2 = int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)) cv2.line(mask, (x1, y1), (x2, y2), 255, 2) result = cv2.bitwise_and(image, image, mask=mask) return result 
  8. Python code to mask lines based on their length in an image using OpenCV.

    import cv2 import numpy as np def mask_lines_by_length(image, min_length=100): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=min_length, maxLineGap=10) mask = np.zeros_like(gray) for line in lines: x1, y1, x2, y2 = line[0] cv2.line(mask, (x1, y1), (x2, y2), 255, 2) result = cv2.bitwise_and(image, image, mask=mask) return result 
  9. OpenCV code to mask lines with specific color range in an image.

    import cv2 import numpy as np def mask_lines_with_color_range(image, lower_color=(0, 0, 0), upper_color=(255, 255, 255)): hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, lower_color, upper_color) result = cv2.bitwise_and(image, image, mask=mask) return result 
  10. Python script to mask lines based on their slope in an image using OpenCV.

    import cv2 import numpy as np def mask_lines_by_slope(image, min_slope=0.5, max_slope=2.0): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10) mask = np.zeros_like(gray) for line in lines: x1, y1, x2, y2 = line[0] if x1 != x2: slope = abs((y2 - y1) / (x2 - x1)) if min_slope <= slope <= max_slope: cv2.line(mask, (x1, y1), (x2, y2), 255, 2) result = cv2.bitwise_and(image, image, mask=mask) return result 

More Tags

android-uiautomator uicontrolstate gridpanel printstacktrace phpunit spotify php-5.5 supertest mimekit windows-shell

More Programming Questions

More Fitness Calculators

More Chemical reactions Calculators

More Organic chemistry Calculators

More Biology Calculators