villaresources.blogg.se

Black and white image with color lines
Black and white image with color lines













black and white image with color lines black and white image with color lines

Īngle_diff = np.minimum(angle_diff, 180 - angle_diff) Rect, box, line, angle = get_rect_box_line_and_angle(c)Īngle_diff = (angle - angle2 + 720) % 180 # Angle difference in degrees (force it to be positive number in range. # Note: The solution is kind of "brute force" solution, and can be better. # Loop decreases n_lines when two lines are merged. # Merge contours with close angles, and close edges # Find contours in thresh_gray, and remove small contours. Thresh_intersect = 255 # Image of intersection points only. Thresh_intersect = np.zeros_like(thresh_gray) Thresh_float = cv2.filter2D(thresh_float, -1, np.ones((3, 3))) # Filter with ones 5x5 Thresh_float = thresh_gray.astype(float) / 255 # Convert to float with range # Split intersection points between lines (fill crossing points with black). Ret, thresh_gray = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

black and white image with color lines

Gray = im # Get only the green color channel (the blue lines should be black). # Convert image to binary image with white lines on black background Im = cv2.imread('BlueLines.png') # Read input image (cv_major_ver, cv_minor_ver, cv_subminor_ver) = (cv2._version_).split('.') # Get version of OpenCV """Return minAreaRect, boxPoints, line and angle of contour""" """Finds minimum distance between any two edges of two lines""" """Convert rotated rectangle box into two array of two points that defines a line"""ĭif0 = (b - p0)**2 + (b - p0)**2 Here is a working code sample: import cv2 Merge contours with close angles, and close edges.Find contours in binary image (and remove small contours).Split intersection points between lines (fill crossing points with black).Convert image to binary image with white lines on black background.The solution below uses the following stages: Instead of merging contours, you may find many short lines, and merge them into long lines based on close angle and edges proximity. The solution I found is based on finding and merging contours, but using Hough Transform may be more robust. You should add extra checks, such as the total number of line-ends being even, and adding a 1 pixel wide black border around the edge in case a line touches the edge and so on.Īt first glance the problem looks simple - convert to binary image, use Hough Line Transform, and count the lines, but it's not working. Note this is not production quality code. Result = generic_filter(im, lineEnds, (3, 3)) # Invert and threshold for white lines on black Im = Image.open('lines.png').convert('L') # Central pixel and one other must be 255 for line end You could count the line ends and divide by two.















Black and white image with color lines